#!/bin/sh

##C
#
# Vaisala software source code file
#
# Copyright (c) Vaisala Oyj 2012.
# All rights reserved.
#
##/C

# @file
#
# Vaisala Post OS Install Script     
#
# $URL: https://subversion.vaisala.com/svn/repos/sigmet/wr_iris_rda/branches/release8.13.5/base/install_tools/sigconfig $
#
# $Id: sigconfig 30160 2014-06-25 18:17:40Z vch $

# 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:
#Select valid software type:
#            -rvp900 : Configure RVP900 system and start RVP9 services
#            -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
#   -instdir <DIR>   : Set directory holding RDA/IRIS files
#Select valid Linux release:"
#               -5   : For CentOs/RHEL5.x."
#               -6   : For CentOs/RHEL6.x."
#Optional directory path where CENTOS6 or RHEL6 is located:"
#  -instdir <DIR> : Set directory holding RDA/IRIS files"

# ==============================
# Identify OS disttribution 

OS_DIST=$(dist=`cat /etc/*release` ; echo ${dist:0:7})

#define yum repository configuration directory
YUMDIR='/etc/yum.repos.d'
YUM_BCK='repo.bck'

# Process argument list
HELP="" ; RVP="" ; RCP="" ; IRIS="" ; OS_VERSION="" ; INSTDIR="/mnt/cdrom"


update_sudoers()
{
    sfile=/etc/sudoers

    chmod u+w $sfile

    sed -i.bak -e "s/^\(Defaults[ ]* requiretty\)/#\1/" ${sfile}
    rm -f ${sfile}.bak

    # Add SERVICES alias if it is not already there
    grep -q -e "^Cmnd_Alias *SERVICES" $sfile  || \
        echo "Cmnd_Alias SERVICES = /sbin/service, /sbin/chkconfig" >> $sfile

    # Add operator to sudoers it is an old IRIS system with 
    # operator login account 
    grep -q -i -e "^operator.*nologin" /etc/passwd || \
    grep -q -i -e "^operator.*SERVICE" $sfile  || \
        echo "operator ALL=(ALL)  NOPASSWD: SERVICES" >> $sfile

    # Add radarop user to sudoers 
    grep -q -i -e "^radarop.*SERVICE" $sfile  || \
        echo "radarop ALL=(ALL)  NOPASSWD: SERVICES" >> $sfile

    chmod u-w $sfile
}


while [ "$1" != "" ] ; do
  OPT="$1" ; shift
  case ${OPT} in
  -rvp900 | -rvp8 | -rda )
    if [ "${RVP}" != "" ] ; then
      echo "Can only specify either -rvp900, -rvp8 or rda" 1>&2 ; exit 1
    else
      RVP="${OPT#-}"
    fi ;;

  -rcp8 )
      RCP="${OPT#-}" ;;

  -iris )
      IRIS="${OPT#-}" ;;

  -5|-6 )
    if [ "${OS_VERSION}" != "" ] ; then
      echo "Can not specify more than one option" 1>&2 ; exit 1
    else
      OS_VERSION="${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

# Verify that there is something to do, and that our release
# directories are reasonable.
#
if [ "${RVP}" = "" -a "${RCP}" = "" -a "${IRIS}" = "" ] ; then
  HELP="true"
elif [ "${OS_VERSION}" = "" ] ; then
  HELP="true"
fi

# Print HELP messages if these were requested, either directly or
# indirectly.
#
if [ "${HELP}" = "true" ] ; then
  echo ""
  echo "Command Line Options:"
  echo "Select valid Sigmet software type:"
  echo "         -rvp900 : Configure RVP900 system and start RVP900 services"
  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 "Select valid Linux release:"
  echo "              -5 : For CentOs/RHEL5.x"
  echo "              -6 : For CentOs/rhel6.x"
  echo "Optional directory path where CentOs6 or RHEL6 is located:"
  echo "  -instdir <DIR> : Set directory holding RDA/IRIS files"
  echo ""
  exit 0
fi

CUR_KERNEL_REL=`uname -r`
KERNEL_REL=`uname -r | sed -e "s/-.*//"`

if [ "${OS_VERSION}" = "5" -a "${KERNEL_REL}" = "2.6.18" ] ; then
  KERNEL_MATCH="true"
elif [ "${OS_VERSION}" = "6" -a "${KERNEL_REL}" = "2.6.32" ] ; then
  KERNEL_MATCH="true"
else
  KERNEL_MATCH="false"
