#!/usr/local/bin/perl

sub CTMkElemUsage {
    print STDERR "Usage: ctmkelem [-c \"comment\"] [-nc] [-eltype type] element-name [...]\n" ;
    print STDERR "Options:\n" ;
    print STDERR "  -c \"comment\" : provide a comment about this action\n" ;
    print STDERR "  -nc : expect no comment on this action\n" ;
    print STDERR "  -eltype type : element type\n" ;
    exit ;
}

if ( $#ARGV < 0 ) {
    &CTMkElemUsage ;
}

$tool = $ENV{"DTOOL"} ;
if ( $tool eq "" ) {
   die "Environment not configured for CTtools" ;
}

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 = "" ;
$eltype = "" ;

$done = 0 ;

while ( ! $done ) {
    $done = 1 ;
    if ( $ARGV[0] eq "-nc" ) {
	shift( @ARGV ) ;
	&CTUDebug( "-nc processed\n" ) ;
	$done = 0 ;
    }
    if ( $ARGV[0] eq "-c" ) {
	shift( @ARGV ) ;
	$comment = $ARGV[0] ;
	shift( @ARGV ) ;
	&CTUDebug( "setting comment to '" . $comment . "'\n" ) ;
	$done = 0 ;
    }
    if ( $ARGV[0] eq "-eltype" ) {
	shift( @ARGV ) ;
	$eltype = $ARGV[0] ;
	shift( @ARGV ) ;
	&CTUDebug( "setting eltype to '" . $eltype . "'\n" ) ;
	$done = 0 ;
    }
}

if ( $#ARGV < 0 ) {
    &CTMkElemUsage ;
}

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

foreach $item ( @ARGV ) {
    if ( -e $item ) {
	if ( -d $item ) {
	    print STDERR "Cannot mkelem on an existing directory." .
		"  Ctmkdir it first.\n" ;
	} else {
	    if ( ! &CTCMMkelem( $item, $projname, $spec, $comment, $eltype )) {
		print STDERR "Could not make a versioned element of '" .
		    $item . "'\n" ;
	    }
	}
    } else {
	print STDERR "No such file '$item'.\n" ;
    }
}
