#!/bin/csh -f 
#
#******************************************************************************
# $Source: /hell/dev/RCS/environment/INSTALL,v $
#
# Flame & Flint installation script.
#
# Last Modification: $Revision: 1.18 $
#                    $Date: 1996/04/08 20:45:07 $
#
# Copyright (c) 1994 Discreet Logic, Inc.
#******************************************************************************

# explicitly set the path to the default for root in case user uses su rather
# than login or su -
set path = ( /usr/sbin /usr/bsd /sbin /usr/bin /bin /etc /usr/etc /usr/bin/X11 )
rehash

#******************************************************************************
# Setup continuously changing vars.
#******************************************************************************

if ( { ( /sbin/hinv -c graphics | /usr/bin/egrep -s 'RealityEngine|VGX' ) } ) then

  set PAINT = "fire"

else

  set PAINT = "spark"

endif


set resourceDirs = ( \
    menu \
    filter \
    lut \
    ${PAINT}/brush \
    ${PAINT}/palette \
    ${PAINT}/set \
    )

set setupDirs = ( \
   action \
   audio \
   batch \
   composite \
   correct \
   crossfade \
   difference \
   dissolve \
   dve \
   edl \
   engineering \
   images \
   key \
   mix \
   morf \
   stabilizer \
   text \
   timewarp \
   ${PAINT}/autopaint \
   ${PAINT}/cutout \
   ${PAINT}/geometry \
   ${PAINT}/mask \
   ${PAINT}/medium \
   ${PAINT}/paper \
   ${PAINT}/picture \
   ${PAINT}/sequence \
 )


#******************************************************************************
# Define some useful aliases.
#******************************************************************************

set CONFIRM = "/usr/bin/X11/xconfirm -geometry 700x150 -c -noframe"

alias QUESTION '$CONFIRM -icon question -header "$header" -t "$msg" -t "$msg2" -t "$msg4" -t "$msg5"'
alias INFORM   '$CONFIRM -icon info     -header "$header" -t "$msg" -t "$msg3" -t "$msg4"'

set msg2 = ""
set msg3 = ""
set msg4 = ""
set msg5 = ""

#******************************************************************************
# Setup the script variables.
#******************************************************************************

