#!/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


# ser config file that will be altered by this script, based on debconf values
CONFIGFILE=/etc/ser/ser-oob.cfg 
# ser defaults file, where config filename is set
DEFAULTFILE=/etc/default/ser


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

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


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-oob/CFG_FILE
if test "$RET" != "!" ; then
  echo "Package ser-oob postinstall script: MODIFYING config file $DEFAULTFILE."
  fn_config_replace $DEFAULTFILE CFG_FILE <<+++
SER_CFG_FILE="$RET"
+++
fi

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

BACKUP="$CONFIGFILE.config_bck"

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

db_get ser-oob/SERVERID
if test "$RET" != "!" ; then
  fn_config_replace $CONFIGFILE SERVERID <<+++
server_id=$RET
+++
fi

db_get ser-oob/LISTEN
if test "$RET" != "!" ; then
  if test "$RET" != "" ; then
    echo "$RET"|sed 's/,/\n/g'|awk '{print "listen=\"" $1 "\""}' | \
    fn_config_replace $CONFIGFILE LISTEN
    # enable the replication block
    echo "#" | fn_config_replace $CONFIGFILE REPLICATION1
    echo "#" | fn_config_replace $CONFIGFILE REPLICATION2
    # enable replication listen addr
    echo "listen=224.0.1.75" | fn_config_replace $CONFIGFILE LISTEN_REPL
  else
    echo "#listen=127.0.0.1" | fn_config_replace $CONFIGFILE LISTEN
    # comment out the replication block
    echo "/*" | fn_config_replace $CONFIGFILE REPLICATION1
    echo "*/" | fn_config_replace $CONFIGFILE REPLICATION2
    # disable replication listen addr
    echo "#listen=224.0.1.75" | fn_config_replace $CONFIGFILE LISTEN_REPL
  fi
fi

db_get ser-oob/ADMINADDR
if test "$RET" != "!" ; then
  if test "$RET" != "" ; then
    echo "listen=udp:$RET" | fn_config_replace $CONFIGFILE LISTEN_ADMIN
  else
    echo "#listen=udp:127.0.0.1" | fn_config_replace $CONFIGFILE LISTEN_ADMIN
  fi
fi

db_get ser-oob/SENDADDR
if test "$RET" != "!" ; then
  fn_config_replace $CONFIGFILE REPL_SEND_ADDR <<+++
		force_send_socket(udp:$RET);
+++
fi

db_get ser-oob/DBURL
DBURL="$RET"
if test "$RET" != "!" ; then
  fn_config_replace $CONFIGFILE DBURL <<+++
modparam("speeddial|auth_db|usrloc|acc_db|domain|uri_db|gflags|avp_db|db_ops", "db_url", "$RET")
+++
fi

db_get ser-oob/DBURLACC
if test "$RET" = "" ; then
  RET="$DBURL"
fi    
if test "$RET" != "!" ; then
  fn_config_replace $CONFIGFILE DBURLACC <<+++
modparam("acc_db", "db_url", "$RET")
+++
fi

db_get ser-oob/AUTHSECRET
if test "$RET" != "!" ; then
  fn_config_replace $CONFIGFILE AUTHSECRET <<+++
modparam("auth", "secret", "$RET")
+++
fi

db_get ser-oob/RTTPPROXY
if test "$RET" != "!" ; then
  fn_config_replace $CONFIGFILE RTTPPROXY <<+++
modparam("nathelper", "rtpproxy_sock", "$RET")
+++
fi

db_get ser-oob/RTP_ENABLE
if test "$RET" != "!" ; then
  fn_config_replace $CONFIGFILE RTP_ENABLE <<+++
rtp_proxy.enabled = "$RET" desc "indicates whether the RTP Proxy is enabled or not (0/1/detect)"
+++
fi

db_get ser-oob/NATPING_INTERVAL
if test "$RET" != "!" ; then
  fn_config_replace $CONFIGFILE NATPING_INTERVAL <<+++
modparam("nathelper", "natping_interval", $RET)
+++
fi

db_get ser-oob/DBMODE
if test "$RET" != "!" ; then
  fn_config_replace $CONFIGFILE DBMODE <<+++
modparam("usrloc", "db_mode", $RET)
+++
fi

db_get ser-oob/CREATE_DB
if [ "$RET" = "true" ] ; then
  db_set ser-oob/CREATE_DB false
  db_get ser-oob/MYSQLPASS
  PASS="$RET"
  db_set ser-oob/MYSQLPASS ""
  echo "Creating ser database"
  /usr/sbin/ser_mysql.sh create -q$PASS
fi

# restart ser - after creating db, but before calling ser_ctl
if [ -x "/etc/init.d/ser" ]; then
  if [ -x "`which invoke-rc.d 2>/dev/null`" ]; then
    invoke-rc.d ser restart || echo "Warning: ser restart failed!"
  else
    /etc/init.d/ser restart || echo "Warning: ser restart failed!"
  fi
fi

db_get ser-oob/SERCTL_DOMAIN
if [ "$RET" != "" ] ; then
  DOMAIN="$RET"
  # clear it, to be sure we do not add it more than once
  db_set ser-oob/SERCTL_DOMAIN
  # add the domain using ser_ctl
  echo "Waiting for ser to finish startup..."
  # wait some time, to allow ser start after starting it
  sleep 10
  echo "Calling ser_ctl to add domain"
  ser_ctl domain add $DOMAIN || echo "Warning: ser_ctl failed!"

  db_get ser-oob/SERCTL_USER
  if [ "$RET" != "" ] ; then
    USER="$RET"
    # clear it, to be sure we do not add it more than once
    db_set ser-oob/SERCTL_USER
    # get the user password
    db_get ser-oob/SERCTL_PASS
    PASS="$RET"
    # clear it
    db_set ser-oob/SERCTL_PASS
    # add the user using ser_ctl
    echo "Calling ser_ctl to add user"
    ser_ctl user add $USER@$DOMAIN -p "$PASS" || echo "Waring: ser_ctl failed!"
    echo "Calling ser_ctl to set user rights"
    ser_ctl attrs set $USER@$DOMAIN sw_is_admin=1 sw_is_hostmaster=1 || echo "Waring: ser_ctl failed!"
  fi
fi


fi # if changing config

echo ""
echo "***"
echo "Configuration of ser-oob 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-oob'"
echo "***"
echo ""

exit 0

