#!/bin/bash
#
# wrapper for workstation import scripts
#
# Thomas Schmitt <thomas@linuxmuster.net>
# 26.09.2013
# GPL v3
#

# 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 linuxmuster environment
. /usr/share/linuxmuster/config/dist.conf
. $HELPERFUNCTIONS
[ -d "$WSHOME" ] || mkdir -p $WSHOME
LOGFILE="$LOGDIR/import_workstations.log"
TMPLOG=/var/tmp/import_workstations.log
[ -e "$TMPLOG" ] && rm -f $TMPLOG

# check for sophomorix lockfile
if [ -e "$SOPHOMORIXLOCK" ]; then
	echo "Fatal! Sophomorix lockfile $SOPHOMORIXLOCK detected!"
	rm -f $locker
	exit 1
fi

# check for postgres DB
get_uidnumber $ADMINISTRATOR
if [ -z "$RET" ]; then
	echo "Fatal! PostgreSQL database service is not available! Is postgres running?"
	rm -f $locker
	exit 1
fi

# check for slapd
if ! smbldap-usershow $ADMINISTRATOR &> /dev/null; then
	echo "Fatal! LDAP service is not available! Is slapd running?"
	rm -f $locker
	exit 1
fi

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

# start nscd if not running
if ! ps ax | grep nscd | grep -v grep &> /dev/null; then
	/etc/init.d/nscd start | tee -a $TMPLOG
	echo | tee -a $TMPLOG
fi

RC=0

# backing up workstation data file
echo -n "Backing up $WIMPORTDATA ... " | tee -a $TMPLOG
if backup_file $WIMPORTDATA &> /dev/null; then
 echo "Ok!" | tee -a $TMPLOG
else
 echo "failed!" | tee -a $TMPLOG
fi

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

# restart nscd service
/etc/init.d/nscd restart | tee -a $TMPLOG

# running user scripts
echo | tee -a $TMPLOG
echo "### Running user scripts - Begin ###" | tee -a $TMPLOG
run-parts "$SYSCONFDIR/import_workstations.d" | tee -a $TMPLOG
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

