#!/usr/bin/perl
#
# Creates "tags" (touched empty files) in 'modeldir'
# area so we can see which files are being used.
#
# Files for R510 would be in $SRCDIR/r510/
#
###################

use File::Basename;

$SRCDIR="/afs/.atlas.umich.edu/hardware/Dell_BIOS_FW";

foreach $script (glob("$SRCDIR/*update.sh")) {
    $script =~ /\/(\w*)update.sh/;
    $model = $1;
    print " Found model $model\n";
    if ( ! -d "$SRCDIR/$model" ) {
	warn " Directory not found for $model\n";
	system("mkdir -p $SRCDIR/$model");
    } else {
# Directory exists...need to clean it out for updating...
	print " Directory $model exists...cleaning\n";
	foreach $f (glob("$SRCDIR/$model/*")) {
	    unlink("$f") or die "Unable to remove file $f: $!\n";  
	}
    }
# Process current update script to find relevant BIN files for this model
    open(IN,"<$script") or die "Unable to open $script: $!\n";
    while(<IN>) {
#	print " Read |$_|\n";
        if (/\$SRCDIR\/(\S+)/) {
            $bfile=$1;
	    print " Found $bfile for $model...\n";
# Verify file exists
	    $bvers="$bfile.version";
	    if ( -e "$SRCDIR/$bvers" ) {
		print " File $bfile version exists\n";

# Make tags of .BIN files
# Get version string ready
		$vstring=`grep -h  -A1 "Title\:" $bvers  | grep -v "Title\:" | tr ' ,\/()"' "_______"`;
		chomp($vstring);
		$bvnew=$bvers;
		$bvnew=~ s/\.BIN\.version//;
# Break apart directory and name to allow us to insert the $model directory
		($bfile,$bdir,$suffix)=fileparse($bvnew);
		$outfile="$bdir/$model/$bfile-$vstring";
		if ( -e $outfile ) {
		    print " Already have info for $bvnew\n";
		} else {
		    print " Touching $outfile\n";
		    system("touch $outfile");
		}
	    } else {
		warn " Unable to locate $bvers in $SRCDIR!\n";
	    }
	} else {
	}
    }
    close(IN) or warn "Unable to close $script: $!\n";	
}
$model="drives";
$script="update-drives.sh";
print " Looking at $model...\n";
if ( ! -d "$SRCDIR/$model" ) {
    warn " Directory not found for $model\n";
    system("mkdir -p $SRCDIR/$model");
} else {
# Directory exists...need to clean it out for updating...
    print " Directory $model exists...cleaning\n";
    foreach $f (glob("$SRCDIR/$model/*")) {
	unlink("$f") or die "Unable to remove file $f: $!\n";  
    }
}
# Process current update script to find relevant BIN files for this model
open(IN,"<$script") or die "Unable to open $script: $!\n";
while(<IN>) {
#	print " Read |$_|\n";
    if (/\$SRCDIR\/(\S+)/) {
	$bfile=$1;
	print " Found $bfile for $model...\n";
# Verify file exists
	$bvers="$bfile.version";
	if ( -e "$SRCDIR/$bvers" ) {
	    print " File $bfile version exists\n";

# Make tags of .BIN files
# Get version string ready
	    $vstring=`grep -h  -A1 "Title\:" $bvers  | grep -v "Title\:" | tr ' ,\/()"' "_______"`;
	    chomp($vstring);
	    $bvnew=$bvers;
	    $bvnew=~ s/\.BIN\.version//;
# Break apart directory and name to allow us to insert the $model directory
	    ($bfile,$bdir,$suffix)=fileparse($bvnew);
	    $outfile="$bdir/$model/$bfile-$vstring";
	    if ( -e $outfile ) {
		print " Already have info for $bvnew\n";
	    } else {
		print " Touching $outfile\n";
		system("touch $outfile");
	    }
	} else {
	    warn " Unable to locate $bvers in $SRCDIR!\n";
	}
    } else {
    }
}
close(IN) or warn "Unable to close $script: $!\n";	
exit;
