#!/usr/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/built/include/ctutils.pl" ;
require "$tool/built/include/ctvspec.pl" ;
require "$tool/built/include/ctquery.pl" ;
require "$tool/built/include/ctproj.pl" ;
require "$tool/built/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" ;
    }
}