fi

if [ "${KERNEL_MATCH}" = "false" ] ; then
  echo ""
  echo "#############################################################"
  echo "## Warning: Kernel release ${KERNEL_REL} does not match CentOs${OS_VERSION:1:1} or RHEL${OS_VERSION:1:1} release"
  if [ "${OS_VERSION}" = "5"  ] ; then
    echo "##          We Require kernel release 2.6.18."  
  elif [ "${OS_VERSION}" = "6"  ] ; then
    echo "##          We Require kernel release 2.6.32."  
  fi
  echo "##          Current kernel release is ${CUR_KERNEL_REL}."
  echo "#############################################################"
  echo ""
  echo -n "Type 'yes' if you want to override: "
  read OVERRIDE 
  if [ "${OVERRIDE}" != "yes" ] ; then
    echo "Exiting sigconfig"
    exit 1;
  fi
fi

IRIS_INSTDIR="${INSTDIR}/CENTOS${OS_VERSION}/iris"      ; HAS_IRIS=""
RDA_INSTDIR="${INSTDIR}/CENTOS${OS_VERSION}/rda"        ; HAS_RDA=""
EXTRASDIR="${INSTDIR}/CENTOS${OS_VERSION}/extras/RPMS"

#echo "RVP ${RVP}"
#echo "RCP ${RCP}"
#echo "IRIS ${IRIS}"
#echo "RHEL_VERSION ${RHEL_VERSION}"
#echo "IRIS_INSTDIR ${IRIS_INSTDIR}"
#echo "RDA_INSTDIR ${RDA_INSTDIR}"
#echo "EXTRASDIR ${EXTRASDIR}"
# Figure out the redhat version number


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


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


if [ ! -d ${EXTRASDIR} ] ; then
    echo "Missing RPMS directory in extras please install RPMS manually: ${EXTRASDIR}" 1>&2
