#!/usr/bin/perl
#
#-----------------------------------------------------------------
#
# Name:	create_version.pl
#
# RCS:  $Id: create_version.pl,v 1.5 2002/05/18 01:02:43 rick Exp $
#
# Copyright (C) 2001, Agile Storage, Inc.
#
# Description:	creates ImageVer string
#
#				usage: "create_version.pl BUILD_TARGET HOST_PLATFORM
#														IMAGE_VERSION_C_FILE"
#
# Created by:	Rick Lund
#
# Date Created:	4/14/01
#
#-----------------------------------------------------------------
#

$exe = $0;

if (@ARGV != 4 && @ARGV != 3)
{
	print "Invalid $exe usage\n";
	exit -1;
}

$tgt = shift;
$platform = shift;
$version_file = shift;
$txt_version_file = shift;

#get prod and variant from the pathname of $txt_version_file
($build,$prod,$variant,$junk) = split(/\//,$txt_version_file,4);

#print "create_version for $tgt in $version_file\n";

$header = "
/*
 *-----------------------------------------------------------------
 *
 * Image Version String file
 *
 * Automatically created by top-level Makefile calling
 * PERL script, $0
 *
 *-----------------------------------------------------------------
 */
";

open VER, ">$version_file" or die "Cannot create version file: $!";

print VER $header;
print VER "#include <nfx-types.h>\n\n";

if (-f "tree-name") {
  open(TREENAMEFILE, "tree-name") or die "Can't open tree-name: $!\n";
  PROC_LINES:
  while ($line = <TREENAMEFILE>) {
	  $_ = $line;
	  if (/^\#/) {next PROC_LINES;}
	  if (/^$/) {next PROC_LINES;}
	  ($key,$treename) = split(/:/,$_);
	  ($treename,$junk) = split(/\n/,$treename);
  }
}
else {
  if (-f "Tools/version-$prod") {
  	$relver = "Tools/version-$prod";
  }
  else {
    $relver = "Tools/version";
  }
  open(REL, "$relver")  or die "Can't open $relver: $!\n";
  @rel = <REL>; close(REL);
  $rel[0] =~ s/[a-zA-Z]//;
  $vstr = $rel[0];
  chomp($vstr);
  $prod =~ tr/a-z/A-Z/;
  $variant =~ tr/a-z/A-Z/;
  $DD = (localtime(time))[3];
  $MM = (localtime(time))[4];
  $MM++;
  $YYYY = (localtime(time))[5]+1900;
  $YY = $YYYY - 2000;
  if ($prod eq "BC" && $variant eq "DBG") {
    $treename = sprintf("R%s%s%s-%02d%02d%02d",$vstr,$prod,$variant,$MM,$DD,$YY); 
  }
  elsif  ($prod eq "BC") {
    $treename = sprintf("R%s%s-%02d%02d%02d",$vstr,$prod,$MM,$DD,$YY); 
  }
  elsif ($prod eq "CG" && $variant eq "DBG") {
    $treename = sprintf("R%s%s%s-%02d%02d%02d",$vstr,$prod,$variant,$MM,$DD,$YY); 
  }
  elsif  ($prod eq "CG") {
    $treename = sprintf("R%s%s-%02d%02d%02d",$vstr,$prod,$MM,$DD,$YY); 
  }
  elsif  ($variant eq "DBG") {
    $treename = sprintf("R%s%s-%02d%02d%02d",$vstr,$variant,$MM,$DD,$YY); 
  }
  else {
    $treename = sprintf("R%s-%02d%02d%02d",$vstr,$MM,$DD,$YY); 
  }
}

#print VER "uchar8 ImageVerString[80] = \"$tgt : $platform : ";

$str = sprintf("%s : %s : %s", $tgt, $treename, scalar localtime);
print VER "uchar8 ImageVerString[80] = \"$str\";\n";

if (! $txt_version_file eq "") {
	open TXTVER, ">$txt_version_file" or die "Cannot create version file: $!";
	print TXTVER "$str\n";
}
	


