#!/bin/ksh

# ****************************
# *                          *
# *  SIGMET Post OS Install  *
# *         Script           *
# *                          *
# ****************************
# File: utils/install/sigconfig

# This script performs all of the post install configuration and
# actual software installation steps necessary for an IRIS or RDA
# system.  These are all NEW installations and will overwrite
# /usr/sigmet/config.

# Must specify exactly one of the following operations:
#      -rvp8       : Configure RVP8 system and start RVP8 services
#      -rcp8       : Configure RCP8 system and start RCP8 services
#       -rda       : Configure generic RDA system without initial services
#      -iris       : Configure IRIS system with autostart
#       -rcw       : Configure combined RDA/IRIS software
#   -instdir <DIR> : Set directory holding RDA/IRIS files

# ==============================
# Process argument list
#
HELP="" ; OPER="" ; RVP8="" ; INSTDIR="/mnt/cdrom/sigmet"

while [ "$1" != "" ] ; do
  OPT="$1" ; shift
  case ${OPT} in
  -rvp8 | -rcp8 | -rda | -iris | -rcw )
    if [ "${OPER}" != "" ] ; then
      echo "Can not specify more than one option" 1>&2 ; exit 1
    else
      OPER="${OPT#-}"
    fi ;;

  -help | -? )
    HELP="true" ;;

  -instdir )
    if [ "${1}" = "" ] ; then
      echo "Missing directory name for -instdir" 1>&2 ; exit 1
    else
      INSTDIR="${1}" ; shift
    fi ;;

  * )
    echo "Unknown option: '${OPT}'" 1>&2 ; exit 1 ;;
 esac
done

# Print HELP messages if these were requested, either directly or
# indirectly.
#
if [ "${HELP}" = "true" ] ; then
  echo "Command Line Options:"
  echo "        -rvp8    : Configure RVP8 system and start RVP8 services"
  echo "        -rcp8    : Configure RCP8 system and start RCP8 services"
  echo "         -rda    : Configure generic RDA system without initial services"
  echo "        -iris    : Configure IRIS system with autostart"
  echo "         -rcw    : Configure combined RDA/IRIS software"
  echo "  -instdir <DIR> : Set directory holding RDA/IRIS files"
  exit 0
fi

# Verify that there is something to do, and that our release
# directories are reasonable.
#
if [ "${OPER}" = "" ] ; then
  echo "You have not chosen any operation to perform" 1>&2 ; exit 1
fi

IRIS_INSTDIR="${INSTDIR}/iris/RHEL3.0" ; HAS_IRIS=""
 RDA_INSTDIR="${INSTDIR}/rda/RHEL3.0"  ; HAS_RDA=""

if [ "${OPER}" = "rcw" -o "${OPER}" = "iris" ] ; then
  HAS_IRIS="true"
  if [ ! -d ${IRIS_INSTDIR} ] ; then
    echo "Bad IRIS install directory: ${IRIS_INSTDIR}" 1>&2 ; exit 1
  fi
fi

if [ "${OPER}" = "rcw"  -o "${OPER}" = "rcp8" -o \
     "${OPER}" = "rvp8" -o "${OPER}" = "rda"  ] ; then
  HAS_RDA="true"
  if [ ! -d ${RDA_INSTDIR} ] ; then
    echo "Bad RDA install directory: ${RDA_INSTDIR}" 1>&2 ; exit 1
  fi
fi

echo "Configuring system, users and services..."

# Delete the existing operator account, and then add it back with
# appropriate settings.
#
/usr/sbin/userdel operator
/usr/sbin/useradd -u1000 -g100 -s/bin/ksh -n operator
chgrp users /home/operator
echo 'xxxxxx' | passwd --stdin operator
/usr/bin/chfn -f '' operator

# Add the observer account with the appropriate settings
#
/usr/sbin/useradd -u1001 -g100 -s/bin/ksh -n observer
chgrp users /home/observer
echo 'xxxxxx' | passwd --stdin observer
/usr/bin/chfn -f '' observer

echo "Cleaning up default services..."

# Turn some services on that default to off
# The following are located at /etc/xinetd.d 
#
/sbin/chkconfig    rsh on
/sbin/chkconfig rlogin on
/sbin/chkconfig telnet on
# The following are located at /etc/init.d 
#
/sbin/chkconfig vsftpd on
service vsftpd start


# Disable unneeded services
#
/sbin/chkconfig --del sendmail

# Fix language issues, root prompt, and fstab for miniroot
#
LANGFIX="/etc/sysconfig/i18n"
if [ -r ${LANGFIX} ] ; then mv ${LANGFIX} ${LANGFIX}.bak ; fi
echo 'LANG="en_US"
SUPPORTED="en_US.UTF-8:en_US:en"
SYSFONT="latarcyrheb-sun16"' > ${LANGFIX}


# Fix per_source for rsh and root prompt.
#
mv /etc/xinetd.d/rsh /etc/xinetd.d/rsh.orig
sed '/^ *}/,$d' < /etc/xinetd.d/rsh.orig   > /etc/xinetd.d/rsh
echo "	per_source		= 100\n}" >> /etc/xinetd.d/rsh

# Remove kerberos from PATH environment variable 
#
mv /etc/profile.d/krb5.sh /etc/profile.d/krb5.sh.org

