#!/usr/bin/perl

# Copyright (c) 2012-2014 by the GalSim developers team on GitHub
# https://github.com/GalSim-developers
#
# This file is part of GalSim: The modular galaxy image simulation toolkit.
# https://github.com/GalSim-developers/GalSim
#
# GalSim is free software: redistribution and use in source and binary forms,
# with or without modification, are permitted provided that the following
# conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
#    list of conditions, and the disclaimer given in the accompanying LICENSE
#    file.
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions, and the disclaimer given in the documentation
#    and/or other materials provided with the distribution.
#

# Typical usage:
#
# Copy the old copyright notice to oldcr, and the new one to newcr.
# Then type things like the following:
#
#     ls ../*/*.py | updatecr
#
# Use the # version for *.py, *.yaml, and use the /**/ version for *.h, *.cpp.
#
# After you've finished the obvious cases, it's a good idea to check
# that you didn't miss anything with lines like
#
#     grep Copyright */*/* | grep 2013
#
# There are a few other random things that may be easier to update manually.
# Especially when the copyright notice doesn't start on the first line (this
# file for example).  Also, don't forget to update the docstring in __init__.py
# and the license in doxypy.py

# Count how many lines to remove
open(A,"oldcr") || die "Cannot open oldcr: $!";
$nlines = 0;
while (<A>) {
    $nlines = $nlines + 1
}

while ($file = <STDIN>) {
    $file =~ s/\s+$//;  # Remove trailing eol
    print $file;
    open(F,$file) || die "Cannot open $file: $!";
    open(G,"newcr") || die "Cannot open newcr: $!";
    open(J,">junk") || die "Cannot open junk: $!";

    # Skip the first nlines
    $count = $nlines;
    while ($count > 0) { <F>; --$count; }

    # Write the new cr file
    while (<G>) {
        print J $_;
    }

    # Write the rest of the source file
    while (<F>) {
        print J $_;
    }

    # Done first pass
    close G;
    close F;
    close J;

    open(F,">$file") || die "Cannot open $file: $!";
    open(J,"junk") || die "Cannot open junk: $!";

    # Copy back to original file name
    while (<J>) {
        print F $_;
    }
    print "   done\n"
}

