#
# thomas@linuxmuster.net
# 01.07.2016
# GPL v3
#

echo "### 99_start-services postpatch"

# repair permissions for root's scheduled cron jobs
chown root:crontab /var/spool/cron/crontabs/root
chmod 600 /var/spool/cron/crontabs/root

# waiting for firewall to come up 
c=0
maxwait=300
while [ $c -lt $maxwait ]; do

 if ping -c 2 $ipcopip &> /dev/null; then
  fwup="yes"
  break
 fi
 
 let c+=5
 echo "Waiting for Firewall, $c of $maxwait seconds ..."
 sleep 5

done

# only if internet connection
if [ -n "$fwup" ]; then
 if ping -c 2 www.linuxmuster.net &> /dev/null; then
  # set time
  ntpdate pool.ntp.org
  hwclock --utc
 fi
fi

# set installed flag
touch $INSTALLED

# reconfigure linuxmuster-pkgs finally
pkgs="base linbo schulkonsole"
[ "$fwconfig" != "custom" -a -n "$fwup" ] && pkgs="$pkgs $fwconfig"
for i in $pkgs; do
 dpkg-reconfigure linuxmuster-$i
done

# apply workstation data if internal net has changed
if [ "$1" = "--modify" -a "$internsub" != "$internsub_old" ]; then
 backup_file $WIMPORTDATA
 n=$internsub; o=$internsub_old
 while [ $n -lt $maxsub ]; do
  sed -e "s/;10.$o./;10.$n./g" -i $WIMPORTDATA
  let n+=1
  let o+=1
 done
 rm -f $LINBODIR/*.torrent
 import_printers
fi

# start services not yet started
for i in dbus openbsd-inetd atd cron openntpd linuxmuster-base isc-dhcp-server; do
 service "$i" stop
 service "$i" start
done

# import_workstations
if [ "$1" = "--modify" ]; then
 import_workstations
 service linbo-multicast restart
 service linbo-bittorrent restart
fi

# update linbofs on initial setup or if certificates where renewed
[ "$update_certs" = "yes" -o "$1" = "--first" ] && update-linbofs
