#!/bin/bash
#Install or update TSM client version 8
function ce() {
        echo $(tput setaf 6)$@$(tput sgr0)
}
function err() {
        echo $(tput setaf 1)ERROR $@$(tput sgr0)
}

ce "######################"
ce "#TSM Installer #"
ce "######################"

if [ "$(id -u)" != "0" ]; then
   err "This script must be run as root" 
   exit 1
fi

if [ "`getconf LONG_BIT`" != "64" ]; then
	err "This server is running a 32 bit version of the Operating System"
	err "For this reason, it cannot be updated."
	exit 1
fi

RELEASE=`rpm -qa |grep -Eo "(almalinux|cloudlinux|centos)-release-[0-9]{1}" | grep -v plesk | cut -d"-" -f3`
if [ "$RELEASE" != "7" ] && [ "$RELEASE" != "8" ] && [ "$RELEASE" != "9" ]; then
  err "This OS release is not allowed."
  exit 1
fi

TSMVER=`rpm -q --queryformat '%{VERSION}\n' TIVsm-BA |awk -F "." '{print $1}'`
if [ "$TSMVER" = "8" ]; then
  err "This server already runs version 8 of the client."
  exit 1
fi

ce "Getting new TSM archive"
if [ ! -d "/usr/local/src/tsm/version8" ]; then
	mkdir -p /usr/local/src/tsm/version8
fi
cd /usr/local/src/tsm/version8

if ! hash wget 2>/dev/null; then
	echo "Seems that wget is not installed. wget is required!"
	exit 1
fi

ce "Downloading newest TSM Client - if script dies, the download failed!"
TARFILE="8.1.26.0-TIV-TSMBAC-LinuxX86.tar"
URL="https://tsmadmin.zitcom.dk/files/Clients/Linux/$TARFILE"
wget $URL >/dev/null 2>&1
if [ "$?" != "0" ]; then
  err "Could not download: $URL"
  exit 1
fi
ce "Unpacking the archive"
tar xf $TARFILE
rm -f $TARFILE


ce "Killing all TSM instances"
killall dsmc > /dev/null 2>&1
killall dsmcad > /dev/null 2>&1

ce "Backing up config"
if [ -f "/opt/tivoli/tsm/client/ba/bin/dsm.sys" ]; then
  cp /opt/tivoli/tsm/client/ba/bin/dsm.sys /root/dsm.sys.bak
else 
  ce "There is no config. We will assume this server is a new install"
  yum install -y glibc
fi

TSMRPM=`rpm -qa --qf "%{NAME}\n" |grep -Eio "^gsk.*|^TIV.*" |sed ':a;N;$!ba;s/\n/ /g'`
if [ "$TSMRPM" != "" ]; then
  ce "Removing current packages: $TSMRPM"
  rpm -e $TSMRPM
else
  ce "No TSM packages installed"
fi


ce "Cleaning up init"
if [ -f "/etc/init.d/tsm" ]; then
	rm -rf /etc/init.d/tsm
fi
if [ -f "/etc/init.d/dsmcad" ]; then
	rm -rf /etc/init.d/dsmcad
fi
chkconfig --del tsm > /dev/null 2>&1
chkconfig --del dsmcad > /dev/null 2>&1


ce "Installing GSK libraries"
rpm -Uvh gsk*rpm

ce "Installing TSM Packages"
rpm -Uvh TIVsm-API64.x86_64.rpm TIVsm-BA.x86_64.rpm

if [ -f "/root/dsm.sys.bak" ]; then
	ce "Restoring config backup"
	mv -f /root/dsm.sys.bak /opt/tivoli/tsm/client/ba/bin/dsm.sys
else
  ce "No backup of conf found. Assuming new install"

  cp /opt/tivoli/tsm/client/ba/bin/dsm.opt.smp /opt/tivoli/tsm/client/ba/bin/dsm.opt
  wget -O /opt/tivoli/tsm/client/ba/bin/dsm.sys https://tsmadmin.zitcom.dk/files/Clients/Linux/dsm.sys.smp

  ce "Setting up dsmc alias"
  wget -O /usr/local/bin/dsmc https://tsmadmin.zitcom.dk/files/Clients/Linux/dsmc_alias.smp
  chmod +x /usr/local/bin/dsmc

  echo -en "TSM Nodename (e.g. BN000123): "
  read nodename

  echo -en "TSM Server (e.g. tsm3.zitcom.dk): "
  read serverhost

  if [ "$nodename" = "" ]; then
	echo "WRONG NODENAME"
	exit 1
  fi
  if [ "$serverhost" = "" ]; then
	echo "WRONG TSM SERVER"
	exit 1
  fi


  sed -i s/LINUXTSM/"$nodename"/g /opt/tivoli/tsm/client/ba/bin/dsm.sys
  sed -i s/TSMSERVER/"$serverhost"/g /opt/tivoli/tsm/client/ba/bin/dsm.sys

fi



echo "

	+-+-+-+ INFO - READ! +-+-+-+

	We will now initialize TSM by making a backup of /etc/passwd
	This will cause TSM to ask for the Nodename (press enter) and 
	the node password. After giving password it will ask for 
	encryption password. NOTE! The encryption password CANNOT
	be recovered! If you throw away the encryption password 
	your backup is gone forever.

	Press enter when you have READ and UNDERSTOOD this.

"
read

ce "Running test"
dsmc incr /etc/passwd

ce "Enabling service"
systemctl enable --now dsmcad

ce "Starting TSM"
/etc/init.d/tsm start

ce "##############################################"
ce "# If all went well above - we are done here! #"
ce "##############################################"
