#!/bin/bash

set -e

# don't do anything when called with other argument than configure 
case "$1" in
  configure)
  ;;  
  abort-upgrade|abort-remove|abort-deconfigure)
    exit 0
  ;;
  *)
    echo "postinst called with unknown argument \$1'" >&2
    exit 1
  ;;
esac

. /usr/share/debconf/confmodule

# add sems user
adduser --quiet --system --group --disabled-password --shell /bin/false \
--gecos "SIP Express Media Server" --home /var/run/sems sems || true

if [ -d /usr/doc -a ! -e /usr/doc/sems -a -d /usr/share/doc/sems ]; then
  ln -sf ../share/doc/sems /usr/doc/sems
fi


# ser defaults file, which will be modified by this script
DEFAULTFILE=/etc/default/sems

if ! test -e $DEFAULTFILE; then
  echo "Warning: sems postinst script can't find config file $DEFAULTFILE. Configuration aborted."
  exit 0
fi

# create voicebox spool directory if 
if [ ! -d /var/spool/voicebox ]; then 
  mkdir -p /var/spool/voicebox/
fi

# make directory writable for sems user
if [ -d /var/spool/voicebox ]; then 
  chown sems /var/spool/voicebox
fi

# ----------------------------------------------------------------------

function fn_config_replace 
{
	if test $# -ne 2; then
	  echo "Error - bad number of input parameters"
	  echo "usage:"
	  echo "fn_config_replace config_file CFG_OPTION_something" 
	  exit 1
	fi

	FILENAME="$1"
	ITEM="$2"

	echo "Changing config option $ITEM."
	OLDFILE="$FILENAME.config_replace_bck"
	cp -f $FILENAME $OLDFILE

	REPLACEMENT="$FILENAME.repl"
	TEMPFILE="$FILENAME.temp"
	TAIL="$FILENAME.tail"

	rm -f $REPLACEMENT
	touch $REPLACEMENT # needed if the input is empty
	while read -r LINE
	do
	  echo "$LINE" >> $REPLACEMENT
	done

	STARTPOS=`nl -b a $FILENAME | grep -w "DEBCONF-$ITEM-START" | sed -e "s/^ *\([0-9]*\).*/\1/g"`
	if [ "$STARTPOS" == "" ]; then
		echo "WARNING: section $ITEM not found"
		return
	fi
		
	ENDPOS=`nl -b a $FILENAME | sed -e "1,${STARTPOS}d" | grep "DEBCONF-$ITEM-END" | head -n 1 | sed -e "s/^ *\([0-9]*\).*/\1/g"`
	if [ "$STARTPOS" == "" ]; then
		echo "WARNING: end of section $ITEM not found"
		return
	fi
	ENDPOS=$(($ENDPOS-1))
	STARTPOS=$(($STARTPOS+1))

	cat $FILENAME | sed -e "1,${ENDPOS}d" > $TAIL
	cat $FILENAME | sed -e "${STARTPOS},\$d" > $TEMPFILE
	cat $REPLACEMENT >> $TEMPFILE
	cat $TAIL >> $TEMPFILE
	rm -f $TAIL
	mv -f $TEMPFILE $FILENAME
}

# pads $1 with as many empty rows as needed until $2 lines are complete
padLines() {
	output="$1"
	needed="$2"
	num=`echo "$output" | wc -l`
	echo "$output"
	moreneeded=$(($needed-$num))
	while (true); do
	if [ $moreneeded -gt 0 ]
	then
		echo ""
		moreneeded=$(($moreneeded-1))
	else
		break
	fi
	done
}

#----------------------------------------------------------------------------


db_get sems/config
if [ "$RET" = "false" ] ; then
  # do not change config file
  echo "Package sems postinstall script: NOT modifying config file $DEFAULTFILE."
else

BACKUP="$DEFAULTFILE.config_bck"

echo "Package sems postinstall script: MODIFYING config file $DEFAULTFILE."
echo "Creating backup copy as $BACKUP"
cp -f $DEFAULTFILE $BACKUP

db_get sems/USER
if test "$RET" != "!" ; then
  fn_config_replace $DEFAULTFILE USER <<+++
SEMS_USER="$RET"
+++
fi

db_get sems/GROUP
if test "$RET" != "!" ; then
  fn_config_replace $DEFAULTFILE GROUP <<+++
SEMS_GROUP="$RET"
+++
fi

#db_get sems/WORKDIR
#if test "$RET" != "!" ; then
#  fn_config_replace $DEFAULTFILE WORKDIR <<+++
#SEMS_WORKDIR="$RET"
#+++
#fi

db_get sems/CREATE_CORE
if test "$RET" != "!" ; then
  fn_config_replace $DEFAULTFILE CREATE_CORE <<+++
SEMS_CREATE_CORE="$RET"
+++
fi

db_get sems/COREDIR
if test "$RET" != "!" ; then
  fn_config_replace $DEFAULTFILE COREDIR <<+++
SEMS_COREDIR="$RET"
+++
fi

fi # if changing config

echo ""
echo "***"
echo "Configuration of sems has finished."
echo ""
echo "To restart it when configuration has changed use '/etc/init.d/sems restart'"
echo ""
echo "To change it's configuration use 'dpkg-reconfigure sems'"
echo "***"
echo ""

echo "Setting sems to start automatically"
/usr/sbin/update-rc.d sems defaults || true


if [ -x "/etc/init.d/sems" ]; then
  if [ -x "`which invoke-rc.d 2>/dev/null`" ]; then
    invoke-rc.d sems restart 3>/dev/null || exit 0
  else
    /etc/init.d/sems restart 3>/dev/null || exit 0
  fi
fi
                                                                

exit 0