#!/usr/local/bin/perl

if ($#ARGV < 0) {
   exit print "Usage: ctdelta element-name [...]\n" ;
}

$tool = $ENV{"DTOOL"} ;
if ( $tool eq "" ) {
    die "not configured for using ct-tools\n" ;
}

require "$tool/include/ctutils.pl" ;
require "$tool/include/ctvspec.pl" ;
require "$tool/include/ctquery.pl" ;
require "$tool/include/ctproj.pl" ;
require "$tool/include/ctcm.pl" ;

$comment = "" ;
$skip = 0 ;

@files = () ;

foreach $item ( @ARGV ) {
  if ( $skip == 0 ) {
      if ( $item eq "-nc" ) {
          &CTUDebug( "-nc processed\n" ) ;
      } elsif ( $item eq "-c" ) {
          $skip = 1 ;
      } else {
          push( @files, $item ) ;
          &CTUDebug( "added '" . $item . "' to files to be processed\n" ) ;
      }
  } elsif ( $skip == 1 ) {
      $comment = $item ;
      &CTUDebug( "setting comment to '" . $comment . "'\n" ) ;
      $skip = 0 ;
  } else {
      &CTUDebug( "got to unknown skip value! (" . $skip . ")\n" ) ;
      $skip = 0 ;
  }
}

$projname = &CTProj ;
$projname =~ tr/A-Z/a-z/ ;
$flav = &CTQueryProj( $projname ) ;
$spec = &CTResolveSpec( $projname, $flav ) ;

foreach $item ( @files ) {
    if ( -e $item ) {
	&CTCMCheckin( $item, $projname, $spec ) ;
	if ( ! &CTCMDelta( $item, $projname, $spec ) ) {
	    print STDERR "Could not delta '$item'\n" ;
	}
    } else {
	print STDERR "No such file '$item'.\n" ;
    }
}
