#!/bin/bash

# System install script. This script takes care of all configuration and
# installation tasks to provide a working iGp/iGp12-F/iGp12-H client system.
# Designed for Redhat series distributions (RedHat, Centos, Fedora).
# Must be run as a root
#
# Copyright (c) 2006-2015 Dimtel, Inc., All Rights Reserved
#
# $Id: install.sh,v 1.9 2020/02/11 18:26:14 dim Exp $
# $Date: 2020/02/11 18:26:14 $
# $Author: dim $
# $Revision: 1.9 $
# $Log: install.sh,v $
# Revision 1.9  2020/02/11 18:26:14  dim
# Updates to support Centos 6, 7, 8
#
# Revision 1.8  2016/07/01 21:24:27  dim
# Updated color highlighting
#
# Revision 1.7  2015/02/13 10:16:11  dim
# Cleanup, support for modern OS (Centos 6 and 7, Fedora 20, 21)
#
# Revision 1.6  2012/03/12 08:46:06  dim
# Added support for iGp-HF version
#
# Revision 1.5  2011/12/13 08:16:37  dim
# bash != sh, as it turns out
#
# Revision 1.4  2011/04/15 18:10:51  dim
# Support for RHEL 6 based distributions, cross-device save/restore move fixed
#
# Revision 1.3  2011/04/14 20:47:48  dim
# Kill caRepeater after installation
#
# Revision 1.2  2010/10/05 18:16:54  dim
# Initial cleanup
#
# Revision 1.6  2009/02/27 17:52:41  dim
# Updated install script to handle multiple distributions
#
# Revision 1.5  2007/12/30 15:29:01  dim
# Script updates for Fedora 8 client software, configuration for IOC
# installations on different motherboards (boot device, network driver)
#
# Revision 1.4  2007/03/14 08:36:13  dim
# Extensive installation script updates
#
# Revision 1.3  2006/12/28 09:57:55  dim
# Installation cleanup, release updates
#
# Revision 1.2  2006/12/11 09:29:15  dim
# Installation process cleanup, EDM config updates
#
# Revision 1.1  2006/12/07 23:10:51  dim
# Distribution packaging and installation work
#
#

# Service functions
display_ok()
{
  /usr/bin/tput hpa 60
  echo -n [
  /usr/bin/tput setaf 2
  echo -n OK
  /usr/bin/tput sgr0
  echo ]
  return 0
}

display_error()
{
  /usr/bin/tput hpa 60
  echo -n [
  /usr/bin/tput setaf 1
  echo -n ERROR
  /usr/bin/tput sgr0
  echo ]
  return 0
}

strstr() {
  [ "${1#*$2*}" = "$1" ] && return 1
  return 0
}

strnstr() {
  [ "${1#*$2*}" = "$1" ] && return 0
  return 1
}

export PATH=/bin:/usr/bin:/sbin:/usr/sbin

# Verify that we are root
if [ $EUID -ne 0 ]; then
  echo "This script must be executed with root permissions"
  exit 1;
fi

# Figure out system architecture
case $(arch) in
  i386)   LIBDIR=lib;;
  i686)   LIBDIR=lib;;
  x86_64) LIBDIR=lib64;;
esac

# Start from figuring out where we are
dir=`dirname "$0"`
src=`(cd "${dir}"; pwd)`

# Figure out the destination
read -p "Username to install under [iGp]: " user
if [ a${user} == a ]; then user=iGp; fi

read -p "Install directory, relative to ~${user} [iGp]: " dest_dir
if [ a${dest_dir} == a ]; then dest_dir=iGp; fi

echo Source directory: ${src}
echo Install username: ${user}
echo Install directory: ${dest_dir}

# Create the user, if not there yet
echo -n Creating user ${user}:
/usr/sbin/useradd -m ${user} 2> /dev/null
case $? in
  0) 
    display_ok
    /usr/bin/passwd ${user}
    ;;
  9)
     echo -n " installing under existing account:"
     display_ok
     ;;
  *) 
     display_error
     exit 2
     ;;
esac

user_home=`/bin/bash -c "echo ~${user}"`
dest=${user_home}/${dest_dir}

