#!/bin/bash
#
# wrapper for workstation import scripts
#
# Thomas Schmitt <thomas@linuxmuster.net>
#
# for oss-linbo
# Frank Schütte <fschuett@gymhim.de>
# 15.03.2021
# GPL v3
#
if [ "$1" = "--help" -o  "$1" = "-h" ]
then
	echo 'Usage: /usr/sbin/import_workstations [OPTION]'
	echo 'With this script we import all registered linbo hosts to OSS.'
	echo
	echo 'Options :'
	echo 'Mandatory parameters :'
	echo "		No need for mandatory parameters. (There's no need for parameters for running this script.)"
	echo 'Optional parameters :'
	echo '		-h,   --help         Display this help.'
	echo '		-d,   --description  Display the descriptiont.'
        exit
fi

if [ "$1" = "--description" -o  "$1" = "-d" ]
then
	echo 'NAME:'
	echo '	import_workstations'
	echo 'DESCRIPTION:'
	echo '	With this script we import all registered linbo hosts to OSS.'
	echo 'PARAMETERS:'
	echo '	MANDATORY:'
	echo "		                    : No need for mandatory parameters. (There's no need for parameters for running this script.)"
	echo '	OPTIONAL:'
	echo '		-h,   --help        : Display this help.(type=boolean)'
	echo '		-d,   --description : Display the descriptiont.(type=boolean)'
	exit
fi


# check for import_workstations lockfile
locker=/tmp/.import_workstations.lock
if [ -e "$locker" ]; then
	echo "Caution! Probably there is another import_workstations process running!"
	echo "If this is not the case you can safely remove the lockfile $locker"
	echo "and give import_workstations another try."
	echo "Workstation import is locked! Exiting!"
	exit 1
fi
touch $locker
chmod 400 $locker

# read environment
source /etc/linbo/linbo.conf || exit 1
source $ENVDEFAULTS || exit 1
source $HELPERFUNCTIONS || exit 1
LOGFILE="$LOGDIR/import_workstations.log"
TMPLOG=/var/tmp/import_workstations.log
[ -e "$TMPLOG" ] && rm -f $TMPLOG

echo | tee -a $TMPLOG
echo "#####################################################################" | tee -a $TMPLOG
echo "Starting import workstations session at `date`" | tee -a $TMPLOG
echo | tee -a $TMPLOG

echo "Updating workstations file..." | tee -a $TMPLOG
$LINBOSHAREDIR/linbo_update_workstations.pl | tee -a $TMPLOG
echo "Done." | tee -a $TMPLOG

RC=0

# source import script
echo | tee -a $TMPLOG
. $LINBOSHAREDIR/wimport.sh | tee -a $TMPLOG
RC_ML=${PIPESTATUS[0]}
[ $RC_ML -ne 0 ] && RC=$RC_ML

echo "Writing dhcpd.conf file..." | tee -a $TMPLOG
$LINBOSHAREDIR/linbo_write_dhcpd.pl | tee -a $TMPLOG
echo "Done." | tee -a $TMPLOG

# restart cranix-api service
systemctl restart cranix-api|tee -a $TMPLOG

# restart samba-ad service
systemctl restart samba-ad| tee -a $TMPLOG

# running user scripts
echo | tee -a $TMPLOG
echo "### Running user scripts - Begin ###" | tee -a $TMPLOG
if [ -d "$SYSCONFDIR/import-workstations.d" ]; then
 (
 cd "$SYSCONFDIR/import-workstations.d"
 for i in `find -mindepth 1 -maxdepth 1|sort`; do
   $SYSCONFDIR/import-workstations.d/$i | tee -a $TMPLOG
 done
 )
fi
echo "### Running user scripts - End ###" | tee -a $TMPLOG

echo | tee -a $TMPLOG
echo "Ending import workstations session at `date`" | tee -a $TMPLOG
echo "#####################################################################" | tee -a $TMPLOG

if [ $RC -ne 0 ]; then
	echo | tee -a $TMPLOG
	echo "Finished with error(s)! Please look at $LOGFILE! Detailed output will be mailed to administrator!" | tee -a $TMPLOG
	cat $TMPLOG | mail -s "import_workstations finished with error(s)!" administrator@localhost
fi

# append log
cat $TMPLOG >> $LOGFILE
rm -f $TMPLOG

# delete locker
rm -f $locker

exit $RC

