#!/bin/csh -f
###############################################################################
#                                                                             #
#   Copyright (c) 1989-1991 Artificial Horizons, Inc.  All rights reserved.   #
#                                                                             #
#      This material  constitutes  the trade secrets  and confidential,       #
#      proprietary information of Bruce Factor and Curtis Priem, and is       #
#      under license to Artificial Horizons, Inc.   The material is not       #
#      to be disclosed,  reproduced,  copied or  used in any manner not       #
#      permitted under license from Artificial Horizons, Inc.  For more       #
#      information email "ahi-interest@ahi.com".                              #
#                                                                             #
###############################################################################
#
# This script installs Aviator from cdrom, tape, network, or after ftp.
# User must have at least 20+ Mbytes available disk space.  Installing
# after ftp takes a lot of disk space (50+ Mbytes).
# User must be cd to the directory containing install_aviator file
# before executing it.
#
set XVERSION = "aviator.1.5.1"
set XVERSBIG = `echo ${XVERSION} | tr a-z A-Z`
#
#
# check usage
#
set XSOURCE = ${PWD}
if (($0 != "install_aviator"  &&  $0 != "./install_aviator")  ||\
    !(-e install_aviator))  then
  echo ""
  echo "Error:  Can't find install_aviator file"
  echo "        User must cd (change directory) to the directory containing"
  echo "        the install_aviator file before executing it."
  goto label_usage
endif
#
#
# get device
#
if ($1 == "")  then
  echo ""
  echo "Error:  device was not specified."
  goto label_usage
endif
set XDEVICE = ""
if (${XDEVICE} == "")  then
  if ($1 == "CDROM")       set XDEVICE = "CDROM"
  if (${XDEVICE} != "")    set XTYPE   = "cdrom"
endif
if (${XDEVICE} == "")  then
  if ($1 == "/dev/rst0")   set XDEVICE = "rst0"
  if ($1 == "/dev/rst1")   set XDEVICE = "rst1"
  if ($1 == "/dev/rst2")   set XDEVICE = "rst2"
  if ($1 == "/dev/rst3")   set XDEVICE = "rst3"
  if ($1 == "/dev/rst8")   set XDEVICE = "rst8"
  if ($1 == "/dev/rst9")   set XDEVICE = "rst9"
  if ($1 == "/dev/rst10")  set XDEVICE = "rst10"
  if ($1 == "/dev/rst11")  set XDEVICE = "rst11"
  if ($1 == "/dev/rst16")  set XDEVICE = "rst16"
  if ($1 == "/dev/rst17")  set XDEVICE = "rst17"
  if ($1 == "/dev/rst18")  set XDEVICE = "rst18"
  if ($1 == "/dev/rst19")  set XDEVICE = "rst19"
  if ($1 == "/dev/rst24")  set XDEVICE = "rst24"
  if ($1 == "/dev/rst25")  set XDEVICE = "rst25"
  if ($1 == "/dev/rst26")  set XDEVICE = "rst26"
  if ($1 == "/dev/rst27")  set XDEVICE = "rst27"
  if (${XDEVICE} != "")    set XTYPE   = "tape"
endif
if (${XDEVICE} == "")  then
  if ($1 == "NETWORK")     set XDEVICE = "NETWORK"
  if (${XDEVICE} != "")    set XTYPE   = "network"
endif
if ($XDEVICE == "")  then
  if ($1 == "BIGFILE")     set XDEVICE = "BIGFILE"
  if (${XDEVICE} != "")    set XTYPE   = "bigfile"
endif
if (${XDEVICE} == "")  then
  echo ""
  echo "Error:  Unknown device: '$1'"
  goto label_usage
endif
#
#
# get destination pathname
#
if ($2 != "")  then
  set XDEST_DIR = $2
else
  if (${XTYPE} == "cdrom"  ||  ${XTYPE} == "network")  then
    echo ""
    echo "Error:  destination directory pathname is required when"
    echo "        installing from cdrom or network."
    goto label_usage
  else
    set XDEST_DIR = "."
  endif
endif
#
#
###############################################################################
######################## create directory structure ###########################
###############################################################################
#
#
# change to destination directory
#
cd $XDEST_DIR
if ($status != 0)  exit(-1)
if ((-e bin)  &&  (-e terrain)  &&  (-e craft)) then
  cd ..
  if ($status != 0)  exit(-1)
endif
if ((-e bin)  &&  (-e terrain)  &&  (-e craft)) then
  cd ..
  if ($status != 0)  exit(-1)
