#!/bin/bash
#
# Copyright (c) 2023 SkyForm Software Corporation
#
# This script sets up SkyForm AIP on each host assuming all files are stored
# on a shared file system.
#
# By default, the path of the shared directory is: /opt/skyformai_shared
# The SkyForm AIP local top directory path will be read from the etc/aip in 
# the shared location.
#
# This script needs at least one parameter --distro to indicate which 
# Cubeman distribution to use. The value must match the directory name 
# where the files are installed, for example: 8.0
#

usage() {
        echo "Usage: $0 --shared=shared_dir --distro=distro_string --deploylocal --gui|--nogui --sshcontrol --upgrade"
        exit 1
}
i_share=`dirname $0`
if [ -f $i_share/install ]; then
  i_share=/opt/skyformai_shared
fi
distro=no
umask 022

bindirs="bin lib64 sbin share monitor"
non_bindirs="etc log work"
nogui=1
upgrade=0
deploylocal=0
sshcontrol=0

while [ $# != '0' ]; do
  case $1 in
  *=*) optarg=`expr "X$1" : '[^=]*=\(.*\)'` ;;
  *)  optarg=no ;;
  esac

  case $1 in
  --shared=*) 
	i_share=$optarg
        ;;
  --distro=*)
	distro=$optarg ;;
  --nogui)
        nogui=1 ;;
  --gui)
        nogui=0 ;;
  --upgrade)
        upgrade=1 ;;
  --deploylocal)
        deploylocal=1 ;;
  --sshcontrol)
        sshcontrol=1 ;;
  --help)
	usage ;;
  *)
	echo "ERROR: Invalid option"
        usage ;; 
  esac
  shift
done

if [ -f /etc/redhat-release ]; then
    echo Checking required software packages on Enterprise Linux...
    osver=`tr -dc '0-9.' </etc/redhat-release  | cut -d. -f1`
    allp=`rpm -qa`
    error=0
    setenforce 0
    sed -i s/SELINUX=enforcing/SELINUX=permissive/ /etc/selinux/config
    if [ $osver -ge '8' ]; then
        packages="libcurl libyaml psmisc net-tools libnsl"
    else
        packages="libcurl libyaml psmisc net-tools"
    fi
    for p in $packages; do
        echo $allp | grep -w $p > /dev/null
        if [ $? != 0 ]; then
            echo "Error: the software package $p is not installed."
            error=1
        fi
    done
    if [ $error == 1 ]; then
        exit 1
    fi
    if [ $osver -eq '6' ]; then
        ln -s /usr/bin/tail /bin/tail
    fi
fi

if [ $distro == "no" ]; then
   distlist=`ls -d $i_share/[0-9]*`
   if [ `echo $distlist | wc -w` == "1" ]; then
      distro=`basename $distlist`
   else
     while true; do
        echo "Please choose one of the following:"
        n=0
        for file in $distlist; do
          n=$((n+1))
          echo "[$n] `basename $file`"
        done
        read -p "Selection:" un
        if [ $un -ge "1" -a $un -le $n ]; then
           distro=`echo $distlist | cut -d' ' -f$un`
	   distro=`basename $distro`
           break
        fi 
     done 
   fi
fi

echo "Selected distro is: $distro"

if [ ! -d $i_share ]; then
   echo "SkyForm AIP shared install [$i_share] not found"
   usage
fi
 
if [ ! -d $i_share/$distro ]; then
   echo "SkyForm AIP distro [$i_share] not found"
   usage
fi

if [ ! -f $i_share/etc/aip ]; then
   echo "The file aip not found in [$i_share/etc]"
   exit 1
fi

i_top=`grep AIP_TOP= $i_share/etc/aip | cut -d= -f2`
# create symbolic link tree
if [ -f $i_top/sbin/cbls ]; then
    upgrade=1
fi

if [ ! -d $i_top ]; then
    mkdir $i_top
    if [ $? != "0" ]; then
	echo "No permission to create directory [$i_top]"
        exit 1
    fi
fi

# check if the host is configured in cb.yaml
hostname --fqdn > /dev/null
if [ $? != "0" ]; then
   echo "WARNING: The hostname of this host is not configured properly. Please fix."
   exit 1
fi

if [ $deploylocal == 1 ]; then
   install="cp -rf"
else
   install="ln -sf"
fi
for dpath in $bindirs; do
   if [ -L "$i_top/$dpath" ]; then # previous install was shared
      rm -rf $i_top/$dpath
   else                            # previous install was local
      if [ $deploylocal == 0 ]; then # install shared
         rm -rf $i_top/$dpath
      fi
   fi
   if [ $dpath == "bin" ]; then
       ln -sf $i_share/$distro/bin $i_top
   else
       $install $i_share/$distro/$dpath $i_top
   fi
done

if [ $deploylocal == 1 ]; then
   if [ -L $i_top/log ] || [ ! -d $i_top/log ]; then
      rm -f $i_top/log
      mkdir -p $i_top/log
   fi
else
   if [ ! -L $i_top/log ]; then
      rm -rf $i_top/log
   fi
fi 

for dpath in $non_bindirs; do
   if [ -L $i_top/$dpath ] || [ ! -d $i_top/$dpath ]; then
      ln -sf $i_share/$dpath $i_top
   fi
done

logdirconf=`grep 'logdir:' $i_top/etc/cb.yaml`
if [ $? == 0 ]; then
    logdir=`echo $logdirconf | cut -d: -f2`
    if [ ! -d $logdir ]; then
        mkdir -p $logdir
    fi
fi  

# set up shell profile
cp -f $i_top/etc/aip.*sh /etc/profile.d

# set up service
if [ -d /etc/init.d ]; then
    cp -f $i_top/etc/aip /etc/init.d
fi

if [ -d /lib/systemd/system ]; then
    cp $i_top/etc/aip.service /lib/systemd/system
    systemctl daemon-reload
    systemctl enable aip
    if [ $upgrade == '0' ]; then
        systemctl restart aip
    fi
else
    if [ $upgrade == '0' ]; then
        service aip start
    fi
    chkconfig aip on
fi

# check libnsl
if ldd $i_top/sbin/cbls | grep "not found"; then
    echo Please install above missing library. For example: yum -y install libnsl
    echo !!! Failed
    exit 1
fi

if [ ! -f /usr/sbin/dmidecode ]; then
    echo Please install dmidecode. For example: yum -y install dmidecode
    echo !!! Failed
    exit 1
fi

if [[ $osver == '8' ]]; then
    pushd . > /dev/null
    cd $i_top/bin
    for file in aipbills aip_user rdpsub; do
       if ! grep python2 $file > /dev/null; then
           sed -i s/python/python2/ $file
       fi
    done
    popd > /dev/null
fi

if [ $nogui == 0 ]; then
    if [ $upgrade == '1' ]; then
        $(dirname $0)/gui-setup --shared=$i_share --upgrade
    else
        $(dirname $0)/gui-setup --shared=$i_share
    fi
fi

# install PAM module
if [ $sshcontrol == '1' ]; then
    if [ -d /lib64/security ]; then
        libdest=/lib64/security
    else
        libdest=/lib/x86_64-linux-gnu/security
    fi
    cp $i_share/$distro/lib64/pam_aip.so $libdest/pam_aip.so
    if ! grep -q "^account required pam_aip.so" /etc/pam.d/sshd; then
        echo account required pam_aip.so CB_ENVDIR=$i_top/etc debug >> /etc/pam.d/sshd
    fi
fi

if [ $upgrade == '1' ]; then
    echo "AIP has been upgraded. Please restart daemons remotely from the master"
fi
