#!/bin/csh -f
#-----------------------------------------------------------------------
#
# VISIT_INSTALL - Install a compressed tar file distribution of visit
#                 in an existing directory.
#
# Author: Eric Brugger
# Date:   October 26, 1996
#
# Usage
#    visit-install [-c open | closed] [-g group] [-gw] [-l] [-r]
#                  [-beta | -private] version platform directory
#
#    The default is to install this version as the current version.
#    The -beta flag makes this version the current beta version instead.
#    The -private flag simply adds this version to the tree without making
#        it the current stable or current beta.
#
# Notes:
#    The options must be first.
#
# Modifications:
#    Jeremy Meredith, Mon Mar 19 12:06:55 PST 2001
#    Made it create symlinks for the current or beta release.
#    Made it support a -beta and -private flag.
#
#    Eric Brugger, Thu Mar 29 14:21:39 PST 2001
#    Replace incorrect "else if" constructs with "elif".
#
#    Eric Brugger, Fri Mar 30 08:45:12 PST 2001
#    I undid my previous replacement of "else if" with "elif".  I
#    replaced tests for links (-l) with tests for existance (-e), since
#    tests for links behaved strangely on AIX systems.
#
#    Eric Brugger, Fri Mar 30 10:55:49 PST 2001
#    Reorder some lines cleaing up old versions and backing up the
#    current version so that it would work properly.
#
#    Eric Brugger, Wed Jul 18 15:51:32 PDT 2001
#    I added the -a flag.
#
#    Eric Brugger, Wed Feb 20 10:52:52 PST 2002
#    I added support for install sample data files.
#
#    Eric Brugger, Wed Aug 21 14:04:25 PDT 2002
#    I modified the script so that existing distributions would be
#    consistent as much as possible while being updated.
#
#    Eric Brugger, Mon Jul 21 17:53:22 PDT 2003
#    I added the ability to install a configuration file.
#
#    Eric Brugger, Thu Jul 31 12:09:02 PDT 2003
#    I removed some error tests that were redundant and caused a
#    maintenance issue.
#
#    Brad Whitlock, Wed Oct 8 16:29:25 PST 2003
#    Added support for clickable scripts in MacOS X.
#
#    Brad Whitlock, Tue Oct 21 14:07:33 PST 2003
#    I made the command script files that we generate on MacOS X have
#    execute file permissions so they can run when double-clicked in
#    the Finder.
#
#    Eric Brugger, Tue Dec 23 11:05:37 PST 2003
#    I added an option to enable usage logging.
#
#    Eric Brugger, Wed May 12 11:05:58 PDT 2004
#    I changed the default for append to be true, and replaced the -a
#    option with -r to make it false.
#
#    Eric Brugger, Thu Sep 23 13:41:22 PDT 2004
#    I corrected a bug where the usage logging flag in the visit script
#    doesn't get set when logging is enabled.  The sed command that made
#    the substitution assumed a certain number of spaces in the line,
#    which changed on a recent update.  The current sed command is more
#    forgiving of extra whitespace.
#
#    Eric Brugger, Thu Sep 30 10:09:21 PDT 2004
#    I made a chgrp command for installing on the mac conditional on the
#    user specifying a group on the command line.
#
#    Jeremy Meredith, Thu Dec  9 17:41:35 PST 2004
#    Made the switch to version-specific visit scripts.
#    Now the main "visit" script is a symlink to frontendlauncher,
#    legacylauncher is the old "visit" script, and frontendlauncher
#    is the new main script.  Thus we should just erase the old visit
#    script and treat legacylauncher and frontendlauncher as two
#    modern equivalents of the old visit script.  Added the ability to
#    launch tools without passing them as an argument to the visit script.
#
#-----------------------------------------------------------------------

set Compress = COMPRESS

set append = true
set beta = false
set private = false
set config = prompt
set logging = false
set group_ownership = none
set dir_permission = 755
set file_permission = 644

# Figure out the platform so if we're on a platform for which we need to
# perform extra steps, we know it.
set uname = `which uname`
set os = `$uname -s | tr "[A-Z]" "[a-z]" | tr -d "[0-9]"`

