#!/bin/bash
#Nifty: Install or update TSM client version 8

# Source niftylib
# shellcheck source=../../lib/universal/niftylib.sh
source <(curl -fSs https://nifty.sh/lib/universal/niftylib.sh)

ce "#########################"
ce "# team.blue TSM Updater #"
ce "#########################"

require_root
require_64bit
require_os centos7 alma8 alma9 alma10

TSM_VERSION="$(rpm -q --qf '%{VERSION}' TIVsm-BA | cut -f1 -d.)"
TSM_INSTALLED="$?"

REPO_FILE="/etc/yum.repos.d/teamblue_saferepo"
if [ -f "$REPO_FILE" ]; then
  ce "Safe repository is already configured"
else
  ce "Configuring team.blue safe repository"
  REPO_FILE="/etc/yum.repos.d/teamblue_saferepo.repo"
  OS_VERSION="$(get_os_version)"
  OS_VERSION_MAJOR="${OS_VERSION/.*/}"

  rpmkeys --import "https://repo.dk.team.blue/centos/$OS_VERSION_MAJOR/x86_64/RPM-GPG-KEY-teamblue"

  [ ! -f "$REPO_FILE" ] && tee <<EOF > "$REPO_FILE"
[teamblue_saferepo]
name=team.blue yum repo
baseurl=https://repo.dk.team.blue/centos/$OS_VERSION_MAJOR/x86_64
enabled=True
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-teamblue
EOF
fi

if ! localectl list-locales | grep -q 'en_US.UTF-8'; then
  ce "Installing english locale and language pack"
  yum install -y glibc-langpack-en
fi

if [ -z "$TSM_INSTALLED" ] && [ "$TSM_VERSION" -lt 8 ]; then
  ce "Killing all TSM instances"
  killall dsmc &> /dev/null
  killall dsmcad &> /dev/null

  if [ -f "/opt/tivoli/tsm/client/ba/bin/dsm.sys" ]; then
    ce "Backing up config"
    cp /opt/tivoli/tsm/client/ba/bin/dsm.sys /root/dsm.sys.bak
  fi

  ce "Removing old packages, if present"
  for RPM in "TIVsm-BA" "TIVsm-API64" "gskssl64" "gskcrypt64"; do
    ce "Removing package: $RPM"
    yum remove -y "$RPM" &> /dev/null
  done
elif [ -z "$TSM_INSTALLED" ]; then
  ce "TSMv8+ is already installed. The package will be updated and init files replaced with systemd units."
else
  ce "TSM is not installed. Will install TSM from safe repository."
fi

# Do this clean up regardless of TSM version currently installed
for s in {tsm,dmscad}; do
  ce "Removing SysV configuration: $s"
  chkconfig --list 2> /dev/null | grep -qE "^$s" && chkconfig --del "$s"
  rm "/etc/init.d/$s" &> /dev/null
done

ce "Installing/updating TSM from repository"
yum install -y TIVsm-BA TIVsm-API64

if [ -n "$TSM_INSTALLED" ] || [ "$TSM_VERSION" -lt 8 ]; then
  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"

    while ! TSM_CONFIG="$(curl -fsS https://mirror.dk.team.blue/internal/nifty.sh/tsm/conf/dsm.sys)" || [ -z "$TSM_CONFIG" ]; do
      MY_IP="$(curl -Ss "https://zxx.dk/clean/")"
      ce "Cannot reach mirror.dk.team.blue from $MY_IP. Please make sure the server is whitelisted."
      ce "Whitelist is configured in mirror.dk.team.blue:/etc/nginx/location.common.conf."

      # Just exit if running without a TTY
      is_interactive || exit 1

      ce "Press enter to retry..."
      read
    done

    echo "$TSM_CONFIG" > /opt/tivoli/tsm/client/ba/bin/dsm.sys
    cp /opt/tivoli/tsm/client/ba/bin/dsm.opt.smp /opt/tivoli/tsm/client/ba/bin/dsm.opt
    mkdir -p /etc/systemd/system/dsmcad.service.d
    tee /etc/systemd/system/dsmcad.service.d/override.conf <<-EOF
      [Unit]
      StartLimitIntervalSec=0

      [Service]
      Restart=always
      RestartSec=5
EOF

    ce "Setting up dsmc alias"
    wget -O /usr/local/bin/dsmc https://mirror.dk.team.blue/internal/nifty.sh/tsm/conf/dsmc_alias
    chmod +x /usr/local/bin/dsmc

    if [ -f "/usr/sbin/plesk" ]; then
      ce "Plesk is installed. Excluding webstat-logs"
      if ! grep -qF 'EXCLUDE "/var/www/vhosts/*/logs/*.webstat"' /opt/tivoli/tsm/client/ba/bin/dsm.sys; then
        ce "Inserting exclude in dsm.sys"
        echo 'EXCLUDE "/var/www/vhosts/*/logs/*.webstat"' >> /opt/tivoli/tsm/client/ba/bin/dsm.sys
      fi
    fi

    ce "Press any key to edit dsm.sys and insert node info"
    read
    vim /opt/tivoli/tsm/client/ba/bin/dsm.sys
  fi
fi

ce "Running test"
dsmc incr /etc/passwd

ce "Starting and enabling TSM service"
systemctl daemon-reload
systemctl enable dsmcad.service
systemctl restart dsmcad.service
echo
systemctl status --no-pager dsmcad.service
echo

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

