#! /bin/csh -f
#-----------------------------------------------------------------------------
# filename: 
# 	mkpgstar
#
# description:
# 	Shell script to create the TOOLKIT_MTD tarfile from the PGS
# 	VOB in ClearCase.
#
# usage:
# 	mkpgstar [PGSTK] [-mklabel | -MKLABEL] 
# 	         [-ws <workspace-dir>] [-vob <VOB-dir>] [-label <label>]
#
# 	AT LEAST ONE of the group PGSTK must be selected
#
# 	  PGSTK    : select toolkit tar file creation
#
# 	  -h       : output this message and exit
# 	  -mklabel : enable labeling of files in the PGS VOB.
# 	  -MKLABEL : like -mklabel except label will be unlocked if needed.
# 	        (this is valid only in conjunction with option: PGSTK)
# 	  -ws      : set workspace area to <workspace-dir>
# 	  -vob     : set toolkit VOB area to <VOB-dir>
# 	  -label   : set toolkit label to <label> and name tar files
# 	             accordingly.
#
# notes:
# 	1) ClearCase view must be activated - 
# 	   If using the old-style directory structure, config spec MUST be 
# 	   set to contain an architecture-specific rule: (/main/{arch}/LATEST) !
# 
# 	2) The variable MKPGSTAR_WS allows you to override the default 
# 	    workspace area where the toolkit is to be copied, cleaned 
# 	    and re-tar'ed, as an alternative to the -ws option.
#
# 	3) The variable MKPGSTAR_VOB allows you to override the default
# 	    VOB area where the toolkit is located as an alternative to 
# 	    the -vob option.
#
# 	4) The variable MKPGSTAR_LABEL allows you to override the default
# 	   label used to label the toolkit files and name the tar files,
# 	   as an alternative to the -label option.
#
# author:
# 	Mike Sucher / A.R.C.
#       Phuong Nguyen/ L3 Communication Inc.
# history:
#	18-Oct-1994 MES Initial version
#	20-Oct-1994 MES Use variables to simplify switching the workspace area
# 			Add comments	
#	25-Oct-1994 DC  Add and test section to label VOB elements
#	27-Oct-1994 MES Include all README files, remove other stuff from bin
# 	14-Feb-1995 MES Update for toolkit Release 4
# 	23-Feb-1995 MES Rewrite command options, add messages.
# 	22-May-1995 MES Update for toolkit Release 5
# 	18-Jul-1995 MES Revise $PGSBIN cleanup section.
# 	20-Sep-1995 MES Revise to auto-configure for handling formal track
# 			directory structure.  
# 			Add -vob option and variable MKPGSTAR_VOB.
# 			Update prolog.
# 	21-Sep-1995 MES Add -label option to set the label
# 	31-Oct-1995 MES Updates for IR-1 release:
# 			 - change label to SDPTK_IR1v1.00
# 			 - remove PGSRUN from architecture-specific section
# 	08-Nov-1995 MES Move $PGSDAT/CBP/de200.eos to $PGSDAT/.. to avoid
# 			multiple copies of this file with the new version 
# 			of the INSTALL-Toolkit script.
# 	24-Apr-1996 MES Updates for Release A version of the toolkit:
# 			- revised PGSBIN cleanup section to use wildcard
# 			  in 'case' clause.  This will catch all files 
# 			  where the file name begins with "INSTALL".
#       16-Aug-2004 PN This file was original used for the sdp toolkit.
#                      Now it's modified to use for the toolkit_mtd
#----------------------------------------------------------------------------

set this_script = `basename $0`

if ($?MKPGSTAR_WS) then
    set workspace = ($MKPGSTAR_WS)
else
    set workspace=( /net/htsc/pgs/ir1 )
endif

if ($?MKPGSTAR_VOB) then
    set  pgsvob   = 	"$MKPGSTAR_VOB"
else
    set  pgsvob   = 	"/ecs/formal/TOOLKIT_MTD"
endif

set pgsvob_parent = 	"`dirname $pgsvob`"


if ($?MKPGSTAR_LABEL) then
    set tk_label = "$MKPGSTAR_LABEL"
else
    set tk_label = "MTDTK5.2.15v1.00"
endif


 
set pgstarfile  = "${tk_label}.tar"

# by default. no options are enabled

set PGSTK_tar   = "0"
set mklabel     = "0"
set help 	= "0"

# check command line for valid usage

if ("$1" == "") then
    set help = 1
endif

# get options from command line