set option_found = true
while ($option_found == true)
   switch ($1)
      case -gw:
         set dir_permission = 775
         set file_permission = 664
         shift
         breaksw
      case -g:
         set group_ownership = $2
         shift
         shift
         breaksw
      case -r:
         set append = false
         shift
         breaksw
      case -beta:
         set beta = true
         shift
         breaksw
      case -private:
         set private = true
         shift
         breaksw
      case -c:
         set config = $2
         shift
         shift
         breaksw
      case -l:
         set logging = true
         shift
         breaksw
      default:
         set option_found = false
         breaksw
   endsw
end

if ($#argv != 3) then
   echo "Usage: visit-install [-c open | closed] [-g group] [-gw] [-l] [-r]"
   echo "                     [-beta | -private] version platform directory"
   exit (1)
endif

if ($beta == true && $private == true) then
   echo "Only -beta or -private may be specified, not both."
   exit (1)
endif

while ($config == prompt)
   echo "Choose a system configuration file.  A system configuration"
   echo "file contains profiles that make it easy to access VisIt on"
   echo "remote computers."
   echo ""
   echo "   1) No system configuration"
   echo "   2) LLNL open network"
   echo "   3) LLNL closed network"
   echo ""
   echo "(1,2,3):"

   set config = $<
   switch ($config)
      case 1:
         set config = none
         breaksw
      case 2:
         set config = open
         breaksw
      case 3:
         set config = closed
         breaksw
      default:
         set config = prompt
         breaksw
   endsw
end

if ($config != none && $config != open && $config != closed) then
   echo "Illegal configuration option, choices are none, open and closed."
   exit (1)
endif

set version = $1;
set base = `echo visit$version | tr "." "_"`
set platform = $2
set dir = $3

set fname1 = $base.$platform.tar.Z
if (! -e $fname1) then
   set Compress = GZIP
   set fname1 = $base.$platform.tar.gz
   if (! -e $fname1) then
      echo "Could not find file containing the distribution."
      exit (2)
   endif
endif
set fname2 = $base.$platform.tar

#
# If the directory name is not an absolute path then make it one.
#
if (`echo $dir | grep "^/"` == "") then
   set dir = `pwd`/$dir
endif

#
# Set up the distribution directory
#
rm -rf distribution
mkdir distribution
cp $fname1 distribution
cd distribution
if ($Compress == "COMPRESS") then
   uncompress $fname1
else
   gunzip $fname1
endif
tar xvbf 20 $fname2
cd ..

#
# If the installation directory doesn't exist, create it.
#
if (! -e $dir)                mkdir $dir
if (! -e $dir/bin)            mkdir $dir/bin
if (! -e $dir/data)           mkdir $dir/data

#
# If not in append mode remove any old backup files and directories
# from the installation directory.
#
if ($append == false) then
   if (-e $dir/bin/legacylauncher-)      rm -f $dir/bin/legacylauncher-
   if (-e $dir/bin/frontendlauncher-)    rm -f $dir/bin/frontendlauncher-
   if (-e $dir/bin/visit-)       rm -f $dir/bin/visit- # not what it used to be
   if ($beta == true) then
      if (-e $dir/beta-)         rm -f $dir/beta-
   else if ($private == false) then
      if (-e $dir/current-)      rm -f $dir/current-
   endif
   if (-e $dir/$version-)        rm -rf $dir/$version-
endif

#
# Copy in the new files.  We are careful to leave the current
# installation in a consistent state with the exception of the
# data directory.
#
if ($logging == false) then
   cp distribution/visit/bin/legacylauncher $dir/bin/legacylauncher+
else
   sed -e "s/logging[ \t]*= 0/logging        = 1/" distribution/visit/bin/legacylauncher > $dir/bin/legacylauncher+
endif
cp distribution/visit/bin/frontendlauncher $dir/bin/frontendlauncher+

if (-e $dir/$version+)           rm -rf $dir/$version+
if ($append == false) then
   mkdir $dir/$version+
else
   if (! -e $dir/$version)       mkdir $dir/$version
   mv $dir/$version $dir/$version+
   ln -s $version+ $dir/$version
endif
cd distribution/visit/$version
find . -print | cpio -pmud $dir/$version+
cd ../../..

cp distribution/visit/data/* $dir/data

#
# Set the link for the configuration file.
#
rm -f $dir/$version+/.visit/config
if ($config == open) then
   ln -s visit-config-open $dir/$version+/.visit/config
else if ($config == closed) then
   ln -s visit-config-closed $dir/$version+/.visit/config
endif

#
# If we're on MacOS X, then we want to create some small scripts that
# can be clicked on from the Finder and have VisIt work.
#
if("$os" == "darwin") then
    # Create a clickable script to run the cli, silex, and xmledit.
    set csh = `which csh`
    set scriptNames = ("VisIt" "cli" "Silex" "XmlEdit")
    set appNames = ("visit" "cli" "silex" "xmledit")
    @ i = 1
    while($i < 5)
        set app = $appNames[$i]
        set appscript = $scriptNames[$i]
        if(-e $dir/bin/$appscript.command) then
            rm -f $dir/bin/$appscript.command
        endif
        echo "#\!$csh -f"           >  $dir/bin/$appscript.command
        if($i > 1) then
            echo "$dir/bin/visit -$app" >> $dir/bin/$appscript.command
        else
            echo "$dir/bin/visit"       >> $dir/bin/$appscript.command
        endif
        echo "exit 0"               >> $dir/bin/$appscript.command
        if ($group_ownership != none) then
            chgrp $group_ownership $dir/bin/$appscript.command
        endif
        chmod $dir_permission  $dir/bin/$appscript.command
        @ i++
    end
endif

#
# Set the permissions properly
#
chmod $dir_permission $dir
chmod $dir_permission $dir/bin
chmod $dir_permission $dir/bin/legacylauncher+
chmod $dir_permission $dir/bin/frontendlauncher+
chmod $dir_permission $dir/data
chmod $file_permission $dir/data/*
chmod $dir_permission $dir/$version+

find $dir/$version+ -type d -exec chmod $dir_permission {} \;
find $dir/$version+ -type f -exec chmod $file_permission {} \;

chmod $dir_permission $dir/$version+/*/bin/*
chmod $dir_permission $dir/$version+/bin/*

#
# Set the group ownership properly
#
if ($group_ownership != none) then
    chgrp $group_ownership $dir/bin
    chgrp $group_ownership $dir/bin/legacylauncher+
    chgrp $group_ownership $dir/bin/frontendlauncher+
    chgrp $group_ownership $dir/data
    chgrp $group_ownership $dir/data/*
    find $dir/$version+ -type d -exec chgrp $group_ownership {} \;
    find $dir/$version+ -type f -exec chgrp $group_ownership {} \;
endif

if ($logging == true) then
    sed -e "s/logging[ \t]*= 0/logging        = 1/" distribution/visit/$version/bin/internallauncher > $dir/$version+/bin/internallauncher
    touch $dir/$version+/usagelog
    chmod 666 $dir/$version+/usagelog
    if ($group_ownership != none) then
        chgrp $group_ownership $dir/$version/usagelog
    endif
endif

#
# Clean up the temporary directories.
#
rm -rf distribution

#
# If not in append mode then backup the old version.
#
if ($append == false) then
   #
   # Copy any current files to the backup filenames
   #
   if (-e $dir/bin/legacylauncher) mv $dir/bin/legacylauncher $dir/bin/legacylauncher-
   if (-e $dir/bin/frontendlauncher) mv $dir/bin/frontendlauncher $dir/bin/frontendlauncher-
   if ($beta == true) then
      if (-e $dir/beta)          mv $dir/beta      $dir/beta-
   else if ($private == false) then
      if (-e $dir/current)       mv $dir/current   $dir/current-
   endif
   if (-e $dir/$version)         mv $dir/$version  $dir/$version-
endif

#
# Make the symbolic links for the programs
#
set programs = "convert curv3dprep makemili mpeg_encode silex surfcomp text2polys time_annotation visit xml2atts xml2avt xml2info xml2java xml2makefile xml2projectfile xml2python xml2window xmledit xmltest"

cd $dir/bin
foreach prog ($programs)
    rm -f $prog
    ln -s frontendlauncher $prog
end

#
# Make the symbolic links for the version
#
cd $dir

#
# If $version exists at this point it is a link and must be removed
# with the rm command.
#
if (-e $version)                 rm -f $version
mv $version+ $version
if (-e bin/legacylauncher)       rm -f bin/legacylauncher
mv bin/legacylauncher+ bin/legacylauncher
if (-e bin/frontendlauncher)     rm -f bin/frontendlauncher
mv bin/frontendlauncher+ bin/frontendlauncher
if ($beta == true) then
   if (-e beta)                  rm -f beta
   ln -s $version beta
else if ($private == false) then
   if (-e current)               rm -f current
   ln -s $version current
endif