endif
#
#
# create aviator directory
#
set XFILE = "aviator"
echo ""
echo "Creating ${PWD}/${XFILE} directory ..."
if (!(-e ${XFILE})) then
  mkdir ${XFILE}
  if ($status != 0)  then
    echo "User must become root to execute the following command(s):"
    echo "        mkdir ${XFILE}"
    echo "        chown ${USER} ${XFILE}"
    echo "If you don't want this done, type ^C now."
    su -c "mkdir ${XFILE} ; chown ${USER} ${XFILE}"
  endif
else
  if (!(-o ${XFILE})) then
    echo "User must become root to execute the following command(s):"
    echo "        chown ${USER} ${XFILE}"
    echo "If you don't want this done, type ^C now."
    su -c "chown ${USER} ${XFILE}"
  endif
endif
chmod 755 ${XFILE}
if ($status != 0)  exit(-1)
cd ${XFILE}
if ($status != 0)  exit(-1)
#
#
# create /usr/games directory
#
set XFILE = "/usr/games"
if (!(-e ${XFILE})) then
  echo ""
  echo "Creating ${XFILE} directory ..."
  echo "User must become root to execute the following command(s):"
  echo "        mkdir ${XFILE}"
  echo "        chown bin ${XFILE}"
  echo "        chmod 2755 ${XFILE}"
  echo "If you don't want this done, type ^C now."
  su -c "mkdir ${XFILE} ; chown bin ${XFILE} ; chmod 2755 ${XFILE}"
endif
#
#
# create /usr/games/aviator link
#
set XFILE = "/usr/games/aviator"
if (${PWD} != ${XFILE}) then
  echo ""
  echo "Creating ${XFILE} link ..."
  set XSTATUS0 = 0
  set XSTATUS1 = 0
  set XSTATUS2 = 0
  if (-e ${XFILE}) then
    rm -r ${XFILE}
    set XSTATUS0 = $status
  endif
  if (${XSTATUS0} == 0) then
    ln -s ${PWD} ${XFILE}
    set XSTATUS1 = $status
  endif
  if ((${XSTATUS0} == 0)  &&  (${XSTATUS1} == 0)) then
    chown ${USER} ${XFILE}
    set XSTATUS2 = $status
  endif
  if ((${XSTATUS0} != 0)  ||  (${XSTATUS1} != 0)  ||  (${XSTATUS2} != 0)) then
    echo "User must become root to execute the following command(s):"
    echo "        rm -r ${XFILE}"
    echo "        ln -s ${PWD} ${XFILE}"
    echo "        chown ${USER} ${XFILE}"
    echo "If you do not want this to be done, just press return."
    echo -n "Enter your root Password: "
    su -c "rm -r ${XFILE} ; ln -s ${PWD} ${XFILE} ; chown ${USER} ${XFILE}" >& /dev/null
    if ($status != 0) then
      echo ""
      echo "An incorrect password or no password was typed."
      echo "After installing aviator you will need to type the following:"
      echo "        setenv AVIATOR_HOME ${PWD}"
      echo "For more information consult ${PWD}/man/aviator.6"
      echo "or Chapter 2 in the 'Aviator Users Manual' for setting the"
      echo "appropriate environment variable(s)."
    else
      echo ""
    endif
  endif
endif
#
#
# remove old ${XVERSION} directory
#
foreach XFILE (aviator.1.5 ${XVERSION})
  if (-e ${XFILE}) then
    echo ""
    echo "Deleting ${PWD}/${XFILE} directory ..."
    rm -r ${XFILE}
    if ($status != 0) then
      echo "User must become root to execute the following command(s):"
      echo "        rm -r ${XFILE}"
      echo "If you don't want this done, type ^C now."
      su -c "rm -r ${XFILE}"
    endif
  endif
end
#
#
# change mode /dev/nit
#
set XFILE = "/dev/nit"
echo ""
echo "Changing mode of ${XFILE} ..."
if (-o ${XFILE})  then
  chmod 666 ${XFILE}
else if (!(-r ${XFILE})  ||  !(-w ${XFILE}))  then
  echo "User must become root to execute the following command(s):"
  echo "        chmod 666 ${XFILE}"
  echo "If you do not enter your root password correctly you will not be"
  echo "capable of dogfighting other players on your network."
  echo "If you do not want this to be done, just press return."
  echo -n "Enter your root Password: "
  su -c "chmod 666 ${XFILE}" >& /dev/null
  if ($status != 0) then
    echo ""
    echo "An incorrect password or no password was typed."
    echo "To enable dogfighting in the future, type the following:"
    echo "        "su -c \"chmod 666 ${XFILE}\"
  else
    echo ""
  endif