while ("$1" != "")

    switch ("$1")

      case PGSTK:
        set PGSTK_tar  = "1"
        breaksw
 
      case "-h":
        set help = "1"
        breaksw

      case "-mklabel":
        set mklabel = "1"
        breaksw

      case "-MKLABEL":
        set mklabel = "2"
        breaksw
 
      case "-ws":
        set workspace = "$2"
        shift
        breaksw

      case "-vob":
        set  pgsvob = "$2"
        set pgsvob_parent = "`dirname $pgsvob`"
        shift
        breaksw

      case "-label":
        set tk_label = "$2"
        set pgstarfile  = "${tk_label}.tar"
        shift
        breaksw

     default:
        echo "${this_script}: invalid option : $1"
        echo ""
        breaksw

    endsw

    shift

end


if ("$help" == "1") then
    sed -n "/filename:/,/author:/p" $0 | cut -c3-100 | grep -v "author:" | more
#   this line MUST be here because it contains the string "author:"
    exit 1
endif

# startup messages
echo "Script to create the TOOLKIT_MTD tar file - 8/2006"
echo ""

echo "${this_script}: toolkit tar files script started at `date`"
echo ""
echo "Host: `hostname`"
echo "Tar files will be created in: $workspace"
echo ""

echo "Options selected: "
if ($PGSTK_tar == "1") then
    echo "    TOOLKIT_MTD tarfile $pgstarfile will be created."
else
    echo "    TOOLKIT_MTD tarfile will NOT be created."
endif

if ($mklabel != "0") then
    if ($PGSTK_tar == "0") then
        echo "    The -mklabel option is only valid in conjunction with option PGSTK"
        exit 1
    endif
    echo "    Files in the VOB will be labeled."
else
    echo "    Files in the VOB will NOT be labeled."
endif
echo ""


#
# sgi 64 bit build mode (1=enabled , 0=disabled)
#
set sgi_64_bit=1

# 
# get the operating system type
# 

set machine=`uname -m | awk '{print $1}'`	# get base machine type
switch ( "$machine" )
    case "i386":        			# SCO box
	setenv OSTYPE sco386
        breaksw
    case "CRAY":    				# CRAY
	setenv OSTYPE UNICOS
        breaksw
    default:					# everybody else
	setenv OSTYPE `uname`
        breaksw
endsw

set user=`id | cut -d\( -f2 | cut -d\) -f1`	# get username
if ($?LOGNAME == 0) setenv LOGNAME $user	# make sure $LOGNAME is defined
setenv USER $LOGNAME				# make sure $USER is defined

# set machine-dependent environment variables:
# 	host: the host name of this machine
# 	arch: used by other achitecture-specific code

switch ( "$OSTYPE" )
    case "AIX": 
        set host = `hostname`
        set arch = ibm
        breaksw
    case "HP-UX": 
        set host = `hostname`
        set arch = hp 
        breaksw
    case "IRIX":  
        set host = `hostname`
        set arch = sgi 
        breaksw
    case "IRIX64":  
        set host = `hostname`
        if ("$sgi_64_bit" == 1) then
            set arch = sgi64
        else
            set arch = sgi
        endif
        breaksw
    case "OSF1":  
        set host = `hostname -s`
        set arch = dec 
        breaksw
    case "sco386": 
        set host = `hostname -s`
        set arch = sco 
        breaksw
    case "SunOS": 
        # distinguish between SunOS 5.x and 4.x versions
        set cbrand=`uname -r | awk -F. '{print $1, $2}'`
        if ("$cbrand" == "5 10") then    
           set arch = sun5                   # release V5.10 SunOS
        else if ("$cbrand" == "5 9") then    
           set arch = sun5                    # release V5.9 SunOS
        else if ("$cbrand" == "5 8") then    
           set arch = sun5                    # release V5.8 SunOS
        else if ("$cbrand" == "5 5") then
           set arch = sun5                    # release V5.x SunOS  
       else                                
           set arch = sun4			# release V4.x SunOS
        endif
        set host = `hostname`
        breaksw
    case "UNICOS": 
        set host = `hostname`
        set arch = cray 
        breaksw
    default:
	echo "Operating system: $OSTYPE not supported"
        breaksw
endsw


# make sure we can find the workspace area before continuing

if (! -d $workspace ) then
    echo "Directory $workspace not found"
    exit 1
endif

if ( $?CLEARCASE_ROOT ) then	# a view is currently active
    set view_tag=(`basename $CLEARCASE_ROOT`)
    echo "Current ClearCase view: $view_tag"
    echo "Current config spec: "
    cleartool catcs
    echo ""
    echo "Be sure to include an architecture-specific branch in your config spec."
    echo ""
else
    echo "You must first activate a ClearCase view."
    exit 1
endif


#
# Make sure the VOB directory exists
#

if (! -d $pgsvob ) then
    echo "Directory $pgsvob not found"
    exit 1
endif

#
# Get the name of the top-level toolkit directory
#

set tk_top = `basename $pgsvob`


#
# Set up the workspace area
#