set VERSION_FILE = `ls -1d *_VERSION`
if (!($#VERSION_FILE)) then

  set header = "Product Version File"
  set msg = "Could not find Product Version File, Install aborted"
  
  INFORM -B "OK" >& /dev/null

  exit 1 

endif

set SOFTWARE = `ls *_VERSION | nawk -F_ '{print $1}' | nawk '{print tolower($1)}'`
set PRODUCT  = `echo $SOFTWARE | nawk '{printf "%s%s",toupper(substr($1,1,1)),substr($1,2)}'`
set VERSION  = `cat *_VERSION | nawk '{gsub("\"","",$3); print $3}'`
set LOGIN    = `echo $VERSION | tr . _ | sed 's/alpha/a/' | sed 's/beta/b/' | nawk -v user="$SOFTWARE" '{printf "%s%s",user,$1}'`
set SWR_INSTALLED = 0
set REBOOT_REQUIRED = "No"

#******************************************************************************
# Am I running as root?
#******************************************************************************

if ( `whoami` != "root" ) then

   set header = "Privileges"
   set msg    = "You must have root privileges to install ${PRODUCT}."

   INFORM -B "OK" > /dev/null

   exit 1

endif

#**************************************************************************
# Am I running the right version of IRIX
#**************************************************************************

set IRIX_RELEASE = `uname -r`

set exes = `ls -1d ./bin/${SOFTWARE}_IRIX_* | /sbin/grep ${IRIX_RELEASE}`

#if ( ! $#exes ) then

#   set header = "IRIX Release"
#   set msg    =  "Your workstation must be driven by IRIX ${IRIX_RELEASE} to run ${PRODUCT}."

#   INFORM -B "OK" >& /dev/null

#   exit 1

#endif

echo "####################################################################"
echo "#                                                                  #"
echo "#             Discreet Logic Software Installation                 #"
echo "#                                                                  #"
echo "#                                                                  #"
echo "####################################################################"

set OLD_ACCOUNT = ()
set ACCOUNT = "/usr/${SOFTWARE}_${VERSION}"

#******************************************************************************
# Check if install type is CDROM.
#******************************************************************************
set CURRENT_DIR = `pwd`
if ( ${CURRENT_DIR} == "/CDROM" ) then

    set CDROM_DIR = "/CDROM"

    #**************************************************************************
    # Copy media.
    #**************************************************************************

    echo ""
    echo "Beginning to copy ${PRODUCT} media to ${ACCOUNT}"
    echo -n "This will take about one minute, please wait..."

    cp -r ${CDROM_DIR} ${ACCOUNT}

    echo "done"

endif

#*****************************************************************************
#  Copy the libio shared object and tablet driver config files.
#*****************************************************************************
if ( (-d /usr/lib/discreet) == 0 ) then

    mkdir /usr/lib/discreet
    chown root.sys /usr/lib/discreet
    chmod 755 /usr/lib/discreet

endif

ln -sf ${ACCOUNT}/io/libio.so /usr/lib/discreet/libio.so

if ( (-d /usr/lib/X11/input/config) == 0 ) then

    mkdir /usr/lib/X11/input/config
    chown root.sys /usr/lib/X11/input/config
    chmod 755 /usr/lib/X11/input/config
   
endif

cp ${ACCOUNT}/bin/dlwacom /usr/lib/X11/input/config/dlwacom


#****************************************************************************
# Locate the currently used version of the product if one exists.
#****************************************************************************

set dirs = `ls -1d /usr/${SOFTWARE}_* | /sbin/grep -v ^/usr/${SOFTWARE}_${VERSION}\$`

if ( $#dirs ) then

   set oldVersionNbs = "-B None"

   foreach dir ( $dirs )

      set oldVersionNbs = "-b `echo ${dir:t} | cut -d_ -f2` ${oldVersionNbs}"

   end

   set header = "${PRODUCT} Version"
   set msg    = "Please select the currently used version of ${PRODUCT}."

   set OLD_ACCOUNT = "/usr/${SOFTWARE}_`QUESTION ${oldVersionNbs}`"

   if ( ${OLD_ACCOUNT} == "/usr/${SOFTWARE}_None" ) set OLD_ACCOUNT = ()
   if ( ${OLD_ACCOUNT} == "/usr/${SOFTWARE}_"     ) set OLD_ACCOUNT = ()

endif

#******************************************************************************
# Create the product user account.
#******************************************************************************

if ( ! { /usr/bin/egrep -s "^${LOGIN}:" /etc/passwd } ) then

   passmgmt -a -g20 -c"${PRODUCT} ${VERSION}" -h${ACCOUNT} -s/bin/csh ${LOGIN}

   passwd -d ${LOGIN}

endif

#******************************************************************************
# Change the ownership of the release directory files.
#******************************************************************************

chown -R ${LOGIN}.user ${ACCOUNT}

#chown -R root ${ACCOUNT}/bin
chown root ${ACCOUNT}/bin/*_IRIX_${IRIX_RELEASE}
chown root ${ACCOUNT}/bin/*_framestore

#chown -R root ${ACCOUNT}/cfg

if ( -e ${ACCOUNT}/clip ) chown root ${ACCOUNT}/clip

chown -R root ${ACCOUNT}/menu
chown -R root ${ACCOUNT}/filter
chown -R root ${ACCOUNT}/lut

chown    root ${ACCOUNT}/${PAINT}
chown -R root ${ACCOUNT}/${PAINT}/brush
chown -R root ${ACCOUNT}/${PAINT}/geometry
chown -R root ${ACCOUNT}/${PAINT}/palette
chown -R root ${ACCOUNT}/${PAINT}/set

#chown -R root ${ACCOUNT}/io
#chown -R root ${ACCOUNT}/sparks

#*******************************************************************************
# Change the access permissions of the release directory files.
#*******************************************************************************

find ${ACCOUNT} -type d -exec chmod 1750 {} \;
find ${ACCOUNT} -type f -exec chmod  640 {} \;

chmod 750 ${ACCOUNT}/INSTALL

chmod 4750 ${ACCOUNT}/bin/*_IRIX_${IRIX_RELEASE}
chmod 4750 ${ACCOUNT}/bin/*_framestore

chmod 1770 ${ACCOUNT}/cfg
chmod 0660 ${ACCOUNT}/cfg/*.cfg

if ( -e ${ACCOUNT}/clip ) chmod 1770 ${ACCOUNT}/clip

chmod 1770 ${ACCOUNT}/menu
chmod 1770 ${ACCOUNT}/filter
chmod 1770 ${ACCOUNT}/lut

chmod 1770 ${ACCOUNT}/${PAINT}/brush
chmod 1770 ${ACCOUNT}/${PAINT}/geometry
chmod 1770 ${ACCOUNT}/${PAINT}/palette
chmod 1770 ${ACCOUNT}/${PAINT}/set

chmod 1770 ${ACCOUNT}/io
chmod  750 ${ACCOUNT}/io/*
chmod 0660 ${ACCOUNT}/io/*.c
chmod 0660 ${ACCOUNT}/io/*.h
chmod 0660 ${ACCOUNT}/io/Make*

chmod 1770 ${ACCOUNT}/sparks

if ( ${SOFTWARE} == "flame" ) then
   
  chmod 777 ${ACCOUNT}/Stone_+_Wire/INSTALL

endif


#******************************************************************************
# Is it a first-time installation?
#******************************************************************************

if ( ! ${#OLD_ACCOUNT} ) then

   #***************************************************************************
   # Create the licence file if required.
   #***************************************************************************

   if ( ! -e ${ACCOUNT}/bin/.sysfile ) then

      set header = "Software Licence"
      set msg    = "Have you received a software licence from Discreet Logic?"

      if ( `QUESTION -B "No" -b "Yes"` == "Yes" ) then

         set header = "File '.sysfile'"
         set msg    = "Get ready to type in the software licence number."

         INFORM -B "OK" > /dev/null

         jot -f ${ACCOUNT}/bin/.sysfile

         if ( -e ${ACCOUNT}/bin/.sysfile ) then

            chown root.user ${ACCOUNT}/bin/.sysfile
            chmod 640       ${ACCOUNT}/bin/.sysfile

         endif

      endif

   endif

   if ( ${SOFTWARE} == "flame" ) then
   
      set header = "Stone & Wire"
      set msg    = "Do you have installed or will you be installing a Stone & Wire Array?"

      if ( `QUESTION -B "No" -b "Yes"` == "No" ) then

	 set SWR_INSTALLED = 0

      else 

	 set SWR_INSTALLED = 1

      endif
      
   endif
   
   #***************************************************************************
   # Create the logical volume table if required.
   #***************************************************************************

   if ( $SWR_INSTALLED == 0 ) then

       set header = "Logical Volume Table"
       set msg    = "Do you need to create a logical volume table?"

       if ( `QUESTION -B "No" -b "Yes"` == "Yes" ) then

          set header = "File '/etc/lvtab'"
          set msg    = "Get ready to type in the logical volume table."

          INFORM -B "OK" > /dev/null

          jot -f /etc/lvtab

          chown root.sys /etc/lvtab
          chmod 644      /etc/lvtab

          while ( ! { mklv -f lv0 } )

             set header = "Logical Volume Table"
             set msg    = "'mklv' cannot create the logical volume 'lv0'."

             INFORM -B "OK" > /dev/null

             set header = "File '/etc/lvtab'"
             set msg    = "Get ready to update the logical volume table."

             INFORM -B "OK" > /dev/null

             jot -f /etc/lvtab

          end

       endif

   endif

   #***************************************************************************
   # Create the default configuration file if required.
   #***************************************************************************

   if ( ! -e ${ACCOUNT}/cfg/default.cfg ) then

      set header = "Default Configuration File"
      set msg    = "Please select your preferred frame resolution."

      switch ( `QUESTION -b "film" -b "PAL" -B "NTSC"` )
         case NTSC:
            cp ${ACCOUNT}/cfg/sample_ntsc.cfg ${ACCOUNT}/cfg/default.cfg
            breaksw
         case PAL:
            cp ${ACCOUNT}/cfg/sample_pal.cfg ${ACCOUNT}/cfg/default.cfg
            breaksw
         case film:
            cp ${ACCOUNT}/cfg/sample_film.cfg ${ACCOUNT}/cfg/default.cfg
            breaksw
      endsw

   endif

   #***************************************************************************
   # Customize the default configuration file.
   #***************************************************************************

   set header = "File 'default.cfg'"
   set msg    = "Get ready to update the default configuration file."
   set msg3   = "IMPORTANT: There are a number of changes to the config file for 4.0."
   set msg4   = "Please refer to the release notes for more information."

   INFORM -B "OK" > /dev/null

   jot -f ${ACCOUNT}/cfg/default.cfg

   if ( -e ${ACCOUNT}/cfg/default.cfg ) then

      chown ${LOGIN}.user ${ACCOUNT}/cfg/default.cfg
      chmod 640           ${ACCOUNT}/cfg/default.cfg

   endif

   set msg3   = ""
   set msg4   = ""

   #***************************************************************************
   # Connect the dongle if required.
   #***************************************************************************

   if ( ${SOFTWARE} != "flint" ) then

      set header = "Security Dongle"
      set msg    = "Please connect the dongle to the selected serial port."

      INFORM -B "OK" > /dev/null

   else if ( ! -e ${ACCOUNT}/bin/.sysfile ) then

      set header = "Demo Dongle"
      set msg    = "Please connect the dongle to the selected serial port."

      INFORM -B "OK" > /dev/null

   endif

   #******************************************************************************
   # Disconnect the diagnostics terminal from serial port 1 if needed.
   #******************************************************************************

   if ( { /usr/bin/egrep -s '^t1:23:respawn:' /etc/inittab } ) then

      set header = "Serial Port 1"
      set msg    = "Do you need to make serial port 1 available for ${PRODUCT}?"

      if ( `QUESTION -b "No" -B "Yes"` == "Yes" ) then

	 sed -e 's/^t1:23:respawn:/t1:23:off:/' /etc/inittab > /etc/inittab.tmp
	 mv -f /etc/inittab.tmp /etc/inittab

	 chown root.sys /etc/inittab
	 chmod 644      /etc/inittab

	 telinit Q

      endif

   endif

   #******************************************************************************
   # Turn off unnecessary IRIX daemons and subsystems if needed.
   #******************************************************************************

   set header = "IRIX Daemons and Subsystems"
   set msg    = "Do you need to turn off unnecessary IRIX daemons and subsystems?"
   set msg2   = "These are the desktop,directoryserver,fontserver,mediad,objectserver, and soundscheme."

   if ( `QUESTION -b "No" -B "Yes"` == "Yes" ) then

      chkconfig desktop         off
      chkconfig directoryserver off
      chkconfig fontserver      off
      chkconfig mediad          off
      chkconfig objectserver    off
      chkconfig soundscheme     off
      set REBOOT_REQUIRED = "Yes"

   endif

   set msg2   = ""

#******************************************************************************
# Is it an updating installation?
#******************************************************************************

else

      if ( `echo ${OLD_ACCOUNT} | cut -d_ -f2 | cut -c1` == "4" ) then

         set BETA_UPDATE = "TRUE"
	 
      
      else
      
         set BETA_UPDATE = "FALSE"
         
      endif
      
      #***************************************************************************
      # Copy the old licence file if one exists.
      #***************************************************************************

      if ( ${BETA_UPDATE} == "TRUE" && -e ${OLD_ACCOUNT}/bin/.sysfile ) then

	 cp ${OLD_ACCOUNT}/bin/.sysfile ${ACCOUNT}/bin

	 chown root.user ${ACCOUNT}/bin/.sysfile
	 chmod 640       ${ACCOUNT}/bin/.sysfile

      endif
      
   #***************************************************************************
   # Create the licence file if required.
   #***************************************************************************

   if ( ! -e ${ACCOUNT}/bin/.sysfile ) then

      set header = "Software Licence"
      set msg    = "Have you received a software licence from Discreet Logic?"

      if ( `QUESTION -B "No" -b "Yes"` == "Yes" ) then

         set header = "File '.sysfile'"
         set msg    = "Get ready to type in the software licence number."

         INFORM -B "OK" > /dev/null

         jot -f ${ACCOUNT}/bin/.sysfile

         if ( -e ${ACCOUNT}/bin/.sysfile ) then

            chown root.user ${ACCOUNT}/bin/.sysfile
            chmod 640       ${ACCOUNT}/bin/.sysfile

         endif

      endif

   endif

   #***************************************************************************
   # Move the old clip directory.
   #***************************************************************************

   if ( ${BETA_UPDATE} == "TRUE" ) then
   
      if ( ! -l ${OLD_ACCOUNT}/clip ) then

	 rmdir ${OLD_ACCOUNT}/clip >& /dev/null
	 rmdir ${ACCOUNT}/clip     >& /dev/null

	 if ( -e ${OLD_ACCOUNT}/clip && ! -e ${ACCOUNT}/clip ) then

            mv ${OLD_ACCOUNT}/clip ${ACCOUNT}/clip

	 endif

	 if ( -e ${ACCOUNT}/clip ) then

            chown -R ${LOGIN}.user ${ACCOUNT}/clip

            find ${ACCOUNT}/clip -type d           -exec chown root {} \;
            find ${ACCOUNT}/clip -type f -name .\* -exec chown root {} \;

            find ${ACCOUNT}/clip -type f -exec chmod 660 {} \;

            find ${ACCOUNT}/clip -type d           -exec chmod 1770 {} \;
            find ${ACCOUNT}/clip -type f -name .\* -exec chmod  640 {} \;

	 endif

	 if ( ! -e ${OLD_ACCOUNT}/clip && -e ${ACCOUNT}/clip ) then

            ( umask 007; ln -s ${ACCOUNT}/clip ${OLD_ACCOUNT}/clip )

            chown -h root.user ${OLD_ACCOUNT}/clip

	 endif

      endif

   endif
   
   #***************************************************************************
   # Copy the custom resource files.
   #***************************************************************************

   set header = "Resource Files"
   set msg    = "Do you want to carry over your custom resource files?"
   set msg2   = "These include custom menus, LUTs, custom filters, custom brushes,"
   set msg3   = "Custom colour palettes, and paint brush setups."

   if ( `QUESTION -b "No" -B "Yes"` == "Yes" ) then

      foreach dir ( ${resourceDirs} )

         @ nbFiles = `ls -1 ${OLD_ACCOUNT}/${dir} | wc -l`

         if ( ${nbFiles} ) then

            pushd ${OLD_ACCOUNT}/${dir}

            foreach oldFile ( * )

               set newFile = "${ACCOUNT}/${dir}/${oldFile}"

               if ( -f "${oldFile}" && ! -e "${newFile}" ) then

                  echo "Copying '${OLD_ACCOUNT}/${dir}/${oldFile}'."

                  cp "${oldFile}" "${newFile}"

                  chown ${LOGIN}.user "${newFile}"
                  chmod 640           "${newFile}"

               endif

            end

            popd

         endif

      end

   endif

   set msg2 = ""
   set msg3 = ""

   #***************************************************************************
   # Copy the setup files.
   #***************************************************************************

   set header = "Setup Files"
   set msg    = "Do you want to carry over your setup files?"

   if ( `QUESTION -b "No" -B "Yes"` == "Yes" ) then

      foreach dir ( ${setupDirs} )

         if ( -e ${OLD_ACCOUNT}/${dir} ) then

            @ nbFiles = `ls -1 ${OLD_ACCOUNT}/${dir} | wc -l`

            if ( ${nbFiles} ) then
            
               echo "Copying '${OLD_ACCOUNT}/${dir}' to '${ACCOUNT}/${dir}'."
               
	       cp ${OLD_ACCOUNT}/${dir}/* ${ACCOUNT}/${dir}
	       
            endif

         endif

      end

   endif


   #************************************************************************
   # Copy the old 4.0 default configuration file if it exists.
   #************************************************************************

   if ( ${BETA_UPDATE} == "TRUE" ) then

      if ( -e ${OLD_ACCOUNT}/cfg/default.cfg ) then

         cp ${OLD_ACCOUNT}/cfg/default.cfg ${ACCOUNT}/cfg/

      endif

   endif

   #***************************************************************************
   # Create the 4.0 default configuration file if required.
   #***************************************************************************

   if ( ! -e ${ACCOUNT}/cfg/default.cfg ) then

      #************************************************************************
      # Create the default configuration file if required.
      #************************************************************************

      set header = "Default Configuration File"
      set msg    = "Please select your preferred frame resolution."

      switch ( `QUESTION -b "film" -b "PAL" -B "NTSC"` )
         case NTSC:
            cp ${ACCOUNT}/cfg/sample_ntsc.cfg ${ACCOUNT}/cfg/default.cfg
            breaksw
         case PAL:
            cp ${ACCOUNT}/cfg/sample_pal.cfg ${ACCOUNT}/cfg/default.cfg
            breaksw
         case film:
            cp ${ACCOUNT}/cfg/sample_film.cfg ${ACCOUNT}/cfg/default.cfg
            breaksw
      endsw

   endif


   #***************************************************************************
   # Update the default configuration file.
   #***************************************************************************

   set header = "File 'default.cfg'"
   set msg    = "Get ready to update the default configuration file."
   set msg3   = "IMPORTANT: There are a number of changes to the config file for 4.0."
   set msg4   = "Please refer to the release notes for more information."

   INFORM -B "OK" > /dev/null

   set msg3   = ""
   set msg4   = ""

   jot -f ${ACCOUNT}/cfg/default.cfg

   if ( -e ${ACCOUNT}/cfg/default.cfg ) then

      chown ${LOGIN}.user ${ACCOUNT}/cfg/default.cfg
      chmod 640           ${ACCOUNT}/cfg/default.cfg

   endif

endif

#******************************************************************************
# Check dlwacom driver registration for possible update.
#******************************************************************************

set DLWACOM_ID = `ml | /sbin/grep dlwacom | cut -d: -f2 | cut -c1-5`

if ( (`/sbin/ml unreg $DLWACOM_ID 2>& /dev/null`) == 0 ) then 
   set REBOOT_REQUIRED = "Yes"
endif



#******************************************************************************
# Installation completed.
#******************************************************************************

set header = "INSTALL"
set msg    = "${PRODUCT} installation completed."

INFORM -B "OK" > /dev/null

#***************************************************************************
# Reboot the workstation.
#***************************************************************************

if ( ${REBOOT_REQUIRED} == "Yes" ) then

   set header = "Restart IRIX"
   set msg    = "You must reboot for changes to the tablet driver, IRIX Daemons and Subsystems to take effect."
   set msg2   = "However, if you are going to install the Stone & Wire driver you may wait until that is done."
   set msg5   = "DO you want to reboot the workstation now?"

   if ( `QUESTION -b "No" -B "Yes"` == "Yes" ) then
   
      reboot
   
   endif

endif

exit 0
