#!/bin/bash
#Nifty: Install TSM on Ubuntu

err() {
  echo $(tput setaf 1)ERROR $@$(tput sgr0)
}

# Detect the current operating system
detect_os() {
  if [ -f "/etc/os-release" ]; then
    . /etc/os-release
  fi

  os="unknown"

  if [ "$ID" = "centos" ]; then
    if [ "$VERSION_ID" = "6" ]; then
      os="centos6"
    elif [ "$VERSION_ID" = "7" ]; then
      os="centos7"
    fi

  elif [ "$ID" = "ubuntu" ]; then
    if [ "$VERSION_ID" = "12.04" ]; then
      os="ubuntu1204"
    elif [ "$VERSION_ID" = "14.04" ]; then
      os="ubuntu1404"
    elif [ "$VERSION_ID" = "16.04" ]; then
      os="ubuntu1604"
    elif [ "$VERSION_ID" = "18.04" ]; then
      os="ubuntu1804"
    fi
  fi

  echo $os
}

# Require a specific operating system
require_os() {
  OS=`detect_os`
  if [ "$OS" != "ubuntu1204" ] && [ "$OS" != "ubuntu1404" ] && [ "$OS" != "ubuntu1604" ] && [ "$OS" != "ubuntu1804" ]; then
    err "This script requires Ubuntu 12.04, 14.04, 16.04 or 18.04"
    exit 1
  fi
}
require_os

killall dsmc > /dev/null 2>&1
killall dsmcad > /dev/null 2>&1

mkdir -p /usr/local/src/tsm
cd /usr/local/src/tsm

wget https://mirror.zitcom.dk/internal/nifty.sh/tsm/7.1.4.0-TIV-TSMBAC-LinuxX86_DEB.tar
wget https://mirror.zitcom.dk/internal/nifty.sh/tsm/conf/dsm_ubuntu.sys

tar fx 7.1.4.0-TIV-TSMBAC-LinuxX86_DEB.tar

VERS=`lsb_release -a 2>&1 |grep Release |awk '{print $2}'`

bit=`getconf LONG_BIT`

if [ "$bit" = "64" ]; then
        apt-get install -y libstdc++6 ksh

  echo "Installing GSK libraries"
        dpkg -i gskcrypt64_8.0-50.52.linux.x86_64.deb gskssl64_8.0-50.52.linux.x86_64.deb

  echo "Installing TSM API"
  dpkg -i tivsm-api64.amd64.deb

  echo "Installing TSM Backup Agent"
  dpkg -i tivsm-ba.amd64.deb
else
  echo "This version of the TSM agent does not support 32 bit! So long old friend..."
  exit 1
fi

# Create script to mysql-backup
mkdir /scripts
wget https://mirror.zitcom.dk/internal/nifty.sh/tsm/custombackup_ubuntu
mv custombackup_ubuntu /scripts/custombackup
chmod 700 /scripts/custombackup

# Grab custom init-script to fix locale issue
wget https://mirror.zitcom.dk/internal/nifty.sh/tsm/dsmcad_ubuntu.init -O /etc/init.d/dsmcad

# System start up
ln -s /etc/init.d/dsmcad /etc/rc2.d/S99tsm

mv /opt/tivoli/tsm/client/ba/bin/dsm.opt.smp /opt/tivoli/tsm/client/ba/bin/dsm.opt
cp dsm_ubuntu.sys /opt/tivoli/tsm/client/ba/bin/dsm.sys

#"${EDITOR:-nano}" /opt/tivoli/tsm/client/ba/bin/dsm.sys

# The en_us locales doesn't seem to exist on Ubuntu, so generate it
echo "Generating en_US locale..."
locale-gen en_US > /dev/null

# Create locale script
cat <<EOF > /usr/local/bin/dsmc
#!/bin/sh
# Execute Tivoli Storage Manager command, but set the proper Locale first
echo Setting Locale to en_US
export LANG=en_US
export LC_ALL=en_US
exec /opt/tivoli/tsm/client/ba/bin/dsmc \$*
EOF

chmod +x /usr/local/bin/dsmc

rm -rf /usr/local/src/tsm

clear

echo "What you need to do now:"
echo " "
echo "1 ) Run: vi /opt/tivoli/tsm/client/ba/bin/dsm.sys"
echo "    - Fill in Nodename and remove the TMS-server you are not going to use"
echo " "
echo "2 ) Run: dsmc incr /etc/passwd"
echo "    - This will prompt for agent password and back up /etc/passwd"
echo " "
echo "3 ) Run: /etc/init.d/dsmcad start"
echo " "
echo "4 ) Smile and wave, boys. Smile and wave"