cp /root/.bashrc /root/.bashrc.orig
echo "export PS1='# '" >> /root/.bashrc

# Create SIGMET root directory and data directory
#
IRIS_ROOT="/usr/sigmet"
mkdir -m 775 ${IRIS_ROOT}
chown operator:users ${IRIS_ROOT}

# If /usr/iris_data is a partition on the hard drive
# mkdir /usr/iris_data will fail, but we can
# still continue on with creating the data dirs 
# along with setting owner and permissions
#
IRIS_DATA="/usr/iris_data"
mkdir ${IRIS_DATA}
cd ${IRIS_DATA}
mkdir ascope ingest input log product product_raw tape_inv temp
chown -R operator:users ${IRIS_DATA}
chmod -R 775 ${IRIS_DATA}

# Begin the actual IRIS/RDA installation.  Begin with an IRIS install
# for either the IRIS or RCW cases, followed by an optional RDA
# install in the RCW case.  Otherwise just do an RDA install.
#
if [ "${HAS_IRIS}" = "true" ] ; then
    ${IRIS_INSTDIR}/instiris -files ${IRIS_INSTDIR} -root ${IRIS_ROOT} \
    -new -manuals -source -headers -emacs

  if [ "${HAS_RDA}" = "true" ] ; then
    ${RDA_INSTDIR}/instiris -files ${RDA_INSTDIR} -root ${IRIS_ROOT} -nodelete \
      -new -manuals -source -headers -emacs
  fi
else
  ${RDA_INSTDIR}/instiris -files ${RDA_INSTDIR} -root ${IRIS_ROOT} \
    -new -manuals -source -headers -emacs
fi

# Configuring RDA environment if we're really an RDA
#
if [ "${HAS_RDA}" = "true" ] ; then
  echo "Configuring RDA environment..."

  cp ${IRIS_ROOT}/bin/rda/rdasys                  /etc/init.d/
  cp ${IRIS_ROOT}/config_template/rc.d/rvp8       /etc/init.d/
  cp ${IRIS_ROOT}/config_template/rc.d/rcp8       /etc/init.d/
  cp ${IRIS_ROOT}/config_template/rc.d/dspexport  /etc/init.d/

  chmod +x /etc/init.d/rdasys
  chmod +x /etc/init.d/rvp8
  chmod +x /etc/init.d/rcp8
  chmod +x /etc/init.d/dspexport

  /sbin/chkconfig --level 2345 rdasys on

  for CONFFILE in rvp8 rcp8 softplane setup_ant ; do
    cp -p ${IRIS_ROOT}/config_template/init/${CONFFILE}.conf ${IRIS_ROOT}/config/
  done

  cd ${IRIS_ROOT}/config
  mkfifo fifo_hostio-x fifo_hostio-y fifo_dau-x fifo_dau-y fifo_dcu-x fifo_dcu-y
  chown operator:users fifo_*

  # Add ${IRIS_ROOT}/bin/dynamic to /etc/ld.so.conf and run ldconfig
  #
  echo "${IRIS_ROOT}/bin/dynamic" >> /etc/ld.so.conf
  /sbin/ldconfig

  # Add the SIGMET pci card info to the pci.ids file
  #
  mv /usr/share/hwdata/pci.ids /usr/share/hwdata/pci.ids.bak
  sed s/'Altera Corporation$'/'Altera Corporation\
          7805  SIGMET RVP8\/Rx IF Receiver\
          7806  SIGMET RVP8\/Tx IF Transmitter\
          7807  SIGMET RVP\/RCP 62-pin I\/O Board'/ /usr/share/hwdata/pci.ids.bak > \
    /usr/share/hwdata/pci.ids
fi

# Add the default X and motif things to /home/operator and
# /home/observer
#
echo "Configuring home environments..."

for USER in operator observer ; do
  cp ${IRIS_ROOT}/config_template/LINUX/desktop/kshrc     /home/${USER}/.kshrc
  cp ${IRIS_ROOT}/config_template/LINUX/desktop/mwmrc     /home/${USER}/.mwmrc
  cp ${IRIS_ROOT}/config_template/LINUX/desktop/profile   /home/${USER}/.profile
  cp ${IRIS_ROOT}/config_template/LINUX/desktop/Xdefaults /home/${USER}/.Xdefaults
  cp ${IRIS_ROOT}/config_template/LINUX/desktop/xinitrc   /home/${USER}/.xinitrc

  chown ${USER}:users /home/${USER}/.*
done

# Add specific services for each type of product
#
echo "Adding specific services for '${OPER}'..."

if [ "${OPER}" = "rvp8" ] ; then
  /sbin/chkconfig --add rvp8
  /sbin/chkconfig --add dspexport
fi
if [ "${OPER}" = "rcp8" ] ; then
  /sbin/chkconfig --add rcp8
fi
if [ "${HAS_IRIS}" = "true" ] ; then
  cp ${IRIS_ROOT}/config_template/rc.d/iris_init_linux /etc/init.d/iris
  chmod +x /etc/init.d/iris
  /sbin/chkconfig --add iris
fi

# Attempt to eject the CDROM and then request that the operator reboot
# the system.  This will clean out the services and start the system
# fresh.
#
echo '**********************************************************'
echo 'Remove the SIGMET CD from your CD drive and type "reboot"'
echo '**********************************************************'
