#!/bin/bash
#
# linuxmuster-opsi
#
# thomas@linuxmuster.net
# 20190308
#

# read linuxmuster.net environment
. /usr/share/linuxmuster-opsi/environment || exit 1

# parsing parameters
getopt $*

header(){
 [ -n "$quiet" ] && return 0
 local opts="$@"
 local msg="# linuxmuster-opsi $opts # $(date) #"
 local chars="$(echo "$msg" | wc -m)"
 echo
 seq -s"#" $chars | tr -d '[:digit:]'
 echo "$msg"
 seq -s"#" $chars | tr -d '[:digit:]'
 echo
}

usage() {
  header "usage"
  echo "linuxmuster-opsi --setup [--first]"
  echo "                 --password=<password>"
  echo "                 --install"
  echo "                 --prepare"
  echo "                 --wsimport"
  echo "                 --reboot"
  echo "                 --quiet"
  echo "                 --help"
  echo
  echo "  setup:    Starts linuxmuster.net specific OPSI setup."
  echo "            Together with --first param it does an initial setup"
  echo "            with forced certificate creation. Expects the server file"
  echo "            /var/lib/linuxmuster/network.settings under"
  echo "            /var/lib/linuxmuster-opsi/settings."
  echo "  password: Change opsiadmin password. If no value is set passwords were asked."
  echo "  install:  Installs necessary OPSI packages."
  echo "  prepare:  Initial opsi configuration. Install has to be invoked before."
  echo "  wsimport: Does client import, expects the server file"
  echo "            /etc/linuxmuster/workstations under"
  echo "            /var/lib/linuxmuster-opsi/workstations."
  echo "  reboot:   Does a reboot finally."
  echo "  quiet:    Minimal console output."
  echo "  help:     Shows this help"
  echo
  exit 1
}

# test parameters
[ -n "$help" ] && usage
[ -n "$wsimport" -a -n "$prepare" ] && usage
[ -n "$wsimport" -a -n "$setup" ] && usage
[ -n "$wsimport" -a -n "$first" ] && usage
[ -n "$wsimport" -a -n "$password" ] && usage
[ -n "$wsimport" -a -n "$install" ] && usage
[ -n "$prepare" -a -n "$setup" ] && usage
[ -n "$prepare" -a -n "$first" ] && usage
[ -n "$prepare" -a -n "$password" ] && usage
[ -n "$prepare" -a -n "$install" ] && usage
[ -n "$password" -a -n "$setup" ] && usage
[ -n "$password" -a -n "$first" ] && usage
[ -n "$password" -a -n "$install" ] && usage
[ -n "$setup" -a -n "$install" ] && usage
[ -z "$wsimport" -a -z "$prepare" -a -z "$setup" -a -z "$password" -a -z "$install" ] && usage

# get action
[ -n "$wsimport" ] && action="wsimport"
[ -n "$prepare" ] && action="prepare"
if [ -n "$password" ]; then
  action="password"
  [ "$password" = "password" ] && password=""
fi
[ -n "$setup" ] && action="setup"
[ -n "$install" ] && action="install"

# logging
LOGFILE="/var/log/linuxmuster-opsi-$action.log"

RC="0"

# print script header
header "$action" | tee -a "$LOGFILE"

# check if i am already running
mypid=$$
mypidfile=/var/run/linuxmuster-opsi-$action.pid
if [ -e "$mypidfile" ]; then
    echo "There is already an linuxmuster-opsi $action process running! Exiting!" | tee -a "$LOGFILE"
    echo "If this is not correct you have to delete $mypidfile!" | tee -a "$LOGFILE"
    exit 0
fi
echo "$mypid" > $mypidfile

# exit if error occurs
bailout(){
 local msg="$1"
 echo "$msg" | tee -a "$LOGFILE"
 rm -f $mypidfile
 header "$action" | tee -a "$LOGFILE"
 exit 1
}

# test if workstationsimport is possible
[ -e "$WIMPORTDATA" ] || (touch "$WIMPORTDATA" || bailout "$WIMPORTDATA not found!")


# upgrade the system in case of prepare
dist_upgrade(){
 apt-get update
 export DEBIAN_FRONTEND=noninteractive
 apt-get -y dist-upgrade
 if ! pkgs_installed; then
  # copy opsi.list
  sed -e "s|@@issue@@|$ISSUE|g" "$OPSILIST_TPL" > "$OPSILIST_TGT"
  # get repo key
  wget -O - "$OPSIKEYURL" | apt-key add - || bailout "Cannot install opsi repository key!"
  # install packages
  apt-get update
  apt-get -y install $OPSIPKGS || bailout "Error on installing opsi packages!"
  echo "Test for installed opsi packages again ..."
  pkgs_installed &> /dev/null || bailout "Not all necessary opsi packages are installed!"
  echo "Ok."
  echo
 fi
}


# source action script
if [ "$action" = "install" ]; then
  (dist_upgrade || RC="1") | tee -a "$LOGFILE"
else
  (. $SHAREDIR/$action.sh || RC="1") | tee -a "$LOGFILE"
fi

# delete pid file
rm -f $mypidfile

# error msg
[ "$RC" = "0" ] || bailout "Finished with error(s)!"
header "$action" | tee -a "$LOGFILE"
echo | tee -a "$LOGFILE"

# reboot
[ -n "$reboot" ] && reboot

exit "$RC"