if [ -a ${dest} ]; then
  read -p "Directory/file ${dest} already exists, OK to delete: [y/N]: " answer
  if strstr $"yY" "$answer"; then
    # Preserve save/restore files, EPICS address list
    tmpdir=`/bin/mktemp -d`
    [ -f ${dest}/config/epics_addr_list ] && /bin/cp ${dest}/config/epics_addr_list ${tmpdir}
    [ -d ${dest}/dl_8/sr ] && /bin/mv ${dest}/dl_8/sr ${tmpdir}/sr_8
    [ -d ${dest}/dl_12/sr ] && /bin/mv ${dest}/dl_12/sr ${tmpdir}/sr_12
    [ -d ${dest}/dl_12H/sr ] && /bin/mv ${dest}/dl_12H/sr ${tmpdir}/sr_12H
    if [ -d ${dest}/dl_8/pvs ]; then
      /bin/mkdir ${tmpdir}/pvs_8
      /bin/mv ${dest}/dl_8/pvs/*.pvs ${tmpdir}/pvs_8/
    fi
    if [ -d ${dest}/dl_12/pvs ]; then
      /bin/mkdir ${tmpdir}/pvs_12
      /bin/mv ${dest}/dl_12/pvs/*.pvs ${tmpdir}/pvs_12/
    fi
    if [ -d ${dest}/dl_12H/pvs ]; then
      /bin/mkdir ${tmpdir}/pvs_12H
      /bin/mv ${dest}/dl_12H/pvs/*.pvs ${tmpdir}/pvs_12H/
    fi
    preserved=1;
    /bin/rm -rf ${dest}
  else
    echo "Installation aborted"
    exit 3
  fi
fi

# Logfile holding installation information
logfile=${dest}/install.log

# Move the distribution to the install point
echo -n "Installing software under ${dest}:"
/usr/bin/rsync -rtl --delete "${src}/iGp/" ${dest}/
# Copy RELEASE over
/bin/cp "${src}/RELEASE" ${dest}/
# Create config directory
/bin/mkdir ${dest}/config

if [ -n "${preserved}" ]; then
  # Re-create saved configs, IOC addresses, save/restore setups
  [ -f ${tmpdir}/epics_addr_list ] && /bin/mv ${tmpdir}/epics_addr_list ${dest}/config/
  if [ -d ${tmpdir}/sr_8 ]; then
    ( cd ${tmpdir}/sr_8
    for i in *; do
      [ -e ${dest}/dl_8/sr/$i ] && /bin/rm -rf ${dest}/dl_8/sr/$i;
      /bin/mv $i ${dest}/dl_8/sr/$i;
    done
    )
    /bin/rmdir ${tmpdir}/sr_8
  fi
  if [ -d ${tmpdir}/sr_12 ]; then
    ( cd ${tmpdir}/sr_12
    for i in *; do
      [ -e ${dest}/dl_12/sr/$i ] && /bin/rm -rf ${dest}/dl_12/sr/$i;
      /bin/mv $i ${dest}/dl_12/sr/$i;
    done
    )
    /bin/rmdir ${tmpdir}/sr_12
  fi
  if [ -d ${tmpdir}/sr_12H ]; then
    ( cd ${tmpdir}/sr_12H
    for i in *; do
      [ -e ${dest}/dl_12H/sr/$i ] && /bin/rm -rf ${dest}/dl_12H/sr/$i;
      /bin/mv $i ${dest}/dl_12H/sr/$i;
    done
    )
    /bin/rmdir ${tmpdir}/sr_12H
  fi
  if [ -d ${tmpdir}/pvs_8 ]; then
    for pvs in ${tmpdir}/pvs_8/*.pvs; do
      name=`basename ${pvs} .pvs`
      if [ ${name} != "TEST" ]; then
        /bin/sed -e "s/^IGPF:TEST:/IGPF:${name}:/" ${dest}/dl_8/pvs/TEST.pvs > ${dest}/dl_8/pvs/${name}.pvs
      fi
      /bin/rm ${pvs}
    done
    /bin/rmdir ${tmpdir}/pvs_8
  fi
  if [ -d ${tmpdir}/pvs_12 ]; then
    for pvs in ${tmpdir}/pvs_12/*.pvs; do
     name=`basename ${pvs} .pvs`
      if [ ${name} != "TEST" ]; then
        /bin/sed -e "s/^IGPF:TEST:/IGPF:${name}:/" ${dest}/dl_12/pvs/TEST.pvs > ${dest}/dl_12/pvs/${name}.pvs
      fi
      /bin/rm ${pvs}
    done
    /bin/rmdir ${tmpdir}/pvs_12
  fi
  if [ -d ${tmpdir}/pvs_12H ]; then
    for pvs in ${tmpdir}/pvs_12H/*.pvs; do
     name=`basename ${pvs} .pvs`
      if [ ${name} != "TEST" ]; then
        /bin/sed -e "s/^IGPF:TEST:/IGPF:${name}:/" ${dest}/dl_12H/pvs/TEST.pvs > ${dest}/dl_12H/pvs/${name}.pvs
      fi
      /bin/rm ${pvs}
    done
    /bin/rmdir ${tmpdir}/pvs_12H
  fi
  /bin/rmdir ${tmpdir}
fi

# Change ownership
/usr/bin/find ${dest} -print0 | xargs -0 chown ${user}:${user}
# Make all files writable by the user
/usr/bin/find ${dest} -print0 | xargs -0 chmod u+w

display_ok

# Install the necessary RPMs
echo -n "Installing necessary RPMs:"
RPMS="libXp libXpm xorg-x11-fonts-75dpi xorg-x11-fonts-ISO8859-1-75dpi"

# Under Centos 6 do not attempt to use yum, repositories are gone
ver=$(cat /etc/redhat-release |sed -e 's/.* \([0-9]\).*/\1/')
if [ $ver -eq 6 ]; then
  echo "Centos 6 detected" > ${logfile}
  # Local install
  MOTIF=openmotif
  arch=$(arch)
  LIST="$RPMS $MOTIF"
  for f in $LIST; do
    [ -f ${src}/sw/$f-*.$arch.rpm ] && rpm -Uvh ${src}/sw/$f-*.$arch.rpm >> ${logfile} 2>&1
    [ -f ${src}/sw/$f-*.noarch.rpm ] && rpm -Uvh ${src}/sw/$f-*.noarch.rpm >> ${logfile} 2>&1
  done
