#!/usr/bin/env perl
use strict;
use Getopt::Long;
use File::Path;

my $CASE   = `./xmlquery  CASE		  -value `;
my $GMAKE	    = `./xmlquery  GMAKE	  -value `;
my $CASETOOLS  = `./xmlquery  CASETOOLS -value `;
# these are needed by the makefile and need to be in the environment

$ENV{CASEROOT} = `./xmlquery  CASEROOT -value `;
$ENV{DEBUG} = `./xmlquery  DEBUG -value `;
$ENV{USE_ESMF_LIB}= `./xmlquery USE_ESMF_LIB -value `;
$ENV{COMP_INTERFACE}= `./xmlquery COMP_INTERFACE -value `;

sub usage {
    die <<EOF;
    
SYNOPSIS
    $CASE.clean_build [options]
    Removes existing CESM object files and libraries  

OPTIONS
  all Cleans all objects
   no arguments implies -a -c -o -w -g -i -r -l
   -a Cleans the atm objects 
   -c Cleans the cpl objects
   -o Cleans the ocn objects
   -w Cleans the wav objects
   -g Cleans the glc objects
   -i  Cleans the ice objects
   -r  Cleans the rof objects
   -l  Cleans the lnd objects
  -m Cleans the mct library
  -s   Cleans the shr library
  -p  Cleans the pio library
  -t  Cleans the timing library (gptl)
  -h  Print Usage and exit.
EOF
}
my %opts;


GetOptions(
    "h|help" => \$opts{help},
    "a" => \$opts{atm},
    "c" => \$opts{cpl},
    "o" => \$opts{ocn},
    "w" => \$opts{wav},
    "g" => \$opts{glc},
    "i" => \$opts{ice},
    "r" => \$opts{rof},
    "l" => \$opts{lnd},
    "m" => \$opts{mct},
    "s" => \$opts{csmshare},
    "t" => \$opts{gptl},
    "p" => \$opts{pio},
) or usage();    

usage() if($opts{help});

my @opts = keys %opts;
my $cnt=0;
foreach(@opts){
    $cnt += $opts{$_};
}

# Set defaults - look for 'all' if no arguments provided, this preserves legacy functionality
if( $cnt==0){
    my $all = shift;
    print "$all\n";
    if($all eq 'all'){
	$opts{mct}=1;
	$opts{pio}=1;
	$opts{gptl}=1;
	$opts{csmshare}=1;
    }
    $opts{atm} = 1;
    $opts{cpl} = 1;
    $opts{ocn} = 1;
    $opts{wav} = 1;
    $opts{glc} = 1;
    $opts{ice} = 1;
    $opts{rof} = 1;
    $opts{lnd} = 1;
}

if($opts{mct}+$opts{pio}+$opts{gptl}+$opts{csmshare} >= 1){
}


if($opts{csmshare}==1){
}


my $sysmod = "$GMAKE -f $CASETOOLS/Makefile ";

foreach my $lib (@opts){
    if($opts{$lib}==1){
	$sysmod .= "clean$lib ";
    }
}
system($sysmod) == 0 or die "$sysmod failed: $?\n";
my @lockedfiles = glob("LockedFiles/env_build*");
foreach my $lf (@lockedfiles) {
    unlink($lf);
}
system("./xmlchange SMP_BUILD=0,NINST_BUILD=0,BUILD_COMPLETE=FALSE,BUILD_STATUS=0");