cd $workspace
echo "Setting directory to `pwd`"
if ( -d $tk_top ) then
    echo "Purging the old temporary toolkit directory"
    /bin/rm -r -f $tk_top
endif
echo "(Re-)creating the temporary toolkit directory"
mkdir $tk_top
echo ""


#
# determine whether formal track directory structure is in use
#

cd $pgsvob
set pgs_formal=1
foreach dir (database obj)

    if ( -d $dir ) then
        continue
    else
        set pgs_formal=0
    endif

end



#
# Load the files (skipping install and lost+found)
#

cd $pgsvob
echo "Setting directory to `pwd`"

set files = "`echo * | sed 's/install//' | sed 's/lost+found//'`"
set files = "`echo $files | sed 's/Makefile//' | sed 's/VERSION//'`"

if ("$pgs_formal" == 1) then
    set files = ( `echo $files | sed "s%bin%bin%"` )
    set files = ( `echo $files | sed "s%database%database/common%"` )
    set files = ( `echo $files | sed "s%lib%lib%"` )
    set files = ( `echo $files | sed "s%obj%obj%"` )
    set files = ( `echo $files | sed "s%test%test%"` )
endif


echo "The following files and directories will be copied:"
echo ""
echo "    $files"
echo ""

echo "Loading files to the temporary toolkit_mtd directory"
echo ""

tar cf - $files | ( chdir $workspace/$tk_top; tar xvf - )




#
# Add the empty test directory
#

cd $workspace/${tk_top}; pwd
if ( ! -d test ) then
mkdir ./test
endif

#
# Set up environment and clean up the toolkit directory
#

setenv PGSHOME $workspace/${tk_top}

setenv PGSBIN 	${PGSHOME}/bin		# exectuable files
setenv PGSDAT 	${PGSHOME}/database/common	# database files
setenv PGSINC 	${PGSHOME}/include	# include (header) files
setenv PGSLIB 	${PGSHOME}/lib  	# library files
setenv PGSOBJ 	${PGSHOME}/obj	        # object files
setenv PGSRUN 	${PGSHOME}/runtime	# runtime files
setenv PGSSRC 	${PGSHOME}/src		# toolkit source files
setenv PGSTST 	${PGSHOME}/test		# test area

#
# set architecture dependencies for formal track directory structure
#

if ("$pgs_formal" == 1) then

    setenv PGSBIN 	${PGSHOME}/bin		        # exectuable files
    setenv PGSDAT 	${PGSHOME}/database/common	# database files
    setenv PGSLIB 	${PGSHOME}/lib  		# library files
    setenv PGSOBJ 	${PGSHOME}/obj		        # object files
    setenv PGSTST 	${PGSHOME}/test		        # test files
endif

cd $PGSHOME

echo ""
echo "Cleaning up PGS home directory: `pwd`"
echo "This will restore the directoy to the pre-installed state."
echo ""

echo ""
echo "Setting file permissions ..."
cd $PGSHOME 
    find . -name "*" -type f -exec chmod u+w {} \;
cd $PGSINC
    find . -name "*.[fh]"   -type f -exec chmod 644 {} \;
    find . -name "*.[fh].*" -type f -exec chmod 644 {} \;
    find . -name "*.ada"    -type f -exec chmod 644 {} \;

cd $PGSDAT
    find . -name "earthfigure.dat"   -type f -exec chmod 644 {} \;
    find . -name " sim.csh"   -type f -exec chmod 755 {} \;
    find . -name "utcpole.dat"   -type f -exec chmod 444 {} \;
    find . -name "PGS_CUC_maths_parameters"   -type f -exec chmod 644 {} \;
    find . -name "udunits.dat"   -type f -exec chmod 644 {} \;
    find . -name "sc_tags.dat"   -type f -exec chmod 644 {} \;
    find . -name "configdata.dat"   -type f -exec chmod 644 {} \;
    find . -name "leapsec.dat"   -type f -exec chmod 444 {} \;

echo ""
echo "Removing object files ..."
cd $PGSSRC
    find . -name "start_time" -print -exec rm -f {} \;
    find . -name "*.o"        -print -exec rm -f {} \;
cd $PGSOBJ
    find . -name "*.o"        -print -exec rm -f {} \;

cd $PGSHOME
echo ""
echo "Removing Makefile and make directory ..."
    \rm -rf make Makefile
echo ""
echo "Removing .cmake.state files ..."
    find . -name ".cmake.*" -print -exec rm -f {} \;

cd $PGSSRC
echo ""
echo "Cleaning up in: `pwd`"
    find . -name "*~"        -print -exec rm -f {} \;
    find . -name "*.[cfh]" -type f -exec chmod 644 {} \;
    find . -name "makefile" -exec chmod 644 {} \;
    find . -name "makefile.cpp" -exec chmod 644 {} \;

