#!/usr/bin/perl -w

# $Id: copy_headers,v 1.1 2006/03/24 23:27:07 craig Exp $

#               Copyright (C) 2006, BroadOn Communications Corp.
#
#  These coded instructions, statements, and computer programs contain
#  unpublished  proprietary information of BroadOn Communications Corp.,
#  and  are protected by Federal copyright law. They may not be disclosed
#  to  third  parties or copied or duplicated in any form, in whole or in
#  part, without the prior written consent of BroadOn Communications Corp.

# A program to copy source files from one tree to another, ignoring
# differences caused by some CVS keyword substitution

use strict;
use Getopt::Long;
use POSIX qw(tmpnam);

sub
usage()
{
    print "Usage: $0 from_file to_file\n";
}

sub
copy_retain_cvs_id($$)
{
    my ($in_file_name, $out_file_name) = @_;
    my ($line, $copy_line);

    open (IN_FILE,  $in_file_name) or die "Can't open $in_file_name";
    open (OUT_FILE, '>' . $out_file_name) or die "Can't open $out_file_name";

    while (defined ($line = <IN_FILE>)) {
        if ($line =~ m/\$Id([^\$]*)\$/o) {
            $copy_line = $line;
            $copy_line =~ s/\$Id/\$Copy/;
            print OUT_FILE $line;
            print OUT_FILE $copy_line;
        }
        else {
            print OUT_FILE $line;
        }
    }
    close(IN_FILE);
    close(OUT_FILE);
}

sub
strip_version_strings($$)
{
    my ($in_file_name, $out_file_name) = @_;
    my ($line);

    open (IN_FILE,  $in_file_name) or die "Can't open $in_file_name";
    open (OUT_FILE, '>' . $out_file_name) or die "Can't open $out_file_name";

    while (defined ($line = <IN_FILE>)) {
        if ($line =~ m/\$Id[^\$]*\$/o) {
            # Do nothing
        }
        elsif ($line =~ m/\$Header[^\$]*\$/o) {
            # Do nothing
        }
        elsif ($line =~ m/\$Copy[^\$]*\$/o) {
            # Do nothing
        }
        else {
            print OUT_FILE $line;
        }
    }
    close(IN_FILE);
    close(OUT_FILE);
}

sub
compare_without_version_strings($$)
{
    my ($file_a, $file_b) = @_;
    my ($tmp_file_a, $tmp_file_b);
    my ($cmp_status);

    $tmp_file_a = POSIX::tmpnam();
    $tmp_file_b = POSIX::tmpnam();

    strip_version_strings($file_a, $tmp_file_a);
    strip_version_strings($file_b, $tmp_file_b);

    $cmp_status = system("cmp -s $tmp_file_a $tmp_file_b");

    unlink($tmp_file_a);
    unlink($tmp_file_b);

    if (($cmp_status & 0xFF) != 0) {
        die "cmp program exited on a signal";
    }
    else {
        $cmp_status >>= 8;
    }


    return($cmp_status);
}
sub
main()
{
    my ($help, $verbose);
    my ($opt_return);
    my ($from_path, $to_name);

    $opt_return = GetOptions (
                                 "help"    => \$help,
                                 "verbose" => \$verbose,
                             );

    if (! defined($ENV{'GBA_SRC'})) {
        exit(0);
    }
    if ($#ARGV != 1) {
        usage();
        exit(1);
    }
    $from_path = "$ENV{'GBA_SRC'}/$ARGV[0]";
    $to_name   = $ARGV[1];

    if (! -e $from_path) {
        print ("Error: file $from_path doesn't exist\n");
        exit(1);
    }
    if (! -e $to_name) {
        copy_retain_cvs_id($from_path, $to_name);
    }
    elsif (compare_without_version_strings($from_path, $to_name) != 0) {
        if (defined($verbose)) {
            print "Copying '$from_path' to '$to_name'\n";
        }
        copy_retain_cvs_id($from_path, $to_name);
    }
    else {
        if (defined($verbose)) {
            print "NOT copying '$from_path' to '$to_name'\n";
        }
    }
}

main;
