#!/usr/local/bin/perl

if ( $#ARGV != 1 ) {
   print "Usage: ctinitproj project-name project-root\n" ;
   print "    You should already be attached to the project\n" ;
   exit print "    ex: ctinitproj dtool $DTOOL\n" ;
}

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

$projname = $ARGV[0] ;
$projroot = &CTUShellEval( $ARGV[1] );
$flav = &CTQueryProj( $projname ) ;
$spec = &CTResolveSpec( $projname, $flav ) ;

# make sure the directory exists
if ( ! -e $projroot ) {
   die "Project root ('$projroot') does not exist" ;
}

require "$tool/include/ctcm.pl" ;

# make all the default subdirectories
$item = "$projroot/bin" ;
if ( ! -e $item ) {
    if ( ! &CTCMMkdir( $item, $projname, $spec ) ) {
	die "Could not create directory '" . $item . "'\n" ;
    }
}
$item = "$projroot/etc" ;
if ( ! -e $item ) {
    if ( ! &CTCMMkdir( $item, $projname, $spec ) ) {
	die "Could not create directory '" . $item . "'\n" ;
    }
}
$item = "$projroot/include" ;
if ( ! -e $item ) {
    if ( ! &CTCMMkdir( $item, $projname, $spec ) ) {
	die "Could not create directory '" . $item . "'\n" ;
    }
}
$item = "$projroot/lib" ;
if ( ! -e $item ) {
    if ( ! &CTCMMkdir( $item, $projname, $spec ) ) {
	die "Could not create directory '" . $item . "'\n" ;
    }
}
$item = "$projroot/lib/ss" ;
if ( ! -e $item ) {
    if ( ! &CTCMMkdir( $item, $projname, $spec ) ) {
	die "Could not create directory '" . $item . "'\n" ;
    }
}
$item = "$projroot/lib/stk" ;
if ( ! -e $item ) {
    if ( ! &CTCMMkdir( $item, $projname, $spec ) ) {
	die "Could not create directory '" . $item . "'\n" ;
    }
}
$item = "$projroot/src" ;
if ( ! -e "$projroot/src" ) {
    if ( ! &CTCMMkdir( $item, $projname, $spec ) ) {
	die "Could not create directory '" . $item . "'\n" ;
    }
}
$item = "$projroot/src/all" ;
if ( ! -e "$projroot/src/all" ) {
    if ( ! &CTCMMkdir( $item, $projname, $spec ) ) {
	die "Could not create directory '" . $item . "'\n" ;
    }
}

# install the project-wide Makefile
require "$tool/include/ctinstmake.pl" ;

$item = "$projroot/Makefile" ;
&CTInstallMake( "$tool/lib/Makefile.project.template", $item ) ;
if ( ! &CTCMMkelem( $item, $projname, $spec ) ) {
    die "Could not make a versioned element of the master makefile\n" ;
}

# now delta everything in
if ( ! &CTCMDelta( $item, $projname, $spec ) ) {
    die "Could not delta '" . $item . "'\n" ;
}
 $item = "$projroot/src/all" ;
if ( ! &CTCMDelta( $item, $projname, $spec ) ) {
    die "Could not delta '" . $item . "'\n" ;
}
$item = "$projroot/src" ;
if ( ! &CTCMDelta( $item, $projname, $spec ) ) {
    die "Could not delta '" . $item . "'\n" ;
}
$item = "$projroot/lib/stk" ;
if ( ! &CTCMDelta( $item, $projname, $spec ) ) {
    die "Could not delta '" . $item . "'\n" ;
}
$item = "$projroot/lib/ss" ;
if ( ! &CTCMDelta( $item, $projname, $spec ) ) {
    die "Could not delta '" . $item . "'\n" ;
}
$item = "$projroot/lib" ;
if ( ! &CTCMDelta( $item, $projname, $spec ) ) {
    die "Could not delta '" . $item . "'\n" ;
}
$item = "$projroot/include" ;
if ( ! &CTCMDelta( $item, $projname, $spec ) ) {
    die "Could not delta '" . $item . "'\n" ;
}
$item = "$projroot/etc" ;
if ( ! &CTCMDelta( $item, $projname, $spec ) ) {
    die "Could not delta '" . $item . "'\n" ;
}
$item = "$projroot/bin" ;
if ( ! &CTCMDelta( $item, $projname, $spec ) ) {
    die "Could not delta '" . $item . "'\n" ;
}
$item = "$projroot" ;
if ( ! &CTCMDelta( $item, $projname, $spec ) ) {
    die "Could not delta '" . $item . "'\n" ;
}