else
  echo "Installing RPMS for CentOS/RHEL ${OS_VERSION}" 1>&2
  if [ "$(ls -A ${YUMDIR})" ] && [ ! -d "${YUMDIR}/${YUM_BCK}" ] ; then 
  install -m 644 -d ${YUMDIR}/${YUM_BCK}
  mv ${YUMDIR}/*.repo ${YUMDIR}/${YUM_BCK}/
  fi
  CURRENTDIR=$(pwd)

  cd ${EXTRASDIR}
   
  case ${OS_VERSION} in

      6 )
          install -m 644 ${CURRENTDIR}/iriscentos6.repo ${YUMDIR}/

          rpm_list_centos6="AdobeReader_enu     \ 
                          blas-devel            \
                          mesa-libOSMesa-devel  \
                          lapack-devel          \
                          proj-devel            \
                          libgeotiff-devel      \
                          ksh			\
			  sox			\
			  xorg-x11-fonts-75dpi"
                                     

          if [ "${HAS_RDA}" = "true" ] ; then
              rpm_list_centos6="${rpm_list_centos6}  \
                             dkms                    \
                             kvasercan-lib           \
                             linux-gpib-lib          \
                             linux-gpib-kmod-common  \
                             nrpz-lib"      
                             
          fi

          if [ "${HAS_IRIS}" = "true" -a "${OS_DIST}" = "CentOS" ] ; then
               rpm_list_centos6="${rpm_list_centos6}  \
                              hdf-devel               \
                              hdf5-devel              \
                              bufr-devel              \
                              netcdf-devel            \
                              jasper-devel            \
                              grib_api-devel          \
			      sox"         
          else
               rpm_list_centos6="${rpm_list_centos6}   \
                              hdf-devel                \
                              hdf5-devel               \
                              bufr-devel               \
                              netcdf-devel             \
                              jasper-devel             \
                              grib_api-devel"         
                                                                    
	  fi 
          shopt -s nullglob
          yum install -y --nogpgcheck $(echo $rpm_list_centos6 | tr -d '\\')
          shopt -u nullglob 
	  ;;

      5 )
	  install -m 644 ${CURRENTDIR}/iriscentos5.repo ${YUMDIR}/

          rpm_list_centos5="acroread        \
			  acroread-plugin   \
                          proj-devel        \
                          libgeotiff-devel  \
			  xorg-x11-fonts-75dpi"


          if [ "${HAS_RDA}" = "true" ] ; then
            rpm_list_centos5="${rpm_list_centos5}  \
                           dkms                    \
                           kvasercan-lib           \
                           linux-gpib-lib          \ 
                           nrpz-lib"

          fi

          if [ "${HAS_IRIS}" = "true" ] ; then
             rpm_list_centos5="${rpm_list_centos5}  \
                            hdf-devel               \
                            hdf5-devel              \
                            bufr-devel              \
                            netcdf-devel            \
                            jasper-devel            \
			    boost                   \
                            grib_api-devel"

          fi
          shopt -s nullglob
          yum install -y --nogpgcheck  $(echo $rpm_list_centos5 | tr -d '\\')
          shopt -u nullglob
          ;;

  esac
fi

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

# Section A.4.2:
# Add radarop account with the appropriate settings.
#
if [ "`echo ~radarop`" != "/home/radarop" ]; then
    /usr/sbin/useradd -G users -m -u 1002 radarop
    echo 'xxxxxx' | passwd --stdin radarop
fi

# Add the observer account with the appropriate settings
#

if [ "`echo ~observer`" != "/home/observer" ]; then
    /usr/sbin/useradd -G users -m -u 1001 observer
    echo 'xxxxxx' | passwd --stdin observer
fi

# Add operator and radarop to sudoers file
update_sudoers

echo "Cleaning up default services..."

# Section A.4.3:
# 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

# Fix per_source for rsh.
#
grep per_source /etc/xinetd.d/rsh >/dev/null \
    || sed -i.bak -e "/^ *}/i\\\tper_source\t\t= 100" /etc/xinetd.d/rsh
rm -f /etc/xinetd.d/rsh.bak

    
# Remove kerberos from this machine (we do not support krb versions of rsh)
# This was a problem in RHEL5
if [ "${RHEL_VERSION}" = "5" ] ; then
  if rpm -q krb5-workstation > /dev/null ; then rpm -e krb5-workstation; fi
fi

# Section A.4.4
# Create IRIS root directory and data directories
#
IRIS_ROOT="/usr/sigmet"
if [ ! -d ${IRIS_ROOT} ] ; then mkdir ${IRIS_ROOT} ; fi
chown operator:users ${IRIS_ROOT}
chmod 6775 ${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"
if [ ! -d ${IRIS_DATA} ] ; then mkdir ${IRIS_DATA} ; fi
cd ${IRIS_DATA}
for DIR in ascope ingest input log product product_raw suncal tape_inv temp zdrcal ; do
    if [ ! -d ${DIR} ] ; then mkdir ${DIR} ; fi
done
chown -R operator:users ${IRIS_DATA}
chmod -R 6775 ${IRIS_DATA}

# Section A.4.5
# Begin the actual IRIS/RDA installation.  Begin with an IRIS install
# followed by an optional RDA install if both are specified.
# Otherwise just do an RDA install.
#
if [ "${HAS_IRIS}" = "true" ] ; then
    ${IRIS_INSTDIR}/instiris -files ${IRIS_INSTDIR} -root ${IRIS_ROOT} \
    -new -manuals

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

# Section A.4.6
# Add the shell environment
#
echo "Configuring home environments..."

IRIS_CONF_DESK="${IRIS_ROOT}/config_template/LINUX/desktop"
for USER in radarop observer ; do
    install --owner=${USER} --group=users ${IRIS_CONF_DESK}/mwmrc        /home/${USER}/.mwmrc
    install --owner=${USER} --group=users ${IRIS_CONF_DESK}/bash_profile /home/${USER}/.bash_profile
    install --owner=${USER} --group=users ${IRIS_CONF_DESK}/Xdefaults    /home/${USER}/.Xdefaults
    if [ "${HAS_RDA}" = "true" ] ; then
	install --owner=${USER} --group=users ${IRIS_CONF_DESK}/xinitrc_mwm /home/${USER}/.xinitrc
    else
	install --owner=${USER} --group=users ${IRIS_CONF_DESK}/xinitrc     /home/${USER}/.xinitrc
    fi
done

# Section A.4.7
# Increase shared memory for all system types 
#
  grep shmmax /etc/sysctl.conf >/dev/null \
    || echo -e "\n# Increase Shared Memory\nkernel.shmmax = 500000000" >> /etc/sysctl.conf 


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

  /sbin/chkconfig --add rdasys

#  cd ${IRIS_ROOT}/config
#  mkfifo fifo_hostio-x fifo_hostio-y fifo_dau-x fifo_dau-y fifo_dcu-x fifo_dcu-y
#  chown radarop: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

  grep rmem_default /etc/sysctl.conf >/dev/null \
    || echo "net.core.rmem_default = 1000000" >> /etc/sysctl.conf 
 
  grep rmem_max /etc/sysctl.conf >/dev/null \
    || echo "net.core.rmem_max = 4000000" >> /etc/sysctl.conf  

  # Add the SIGMET pci card info to the pci.ids file
  #

  grep "SIGMET RVP8" /usr/share/hwdata/pci.ids >/dev/null
  if [ "$?" = "1" ] ; then
      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
fi

# Section A.4.9
# Add specific services for each type of product
#

if [ "${RVP}" = "rvp8" ] || \
   [ "${RVP}" = "rvp900" ] || \
   [ "${RCP}" = "rcp8" ] || \
   [ "${HAS_IRIS}" = "true" ] ; then
  /sbin/chkconfig --add antennad
fi

echo "Adding specific services for '${RVP}'..."

if [ "${RVP}" = "rvp8" ] ; then
  /sbin/chkconfig --add rvp8
  /sbin/chkconfig --add dspexport
fi

if [ "${RVP}" = "rvp900" ] ; then
  /sbin/chkconfig --add rvp900
  /sbin/chkconfig --add dspexport
 if [ -f /etc/sysconfig/network-scripts/ifcfg-eth0 ] ; then
  grep MTU /etc/sysconfig/network-scripts/ifcfg-eth0 >/dev/null \
    || echo "MTU=8192" >> /etc/sysconfig/network-scripts/ifcfg-eth0
 fi

# Configure eth1 interface for RVP9 IFDR. It was configured in kickstart, 
# but kickstart crash if the system doesn't have eth1 device. This is not
# a good solution to modify ifcfg-eth1, but needs to handle not to add 
# the same parameters to the file if the script is re-run. 
#
 if [ -f /etc/sysconfig/network-scripts/ifcfg-eth1 ] ; then  
  grep MTU /etc/sysconfig/network-scripts/ifcfg-eth1 >/dev/null \
    || echo "MTU=8192" >> /etc/sysconfig/network-scripts/ifcfg-eth1
  grep BOOTPROTO /etc/sysconfig/network-scripts/ifcfg-eth1 >/dev/null \
    || sed -i".bak" -e '/DEVICE/ a BOOTPROTO=none' \
    /etc/sysconfig/network-scripts/ifcfg-eth1
  grep IPADDR /etc/sysconfig/network-scripts/ifcfg-eth1 >/dev/null \
    || sed -i".bak" -e '/BOOTPROTO/ a IPADDR=10.0.1.213' \
    /etc/sysconfig/network-scripts/ifcfg-eth1
  grep NETMASK /etc/sysconfig/network-scripts/ifcfg-eth1 >/dev/null \
    || sed -i".bak" -e '/IPADDR/ a NETMASK=255.255.255.0' \
    /etc/sysconfig/network-scripts/ifcfg-eth1
  sed -i".bak" \
   -e 's/ONBOOT=\([\"]*\)no\([\"]*\)$/ONBOOT=\1yes\2/' \
    /etc/sysconfig/network-scripts/ifcfg-eth1
 fi 

 if [ -f /etc/sysconfig/network-scripts/ifcfg-eth1.bak ] ; then
  /bin/rm /etc/sysconfig/network-scripts/ifcfg-eth1.bak 
 fi 
fi
# Creating and modifying /etc/sysconfig/rpcbind because of the 
# "weak credentials" error caused when using RPCs on RHEL6 or CentOs 6.
if [ "${OS_VERSION}" = "6" ] ; then
  if [ ! -r /etc/sysconfig/rpcbind ] ; then
    echo "RPCBIND_ARGS=-i">/etc/sysconfig/rpcbind
    /sbin/service rpcbind restart
  fi
fi
if [ -f /etc/modprobe.d/dist-oss.conf ] ; then
sed -i".bak" -e 's/^# install/install/' /etc/modprobe.d/dist-oss.conf
rm -f /etc/modprobe.d/dist-oss.conf.bak
fi
if [ "${RCP}" = "rcp8" ] ; then
  /sbin/chkconfig --add rcp8
fi

if [ "${HAS_IRIS}" = "true" ] ; then
  /sbin/chkconfig --add iris
fi

# Request that the radarop eject the CDROM and reboot
# the system.  This will clean out the services and start the system
# fresh.
#
echo '**********************************************************'
echo 'Remove CD ("cd /" then "eject /mnt/cdrom"), then "reboot"'
echo '**********************************************************'