else
  # First of all, disable GPG signature check
  /bin/grep gpgcheck=1 /etc/yum.conf > /dev/null
  if [ $? -eq 0 ]; then
    tmp=`/bin/mktemp -p /tmp`
    /bin/sed -e 's/gpgcheck=1/gpgcheck=0/' /etc/yum.conf > ${tmp}
    /bin/mv /etc/yum.conf /etc/yum.conf.iGp_save
    /bin/mv ${tmp} /etc/yum.conf
    yum_restore=1
  else
    yum_restore=0
  fi
  # Motif package can be openmotif, motif, or openmotif22
  for MOTIF in openmotif openmotif22 motif; do
    /usr/bin/yum list $MOTIF > ${logfile} 2>&1
    if [ $? -eq 0 ]; then
      # Found it
      break;
    fi
  done
  /usr/bin/yum install $RPMS -y > ${logfile} 2>&1
  /usr/bin/yum install $MOTIF -y >> ${logfile} 2>&1

  # If libpng12.so.0 does not exist, install it
  [ ! -e /usr/${LIBDIR}/libpng12.so.0 ] && /usr/bin/yum install libpng12 -y >> ${logfile} 2>&1

  # Install compat-readline6 - needed on Centos 8
  [ ! -e /${LIBDIR}/libreadline.so.6 ] && /usr/bin/yum install ${src}/sw/compat-readline6*rpm -y >> ${logfile} 2>&1

  # Restore yum.conf, if necessary
  if [ $yum_restore -eq 1 ]; then
    /bin/mv /etc/yum.conf.iGp_save /etc/yum.conf
  fi
fi

RPMS="${RPMS} ${MOTIF}"
err=""
for name in ${RPMS}; do
  /bin/rpm -q ${name} >> ${logfile}
  if [ $? -eq 1 ]; then
    err="${name} ${err}"
  fi
done
if [ -n "$err" ]; then
  display_error
  echo Missing: $err
else
  display_ok
fi

# Rebuild edm object files
echo -n "Rebuilding EDM configuration files:"
(cd ${dest}; . ./envSet.sh; edm_config) >> ${logfile} 2>&1
display_ok

# Make sure caRepeater is not running under root
/usr/bin/pkill -9 caRepeater

# Add environment setup to .bashrc
# Remove old line, if any
dotfile=${user_home}/.bashrc
echo -n "Adding environment setup to ${dotfile}:"
tmp=`/bin/mktemp -p /tmp`
/bin/grep -v "/envSet.sh # Added by iGp12 install, do not modify" ${dotfile} > ${tmp}
/bin/cp ${dotfile} ${dotfile}.iGp.save
/bin/cp ${tmp} ${dotfile}
/bin/rm ${tmp}
echo ". \${HOME}/${dest_dir}/envSet.sh # Added by iGp12 install, do not modify" >> ${dotfile}
display_ok

# We are done!
echo "The installation is complete."
cat <<EOF
Do not forget to run "IOC_add <IP address> <device name>" for each IOC. 
For example, for IOC at 192.168.0.111 with device name FB (EPICS
channels start with IGPF:FB:) type: IOC_add 192.168.0.111 FB
EOF
