#!/bin/ksh
#
#                   MAKE_INSTALL
#
#   A tool to make install files for to go on the ftp site.
#
#   NOTE, you cannot use this script to make tar files for the
#   the server for acedb versions earlier than 4_8 because it
#   only produces archives of the socket server binaries.
#
#
###################################################################
#
#
# All download files should be of the pattern:
#
#        ACEDB-<package>.<version>.tar.gz
#
# where
#        <package> is currently   binary<ARCH> | source | docs | help | tools | demo
#
#        <version> is of the form "version"_"release""update", e.g. 4_8d
#
# e.g.       ACEDB-source.4_8d.tar.gz
#       ACEDB-binaryALPHA.4_7l.tar.gz
#
# The INSTALL script in wtools expects that the files will have this
# naming pattern and will probably break if this is not so.
#
#
# NOTES:
#        1) -q option doesn't do anything at the moment.     
#
#        2) When this is thoroughly tested the "-v" option needs to be
#           removed from the tar commands to cut down on output.
#
#        3) script calls wtools/aceGetVersion
#
#        4) script relies on output of "tace -version" not changing.
#
#
#


# Set to non-NULL to turn on debug in whole script.
#debug=""
debug="1"


#--------------------------  functions  -------------------------------

function print_usage
{
  if [[ -n $1 ]]
  then
    print
    print "Warning: $1"
  fi

  print
  print "Usage:        MAKE_INSTALL [-b|-e|-d|-h|-s|-t|-v|-w] [-q] [-a <nnn>] <source_directory>"
  print
  print "  -b|-e|-d|-h|-s  specifies type of archive (-b is default), where:"
  print
  print "     -b  ALPHA | LINUX | SGI | SOLARIS  makes a non-server binary archive for named platform"
  print "     -e  ALPHA | LINUX | SGI | SOLARIS  makes a server binary archive for named platform"
  print "     -d  makes a documents archive"
  print "     -h  makes an acedb help files archive"
  print "     -s  makes a source code archive"
  print "     -t  makes a tools code archive"
  print "     -v  makes a file containing a description of the code archive"
  print "     -w  makes a demo database archive"
  print
  print "  -q        MAKE_INSTALL will without asking any questions and will just fail"
  print "            if there is any doubt about overwriting files etc."
  print
  print "  -a  <nnn> specifies directory where archive should be placed, default is PWD"
  print
  print "  <source_directory> specifies directory from which archive is to be made,"
  print "                     directory should be one of the standard acedb build directories"
  print "                     named RELEASE.nnn. Program will then construct archive either"
  print "                     from RELEASE.nnn or RELEASE.nnn.BUILD"
  print

  exit 1
}

function bomb_out
{
  print
  print "Fatal Error : $1"
  print

  exit 1
}

function print_divider
{
  print
  print '====================================================================='
  print
}


function createVersionFile
{
  today=$(date)
  print '<!--      ACEDB Build Archive Details              -->'   >  $4
  print '<!-- file created by: wtools/MAKE_INSTALL          -->'   >> $4
  print '<!-- file created on: '$today' -->\n'                     >> $4
  print '<!--#set var="Version"         value="'$1'" -->'          >> $4
  print '<!--#set var="Compiled"        value="'$2'" -->'          >> $4
  print '<!--#set var="Build_directory" value="'$3'" -->'          >> $4
  print '<!--#set var="Aquila_results"  value="NOTHING YET" -->'   >> $4

  # this is really yucky, I'm not sure if all this shouldn't be in the
  # updateftp code really...perhaps that would be MUCH better.
  for i in $alpha $linux $sgi $sun
  do
    os_dir=$(ls | grep $i)
    os_level=$(cat $os_dir'/'$i*'.OS')
    print '<!--#set var="'$i'_OS_level" value="'$os_level'" -->'          >> $4
  done
}


#------------------------- main routine -------------------------------


# filenames
prefix='ACEDB'

srcname='source'
srcpattern='./w*'
binname='binary'
binpattern='./acediff ./dotter ./giface ./tace ./tagcount ./taql ./xace ./xremote'
servername='server'
serverpattern='./makeUserPasswd ./saceclient ./saceserver ./sgifaceserver ./sxaceclient'
helpname='help'
helppattern='./*'
docname='doc'
docpattern='./*'
toolsname='tools'
toolspattern='./*'
demoname='demo'
demopattern='./*'
versionname='BuildVersion.shtml'
versionpattern=''

suffix='tar'
gzsuffix='gz'

# machine architectures
alpha='ALPHA'
linux='LINUX'
sgi='SGI'
sun='SOLARIS'

# set some defaults
tar_type=$binname
pattern=$binpattern
architecture=$alpha
target_dir=$PWD
work_dir='/tmp'
quiet=""
currdir=$PWD


# Do the flags... (see usage string for description of switches/arguments)
while getopts :b:e:dhstvwqa: arguments
do
  case $arguments in
    'b') case $OPTARG in
           $alpha | $linux | $sgi | $sun) architecture=$OPTARG ;;
           *) print_usage "-$arguments must be followed by: $alpha | $linux | $sgi | $sun"
         esac
         tar_type=$binname
         pattern=$binpattern ;;
    'e') case $OPTARG in
           $alpha | $linux | $sgi | $sun) architecture=$OPTARG ;;
           *) print_usage "-$arguments must be followed by: $alpha | $linux | $sgi | $sun"
         esac
         tar_type=$servername
         pattern=$serverpattern ;;
    'd') tar_type=$docname
         pattern=$docpattern ;;
    'h') tar_type=$helpname
         pattern=$helppattern ;;
    's') tar_type=$srcname
         pattern=$srcpattern ;;
    't') tar_type=$toolsname
         pattern=$toolspattern ;;
    'v') tar_type=$versionname
         pattern=$versionpattern ;;
    'w') tar_type=$demoname
         pattern=$demopattern ;;
    'q') quiet="yes" ;;
    'a') if [[ -d $OPTARG ]]
         then
           target_dir=$OPTARG
         else
           bomb_out "argument to -$arguments ($OPTARG) is not a directory."
         fi ;;

    # garbage input
    '?') print_usage "-$OPTARG is not a valid switch" ;;
    ':') print_usage "You forgot the argument for switch -$OPTARG" ;;
  esac