endif
#
#
###############################################################################
########################### installing from tape ##############################
###############################################################################
if (${XTYPE} == "tape")  then
#
#
# skip over install file
#
echo ""
echo "Rewinding tape ..."
mt -f /dev/${XDEVICE} rewind
if ($status != 0)  exit(-1)
mt -f /dev/n${XDEVICE} fsf 1
if ($status != 0)  exit(-1)
#
#
# extract, uncompress, and sparse terrain data files
#
echo ""
echo "Extracting, uncompressing, and sparsing 17 terrain files ..."
foreach XFILE (022_130 023_130 023_131 024_131 025_130 026_129\
               026_130 027_129 027_130 028_127 028_128 028_129\
               029_128 067_149 067_150 068_149 068_150)
  bar xvf /dev/n${XDEVICE}
  if ($status != 0)  exit(-1)
  mt -f /dev/n${XDEVICE} fsf 1
  if ($status != 0)  exit(-1)
  if (-e ${XVERSION}/terrain/${XFILE}.Z)  then
    echo "uncompressing ${XVERSION}/terrain/${XFILE}.Z ..."
    uncompress ${XVERSION}/terrain/${XFILE}
    if ($status != 0)  exit(-1)
  endif
  echo "making ${XVERSION}/terrain/${XFILE} sparse ..."
  ${XSOURCE}/atl_mksparse ${XVERSION}/terrain/${XFILE}
  if ($status != 0)  exit(-1)
end
#
#
# extract files
#
echo ""
echo "Extracting files ..."
bar xvf /dev/n${XDEVICE}
if ($status != 0)  exit(-1)
mt -f /dev/n${XDEVICE} fsf 1
if ($status != 0)  exit(-1)
#
#
# clean up
#
echo ""
echo "Rewinding tape ..."
mt -f /dev/${XDEVICE} rewind
if ($status != 0)  exit(-1)
rm ${XSOURCE}/atl_mksparse
if ($status != 0)  exit(-1)
if (!(-e ${XSOURCE}/../etc)  ||  !(-e ${XSOURCE}/create_aviator)) then
  rm ${XSOURCE}/install_aviator
  if ($status != 0)  exit(-1)
endif
#
#
endif
###############################################################################
################ installing from mounted device or filesystem #################
###############################################################################
if (${XTYPE} == "cdrom"  ||  ${XTYPE} == "network")  then
#
#
# copying files
#
echo ""
echo "Copying files ..."
mkdir ${XVERSION}
if ($status != 0)  exit(-1)
cp -r ${XSOURCE}/../[R-s]* ${XVERSION}
if ($status != 0)  exit(-1)
#
#
# copy and sparse terrain data files
#
echo ""
echo "Copying and sparsing 17 terrain files ..."
mkdir ${XVERSION}/terrain
foreach XFILE (022_130 023_130 023_131 024_131 025_130 026_129\
               026_130 027_129 027_130 028_127 028_128 028_129\
               029_128 067_149 067_150 068_149 068_150)
  echo "copying ${XVERSION}/terrain/${XFILE} ..."
  cp ${XSOURCE}/../terrain/${XFILE} ${XVERSION}/terrain
  if ($status != 0)  exit(-1)
  echo "making  ${XVERSION}/terrain/${XFILE} sparse ..."
  ${XVERSION}/atl/bin/atl_mksparse ${XVERSION}/terrain/${XFILE}
  if ($status != 0)  exit(-1)
end
cp ${XSOURCE}/../terrain/US_MAP ${XVERSION}/terrain
#
#
endif
###############################################################################
######################## installing bigfile after ftp #########################
###############################################################################
if (${XTYPE} == "bigfile")  then
#
#
# find bigfile
#
if (!(-e ${XSOURCE}/${XVERSBIG})) then
  echo ""
  echo "Error:  Can't find ${XSOURCE}/${XVERSBIG} file."
  goto label_usage
