#!/usr/bin/perl
#****************************************************************/
# $RCSfile: buildToDoList.pl,v $
# ----------------------------------------------------------------
# $Source: /ma/miwa/CvsRepository/mjm/tools/A/CM/bin/buildToDoList.pl,v $
# ----------------------------------------------------------------
# $Revision: 1.6 $
# ----------------------------------------------------------------
# $Author: miwa $
# ----------------------------------------------------------------
# $Date: 2005/08/10 11:43:24 $
# ----------------------------------------------------------------
#****************************************************************/

# open files
open (FIN, "$ARGV[0]") || die "file not found: $ARGV[0]\n";
open (FOUT, ">$ARGV[1]") || die "file not found: $ARGV[1]\n";

# check batch mode
$batch = 0;
$batch = $ARGV[2] if ( $#ARGV == 2 );

# load ignore file
@Ignore = ();
if ( ! -f ".cm.ignore" && -f "$ENV{HOME}/.cm.ignore") {
	system("cp $ENV{HOME}/.cm.ignore .cm.ignore")
}
$IgnoreFile = ".cm.ignore";
if ( open (F_IGNORE, $IgnoreFile) )
{
    print "loading $IgnoreFile\n";
    @Ignore = <F_IGNORE>;
}

# process input file
while ( $line = <FIN> )
{
	$line =~ s/^\s+//;
    if ( $line =~ /^C/ )
    {
        print $line;
        die "Cannot proceed with conflicts";
    }
    elsif ( $line =~ /^\?/ )
    {
        # check ignore
        $ToIgnore = 0;
        foreach $IgnoreLine ( @Ignore )
        {
            chomp( $IgnoreLine );
            if ( $line =~ /$IgnoreLine/ )
            {
                $ToIgnore = 1;
                last;
            }
        }
        if ( ! $ToIgnore )
        {
            $Repeat = 1;
            $line =~ s/\?//;
            if ($batch) {
	        	print FOUT "A $line";
	        }
	        else {
            	
		        while ( $Repeat )
		        {
		            print $line;
		            print "What shall I do: (d)elete, (a)dd to CM, (B) add as binary, (i)gnore for ever or do (n)othing?\n";
		            $Answer = <STDIN> ;
		            chomp( $Answer );
		            $Answer = uc( $Answer );
		            if ( $Answer =~ /^[ABDI]/ )
		            {
		                print FOUT $Answer.$line;
		                $Repeat = 0;
		            }
		            elsif ( $Answer eq "N" )
		            {
		                $Repeat = 0;
		            }
		            else
		            {
		                print "unknown input!\n";
		            }
		        }
		    }
        }
    }
    elsif ( $line =~ /^U/ )
    {
        # check file existence
        $FileName = $line;
        chomp( $FileName );
        $FileName =~ s/U //;
        if ( open( F_TEST, $FileName ) )
        {
            close( F_TEST );
        }
        else
        {
            # file has vanished. What shall do ?
            if ($batch) {
	        	print FOUT "R $FileName";
	        }
	        else {
		        $Repeat = 1;
		        $line =~ s/\?//;
		        while ( $Repeat )
		        {
		            print $FileName."\n";
		            print "What shall I do: (r)emove file in CM or (u)pdate local file?\n";
		            $Answer = <STDIN> ;
		            chomp( $Answer );
		            $Answer = uc( $Answer );
		            if ( $Answer =~ /^[RU]/ )
		            {
		                print FOUT $Answer." ".$FileName."\n";
		                $Repeat = 0;
		            }
		            else
		            {
		                print "unknown input!\n";
		            }
		        }
		    }
        }
    }        
    elsif ( $line =~ /^M/ )
    {
        print FOUT $line;
    }
    elsif ( $line =~ /^D/ )
    {
        print FOUT $line;
    }
    elsif ( $line =~ /^R/ )
    {
        print FOUT $line;
    }
    elsif ( $line =~ /^A/ )
    {
        print FOUT $line;
    }
    elsif ( $line =~ /^B/ )
    {
        print FOUT $line;
    }
    elsif ( $line =~ /^I/ )
    {
        print FOUT $line;
    }
    else
    {
    	; # do nothing: other lines are ignored
    }
}
