#!/bin/bash
#
# postinst script for linuxmuster
# thomas@linuxmuster.net
# 22.03.2015
#

set -e

# summary of how this script can be called:
#        * <postinst> `configure' <most-recently-configured-version>
#        * <old-postinst> `abort-upgrade' <new version>
#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
#          <new-version>
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package
#

# Source debconf library.
#. /usr/share/debconf/confmodule

#db_version 2.0


LINBODIR=/var/linbo
DISTCONF=/usr/share/linuxmuster/config/dist.conf

case "$1" in

 configure)

 # init script
 update-rc.d linbo-multicast defaults
 update-rc.d linbo-bittorrent defaults

 # linuxmuster related stuff
 if [ -e "$DISTCONF" ]; then
  . $DISTCONF
  . $HELPERFUNCTIONS
  [ -e "$INSTALLED" ] || exit 0
  LINBOLOGDIR="$LOGDIR/linbo"
 else
  exit 0
 fi

 # config file backup dir
 mkdir -p "$LINBODIR/backup"
 # windows activation stuff
 mkdir -p "$LINBODIR/winact"
 # additional torrents
 mkdir -p "$LINBODIR/torrentadds"

 # backup pxelinux configfiles
 [ -e "$LINBODIR/backup/$(basename $PXECFGDIR)" ] || cp -a "$PXECFGDIR" "$LINBODIR/backup"

 # update serverip in start.conf
 echo "Setting server ip in start.conf.default."
 sed -e "s/^[Ss][Ee][Rr][Vv][Ee][Rr] = \([0-9]\{1,3\}[.]\)\{3\}[0-9]\{1,3\}/Server = $serverip/" -i "$LINBODIR/start.conf"

 # provide the default pxelinux configfile
 conf="$PXECFGDIR/default"
 if [ ! -s "$conf" -o -L "$conf" ]; then
  echo "Creating default pxelinux configfile."
  rm -f "$conf"
  tpl="$LINBOTPLDIR/default.pxe"
  sed -e "s|@@default@@|linbo|
          s|@@kopts@@||g" "$tpl" > "$conf"
 fi

 # pxelinux configfiles and menu.lst update
 for i in $PXECFGDIR/* $LINBODIR/menu.lst.*; do
  [ -f "$i" ] || continue
  grep -q linbofs.gz "$i" && sed -e 's|linbofs.gz|linbofs.lz|g' -i $i
 done

 # start.confs update
 if ls $LINBODIR/start.conf.* &> /dev/null; then

  # iterate over hostgroups
  for i in $(grep -i ^[a-z0-9] /etc/linuxmuster/workstations | awk -F\; '{ print $3 }' | sort -u); do
   conf="$LINBODIR/start.conf.$i"
   [ -e "$conf" ] || continue
   # add new KernelOptions option
   if ! grep -wqi ^kerneloptions "$conf"; then
    echo "Adding KernelOptions to start.conf.$i."
    if [ -s "$PXECFGDIR/$i" ]; then
     kopts="$(grep -i ^append "$PXECFGDIR/$i" | grep -i initrd=linbofs.lz | grep -i quiet | tail -1 | sed -e "s|append ||I" -e "s|splash||I" -e "s|initrd=linbofs.lz||I" -e "s|quiet||I" -e "s|vga=788||I" -e 's/^ *//g' -e 's/ *$//g')"
    else
     kopts=""
    fi
    cp -f "$conf" "$LINBODIR/backup"
    sed -e "/^\[[Ll][Ii][Nn][Bb][Oo]\]/a\
KernelOptions = $kopts" -i "$conf"
   fi
  done
 fi

 # check for linbo mail wrapper in /etc/aliases
 if ! grep -q ^linbo: /etc/aliases; then
  cp /etc/aliases /etc/aliases.dpkg-old
  sed -e "/^root:/a\
linbo: \"\|\/usr\/share\/linuxmuster-linbo\/mail2log.sh\"" -i /etc/aliases
  newaliases
 fi

 # create bittorrent system user if necessary
 if ! grep -qw ^bittorrent /etc/passwd; then
  useradd -r -g nogroup -c "BitTorrent User" -d /var/lib/bittorrent -s /bin/false bittorrent
 fi

 # provide a bittorrent default file if necessary
 if [ ! -s /etc/default/bittorrent -a -s /usr/share/doc/bittorrent/examples/bittorrent.default ]; then
  cp /usr/share/doc/bittorrent/examples/bittorrent.default /etc/default/bittorrent
 fi

 # provide a bittorrent init script if necessary
 if [ ! -s /etc/init.d/bittorrent -a -s /usr/share/doc/bittorrent/examples/bittorrent.init ]; then
  cp /usr/share/doc/bittorrent/examples/bittorrent.init /etc/init.d/bittorrent
  chmod 755 /etc/init.d/bittorrent
  update-rc.d bittorrent defaults
 fi

 # provide a bittorrent logrotate file if necessary
 if [ ! -s /etc/logrotate.d/bittorrent ] || ! grep -q "create 600 bittorrent" /etc/logrotate.d/bittorrent &> /dev/null; then
  cp /usr/share/linuxmuster-linbo/templates/bittorrent.logrotate.d /etc/logrotate.d/bittorrent
 fi
 [ -d /var/log/bittorrent ] || mkdir -p /var/log/bittorrent
 chown bittorrent /var/log/bittorrent -R

 # change owner of logdir to nobody
 [ -d "$LINBOLOGDIR" ] || mkdir -p $LINBOLOGDIR
 chown nobody $LINBOLOGDIR -R

 # create dropbear ssh keys
 if [ ! -s "$SYSCONFDIR/linbo/ssh_host_rsa_key" ]; then
  ssh-keygen -t rsa -N "" -f $SYSCONFDIR/linbo/ssh_host_rsa_key
  /usr/lib/dropbear/dropbearconvert openssh dropbear $SYSCONFDIR/linbo/ssh_host_rsa_key $SYSCONFDIR/linbo/dropbear_rsa_host_key
 fi
 if [ ! -s "$SYSCONFDIR/linbo/ssh_host_dsa_key" ]; then
  ssh-keygen -t dsa -N "" -f $SYSCONFDIR/linbo/ssh_host_dsa_key
  /usr/lib/dropbear/dropbearconvert openssh dropbear $SYSCONFDIR/linbo/ssh_host_dsa_key $SYSCONFDIR/linbo/dropbear_dss_host_key
 fi

 # kill obsolete btdownloadheadless processes from old versions
 if ps ax | grep -v grep | grep -q btdownloadheadless; then
  service linbo-bittorrent stop
  oldpids="$(ps ax | grep -v grep | grep btdownloadheadless | awk '{ print $1 }')"
  echo "Sending obsolete btdownloadheadless processes the TERM signal."
  kill $oldpids
  service linbo-bittorrent start
 fi

 # create atftpd configs if necessary
 conf="/etc/default/atftpd"
 tpl="$LINBOTPLDIR/atftpd.default"
 grep -q "$LINBODIR" "$conf" || update_config=yes
 if [ -n "$update_config" ]; then
  echo "Patching $conf."
  cp "$conf" "$conf".dpkg-bak
  sed -e "s|@@linbodir@@|$LINBODIR|
          s|@@serverip@@|$serverip|" "$tpl" > "$conf"
  tftpd_restart=yes
 fi
 if [ -n "$tftpd_restart" ]; then
  # check for tftp in inetd.conf
  if grep -q ^tftp /etc/inetd.conf; then
   echo "Deactivating tftp in /etc/inetd.conf."
   cp /etc/inetd.conf /etc/inetd.conf.dpkg-bak
   sed -e 's|^tftp|#tftp|' -i /etc/inetd.conf
   service openbsd-inetd restart
  fi
  service atftpd restart
 fi

 # update rsyncd.conf
 conf=/etc/rsyncd.conf
 tpl="$LINBOTPLDIR/$(basename $conf)"
 if ! grep rsync-post-download.sh "$conf" &> /dev/null; then
  echo "Updating $conf."
  [ -e "$conf" ] && cp "$conf" "$conf.dpkg-bak"
  sed -e "s|@@linbodir@@|$LINBODIR|g" "$tpl" > "$conf"
  service rsync restart
 fi

 # remove ALLOWED_DIR from bittorrent config
 conf="/etc/default/bittorrent"
 if grep -q ^ALLOWED_DIR "$conf" &> /dev/null; then
  echo "Removing ALLOWED_DIR from $conf."
  cp "$conf" "$conf.dpkg-bak"
  sed -e 's|^ALLOWED_DIR|#ALLOWED_DIR|g' -i "$conf"
  service bittorrent restart || true
 fi

 # update linbofs.lz
 $LINBOUPDATE
 ;;

 abort-upgrade|abort-remove|abort-deconfigure)

 ;;

 *)
	 echo "postinst called with unknown argument \`$1'" >&2
	 exit 1
 ;;

esac

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

#db_stop

exit 0

