#!/bin/bash

exec >> /var/log/debug
exec 2>&1

SED=/bin/sed
MV=/bin/mv
IFCONFIG=/sbin/ifconfig
AWK=/usr/bin/nawk
CUT=/usr/bin/cut
KLOG=/usr/sbin/klog-shell

function log() {
    ${KLOG} -d "kgateway-ifup" -i "GATEWAY" -l ${1} -m "${2}"
}

case "${MODE}" in
    start)
        IPADDR=`${IFCONFIG} ${IFACE} | ${AWK} '/inet addr:/{ print substr( $2, 6 ) }'`
        [ "$IPADDR" = "" ] && {
            log ERROR "Unable to detect ${IFACE} current address"
        } || {
            log INFO "Interface ${IFACE} UP, updating SIP and RTP interface address to ${IPADDR}"
            ${SED} "s/SipInterface:.*/SipInterface: ${IPADDR}/g" < /etc/khomp/config/system.yaml | ${SED} "s/RtpInterface:.*/RtpInterface: ${IPADDR}/g" > /etc/khomp/config/system.yaml.in
            ${MV} /etc/khomp/config/system.yaml.in /etc/khomp/config/system.yaml
        }
    ;;
    stop)
        ## ignored
    ;;
    *)
        log ERROR "Unsupported mode [${MODE}]"
    ;;
esac

exit 0