done

# Now do the remaining args...
((args_left = OPTIND - 1))                  
shift $args_left

if [[ -z $1 ]]
then
  print_usage "You did not specify a directory from which to make the install file"
elif [[ -n $2 ]]
then
  print_usage "Too many command line arguments: $*"
else
  source_dir=$1
fi


# check source directory that is to be archived.
if [[ ! -d $source_dir || ! -d $source_dir'.BUILD' ]]
then
  bomb_out "Both $source_dir & $source_dir.BUILD are required to make the archive."
fi


# Get the release number using wtools script.
verprog=$source_dir'.BUILD/wtools/aceGetVersion'
vertarget=$source_dir'.BUILD/wnq'
if [[ ! -d $vertarget || ! -f $verprog || ! -x $verprog ]]
then
  bomb_out "Can't find out acedb release version, need $verprog and $vertarget"
fi
release=$($verprog $vertarget)



if [[ -n $debug ]]
then
  print "startup values:"
  print "tar_type=$tar_type, pattern=$pattern, architecture=$architecture"
  print "source_dir=$source_dir, target_dir=$target_dir, release=$release"
fi


print_divider
print "      ACEDB build of $tar_type installation file starting..."
print_divider


# check for directory and try to cd to it.
if [[ $tar_type != $binname && $tar_type != $versionname ]]
then
  source_dir=$source_dir'.BUILD'
fi
if [[ -d $source_dir ]]
then
  print "Using $source_dir to make $tar_type installation file"
  print
else
  bomb_out "$source_dir is not a directory"
fi
cd $source_dir || bomb_out "cannot cd to $source_dir"


# debugging...
ls -l $serverpattern


# construct file names for archives.
if [[ $tar_type = $binname || $tar_type = $servername ]]
then
  archive_name=$prefix'-'$tar_type$architecture'.'$release'.'$suffix
elif [[ $tar_type = $versionname ]]
then
  archive_name=$prefix'-'$tar_type
else
  archive_name=$prefix'-'$tar_type'.'$release'.'$suffix
fi

short_name=$archive_name
if [[ $tar_type = $versionname ]]
then
  long_name=$archive_name
else
  long_name=$archive_name'.'$gzsuffix
fi
archive_name="$target_dir/$archive_name"

print "Archive will be created as $long_name"
print


# Either create a flat file containing version/build details
# or one of the code archives.
if [[ $tar_type = $versionname ]]
then
  # Make a flat file containing the version details.

  tace='./bin.'$ACEDB_MACHINE'/tace'

  if [[ ! -x $tace ]]
  then
    bomb_out "Cannot find executable $tace to get acedb compile date"
  fi

  compile_date=$($tace '-version')

  # I'd like to use Korn patterns here but our stupid Korn shells are
  # too ancient...so I'm relying on "set"
  #   pattern='compiled on:'
  #   acedb_version=${compile_date%$pattern}
  #   acedb_builddate=${compile_date#$pattern}
  #
  set $compile_date
  acedb_version="$2"
  acedb_builddate="$5 $6 $7 $8"

  if [[ -z $acedb_version || -z $acedb_builddate ]]
  then
    bomb_out "Can't find version and/or compile date from version string from tace: $1"
  fi

  createVersionFile "$acedb_version" "$acedb_builddate" "`basename $source_dir`" "$archive_name"

else

  # For some archives we need to cd to a specific directory to make the archive,
  # a bit mucky for the bin dir, where more is required than I thought...
  case $tar_type in
    $binname) tar_dir=$(ls | grep $architecture)  ;;
    $servername) tar_dir=$(ls | grep $architecture)  ;;
    $helpname) tar_dir='whelp' ;;
    $docname) tar_dir='wdoc' ;;
    $toolsname) tar_dir='wtools' ;;
    $demoname) tar_dir='wdemo' ;;
    *) tar_dir='./' ;;
  esac
  cd $tar_dir || bomb_out "Can't cd to $tar_dir"

  # OK, tar up the files...
  print "Creating tar file archive $short_name"
  tar -cvf$archive_name $pattern >/dev/null || bomb_out "tar of $tar_type files failed."
  print

  # For the binary archive we need to stick some extra stuff on the end of the
  # archive.
  if [[ $tar_type = $binname ]]
  then
    tar_dir=$source_dir'.BUILD'
    cd $currdir || bomb_out "Can't return to intial directory $currdir"
    cd $tar_dir || bomb_out "Can't cd to $tar_dir to finish $tar_type tar."

    pattern="./wspec ./wdoc ./wquery ./wscripts ./wgf ./wtools"

    tar -rvf$archive_name $pattern >/dev/null || bomb_out "tar of $tar_type files failed."
  fi


  # Now check that the archive got made successfully using tar -t
  tar -tf$archive_name >/dev/null || bomb_out "check of tar file integrity failed."


  # do the gzip...
  print "Compressing $short_name into $short_name.$gzsuffix"
  gzip $archive_name || bomb_out "could not run compress $archive_name"
  print
  archive_name=$archive_name'.'$gzsuffix

fi


print "The installation file is stored as $archive_name"

print_divider

exit 0