endif
#
#
# extracting files
#
echo ""
echo "Extracting files ..."
bar xvf - < ${XSOURCE}/${XVERSBIG}
if ($status != 0)  exit(-1)
#
#
# uncompress and sparse terrain data files
#
echo ""
echo "Uncompressing and sparsing 17 terrain files ..."
foreach XFILE (022_130 023_130 023_131 024_131 025_130 026_129\
               026_130 027_129 027_130 028_127 028_128 028_129\
               029_128 067_149 067_150 068_149 068_150)
  if (-e ${XVERSION}/terrain/${XFILE}.Z)  then
    echo "uncompressing ${XVERSION}/terrain/${XFILE}.Z ..."
    uncompress ${XVERSION}/terrain/${XFILE}
    if ($status != 0)  exit(-1)
  endif
  echo "making ${XVERSION}/terrain/${XFILE} sparse ..."
  ${XVERSION}/atl/bin/atl_mksparse ${XVERSION}/terrain/${XFILE}
  if ($status != 0)  exit(-1)
end
#
#
endif
###############################################################################
############################ final housekeeping ###############################
###############################################################################
#
#
# install links
#
echo ""
echo "Creating links ..."
foreach XFILE (README atl bin craft etc man terrain)
  rm -rf ${XFILE}
  ln -s ${XVERSION}/${XFILE}
  if ($status != 0) then
    echo "Can't create aviator/${XFILE} link"
    exit(-1)
  endif
end
#
#
# create passwords file
#
set XFILE = "passwords"
if (!(-e ${XFILE})) then
  echo ""
  echo "Creating aviator/${XFILE} file ..."
  echo "#"                                            >  ${XFILE}
  echo "#           Aviator Passwords File          " >> ${XFILE}
  echo "#"                                            >> ${XFILE}
  echo "# WARNING: Do not delete, copy, or move this" >> ${XFILE}
  echo "# file.   If you do,  your passwords will no" >> ${XFILE}
  echo "# longer work  and you will have to purchase" >> ${XFILE}
  echo "# new ones."                                  >> ${XFILE}
  echo "#"                                            >> ${XFILE}
  echo "# See man/passwords.5 for more information."  >> ${XFILE}
  echo "# Place passwords at end of this file."       >> ${XFILE}
  echo "#"                                            >> ${XFILE}
endif
#
#
# change files' modes
#
echo ""
echo "Changed modes on files ..."
chmod 555 ../aviator
chmod 644 ./README
chmod 644 ./passwords
chmod 755 ${XVERSION}/
chmod 755 ${XVERSION}/atl/
chmod 755 ${XVERSION}/atl/bin/
chmod 755 ${XVERSION}/atl/bin/*
chmod 755 ${XVERSION}/atl/examples/
chmod 644 ${XVERSION}/atl/examples/*
chmod 755 ${XVERSION}/atl/h/
chmod 644 ${XVERSION}/atl/h/*
chmod 755 ${XVERSION}/atl/lib/
chmod 644 ${XVERSION}/atl/lib/*
chmod 755 ${XVERSION}/atl/man/
chmod 644 ${XVERSION}/atl/man/*
chmod 755 ${XVERSION}/bin/
chmod 755 ${XVERSION}/bin/*
chmod 755 ${XVERSION}/craft/
chmod 644 ${XVERSION}/craft/*
chmod 666 ${XVERSION}/craft/craftcap.o
chmod 755 ${XVERSION}/man/
chmod 644 ${XVERSION}/man/*
chmod 755 ${XVERSION}/etc/
chmod 755 ${XVERSION}/etc/*
chmod 755 ${XVERSION}/terrain/
chmod 644 ${XVERSION}/terrain/*
#
#
echo ""
echo "Installation complete"
exit(0)
#
#
###############################################################################
############################### Usage message #################################
###############################################################################
#
label_usage:
echo ""
echo "Usage:  install_aviator device [dest_dir]"
echo "        device   = CDROM | /dev/rst* | NETWORK | BIGFILE"
echo "        dest_dir = directory pathname (required for cdrom and network)"
echo ""
echo "        Installing from the cdrom (/dev/sr*), example:"
echo "            mnt /dev/sr0 /mnt"
echo "            cd /mnt/${XVERSION}/etc"
echo "            install_aviator CDROM /home/bigdisk"
echo ""
echo "        Installing from tape (/dev/rst*), example:"
echo "            tar xvf /dev/rst8"
echo "            install_aviator /dev/rst8"
echo ""
echo "        Installing from the network (automounter), example:"
echo "            cd /net/machine/usr/games/aviator/${XVERSION}/etc"
echo "            install_aviator NETWORK /home/bigdisk"
echo ""
echo "        Installing from the network (mount), examples:"
echo "            mount machine:/usr/games/aviator /mnt"
echo "            cd /mnt/${XVERSION}/etc"
echo "            install_aviator NETWORK /home/bigdisk"
echo ""
echo "        Installing after ftp, example:"
echo "            install_aviator BIGFILE"
echo ""
exit(-1)