#
# clean up in $PGSBIN
#
# Note: 
#   All files will be removed EXCEPT the ones specified in the
# upper group of case stements in the switch block.  Any files
# to be kept in future releases should be added there.
#

cd $PGSBIN
echo ""
echo "Cleaning up in: `pwd`"

set binfiles=( `\ls` )

foreach file ( $binfiles )

    switch ($file )

      case "INSTALL*":
      case "mkpgslib":
      case "mkpgslib.cpp":
      case "mkpgstar":      
      case "ftp.csh":
      case "pgs-flags":
      case "pgs-flags-cpp":
      case "common":
        echo "   keeping : $file"
        breaksw
      case "dec":
        echo "   keeping : $file"
        breaksw
      case "hp":
        echo "   keeping : $file"
        breaksw
      case "ibm":
        echo "   keeping : $file"
        breaksw
      case "irix65":
        echo "   keeping : $file"
        breaksw
      case "linux":
        echo "   keeping : $file"
        breaksw
      case "macintosh":
        echo "   keeping : $file"
        breaksw
      case "sgi":
        echo "   keeping : $file"
        breaksw
      case "sgi32":
        echo "   keeping : $file"
        breaksw
      case "sgi64":
        echo "   keeping : $file"
        breaksw
      case "sun5":
        echo "   keeping : $file"
        breaksw
      case "sun5.8":
        echo "   keeping : $file"
        breaksw
      case "sun5.9":
        echo "   keeping : $file"
        breaksw
      case "sun5.10":
        echo "   keeping : $file"
        breaksw
      case "winnt98":
        echo "   keeping : $file"
        breaksw
      case "tmp":
        echo "   keeping : $file"
        breaksw

      default:
        echo "   removing: $file"
        /bin/rm -f $file
        breaksw
    endsw

end

#
# clean up in $PGSRUN
#
# Note: 
#   Removing old PCFs.
#

cd $PGSRUN
echo ""
echo "Cleaning up in: `pwd`"

set files=(`\ls Log*`)
if ($status == 0) then
    foreach file ($files)
        echo "    $file"
        /bin/rm -f  $file
    end
endif

cd $PGSINC
echo ""
echo "Cleaning up in: `pwd`"

set files=(`\ls PGS*_{[0-9],[0-9][0-9]}.[hf]`)
if ($status == 0) then
    foreach file ($files)
        echo "    $file"
        /bin/rm -f  $file
    end
endif

if (-f PGS_TYPES.h.non-cray) then
    mv PGS_TYPES.h.non-cray PGS_TYPES.h
endif

find . -name makefile.CM -exec rm {} \;


cd $PGSLIB
echo ""
echo "Cleaning up in: `pwd`"

set files=(`\ls *.a *.a.old`)
if ($status == 0) then
    foreach file ($files)
        echo "    $file"
        /bin/rm -f  $file
    end
endif


#
# Create the toolkit tar file
#

echo ""
echo ""
cd $workspace
echo "Setting directory to `pwd`"
echo "Create the Toolkit tar file $pgstarfile ..."
echo ""

tar cvf $pgstarfile ${tk_top}

#
# Label the files in the VOB
#

if ("$mklabel" != "0") then

    echo ""
    echo ""
    cd $workspace; echo "Setting directory to `pwd`"
    echo "Adding labels to all VOB elements put in tar file"
    echo ""

    set label_ls = "`cleartool lstype -lbtype -vob $pgsvob | grep $tk_label`"
    if ("$label_ls"  == "") then
        echo "Adding label type $tk_label to VOB $pgsvob"
        cleartool mklbtype -vob $pgsvob -nc $tk_label
    else
        set label_lk = `echo "'$label_ls'" | awk -F\( '/locked/ {print $2}' `
        set label_lk = `echo "$label_lk" | awk -F\) '{print $1}' `
        if ("$label_lk"  == "") then
            echo "Label type $tk_label was found in VOB $pgsvob - UNLOCKED"
        else
            echo "Label type $tk_label was found in VOB $pgsvob - LOCKED"
            if ("$mklabel" == "2") then
                cleartool unlock -lbtype -vob $pgsvob $tk_label
            else
                echo "Use option -MKLABEL to force the label to be unlocked"
                echo "Exiting."
                exit
            endif
        endif
    endif
    echo ""

    tar tvf $pgstarfile | awk '{print $7}' >! cclist

    foreach file ( `cat cclist` )
      /usr/atria/bin/cleartool mklabel -replace $tk_label ${pgsvob_parent}/$file
    end

    cleartool lock -lbtype -vob $pgsvob $tk_label

    echo ""
    echo "Finished adding labels"
    echo ""

endif 		# if ("$mklabel" == "1")


echo ""
echo ""
echo "${this_script}: toolkit tar file script completed."
echo ""
