#!/bin/sh

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 ser user
adduser --quiet --system --group --disabled-password --shell /bin/false \
--gecos "SIP Express Router" --home /var/run/ser ser || true

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


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

if ! test -e $DEFAULTFILE; then
  echo "Warning: ser postinst script can't find config file $DEFAULTFILE. Configuration aborted."
  exit 0
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 ser/config
if [ "$RET" = "false" ] ; then
  # do not change config file
  echo "Package ser postinstall script: NOT modifying config file $DEFAULTFILE."
else

BACKUP="$DEFAULTFILE.config_bck"

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

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

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

db_get ser/WORKDIR
if test "$RET" != "!" ; then
  fn_config_replace $DEFAULTFILE WORKDIR <<+++
SER_WORKDIR="$RET"
+++
fi

db_get ser/KERNEL_CORE_PID
if test "$RET" != "!" ; then
  fn_config_replace $DEFAULTFILE KERNEL_CORE_PID <<+++
SER_KERNEL_CORE_PID="$RET"
+++
fi

db_get ser/MEMORY
if test "$RET" != "!" ; then
  fn_config_replace $DEFAULTFILE MEMORY <<+++
SER_MEMORY="$RET"
+++
fi

fi # if changing config

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

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


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

exit 0