#!/bin/sh

if [ -z "$IFS_IFNAME" ] || [ -z "$IFS_RUNDIR" ]; then
    echo "This script should not be run manually." 1>&2
    exit "$IFS_RC_ERROR"
fi

DAEMON_VERBOSITY=

if [ "$IFS_VERBOSE" = 1 ]; then
    DAEMON_VERBOSITY="-v"
    set -x
fi

UDHCPC_BIN="/usr/sbin/udhcpc"
UDHCPC_PIDFILE="${IFS_RUNDIR}/udhcpc.pid"
UDHCPC_CONF="/etc/udhcpc/udhcpc.conf"

start_udhcpc () {
    # save config file checksum
    if [ -f "$UDHCPC_CONF" ]; then
        sha256sum "$UDHCPC_CONF" > "${IFS_RUNDIR}/udhcpc.conf.sha256"
    else
        rm -f "${IFS_RUNDIR}/udhcpc.conf.sha256"
    fi

    # start udhcpc
    start-stop-daemon --start --oknodo $DAEMON_VERBOSITY \
        --startas "$UDHCPC_BIN" --pidfile "$UDHCPC_PIDFILE" \
        -- -b -p "$UDHCPC_PIDFILE" -i "$IFS_IFNAME"
    rc="$?"

    if [ "$rc" = 0 ]; then
        # save current wrapper config
        cp "${IFS_RUNDIR}/wrapper.sh" "${IFS_RUNDIR}/wrapper.sh.active"
    fi

    return $rc
}

stop_udhcpc () {
    # stop udhcpc
    start-stop-daemon --stop --oknodo $DAEMON_VERBOSITY \
            --exec "$UDHCPC_BIN" --pidfile "$UDHCPC_PIDFILE"
    rc="$?"

    # cleanup files
    rm -f "$UDHCPC_PIDFILE"

    return $rc
}

case "$1" in
    start)
        # check if pid file exists
        if [ -f "$UDHCPC_PIDFILE" ]; then
                # check if udhcpc is still running
                if start-stop-daemon --stop --quiet --signal USR1 \
                        --exec "$UDHCPC_BIN" --pidfile "$UDHCPC_PIDFILE"; then

                        # check if wrapper has changed
                        if cmp -s "${IFS_RUNDIR}/wrapper.sh" "${IFS_RUNDIR}/wrapper.sh.active"; then
                            # only check conf file if there is or was one
                            if [ -f "$UDHCPC_CONF" ] || [ -f "${IFS_RUNDIR}/udhcpc.conf.sha256" ]; then
                                # restart if config file has changed
                                if ! sha256sum --check --status "${IFS_RUNDIR}/udhcpc.conf.sha256" 2> /dev/null; then
                                    do_start=r
                                else
                                    do_start=n
                                fi
                            else
                                do_start=n
                            fi
                        else
                            do_start=r
                        fi
                else
                        rm -f "$UDHCPC_PIDFILE"
                        do_start=y
                fi
        else
            do_start=y
        fi

        # nothing todo
        if [ "$do_start" = "n" ]; then
            exit "$IFS_RC_OK"
        fi

        # (re)start udhcpc
        if [ "$do_start" = "r" ]; then
            stop_udhcpc
        fi
        start_udhcpc

        # set return code
        case "$?" in
            0)
                if [ "$do_start" = "r" ]; then
                    exit "$IFS_RC_CHANGED"
                else
                    exit "$IFS_RC_STARTED"
                fi
                ;;
            1)
                exit "$IFS_RC_OK"
                ;;
            *)
                exit "$IFS_RC_ERROR"
                ;;
        esac
        ;;
    check-start)
        # check if pid file exists
        if [ -f "$UDHCPC_PIDFILE" ]; then
                # check if udhcpc is still running
                if start-stop-daemon --stop --quiet --signal 0 \
                        --exec "$UDHCPC_BIN" --pidfile "$UDHCPC_PIDFILE"; then

                        # check if wrapper has changed
                        if cmp -s "${IFS_RUNDIR}/wrapper.sh" "${IFS_RUNDIR}/wrapper.sh.active"; then
                            # only check conf file if there is or was one
                            if [ -f "$UDHCPC_CONF" ] || [ -f "${IFS_RUNDIR}/udhcpc.conf.sha256" ]; then
                                # restart if config file has changed
                                if ! sha256sum --check --status "${IFS_RUNDIR}/udhcpc.conf.sha256" 2> /dev/null; then
                                    exit "$IFS_RC_CHANGED"
                                else
                                    exit "$IFS_RC_OK"
                                fi
                            else
                                exit "$IFS_RC_OK"
                            fi
                        else
                            exit "$IFS_RC_CHANGED"
                        fi
                else
                        rm -f "$UDHCPC_PIDFILE"
                        exit "$IFS_RC_STARTED"
                fi
        else
            exit "$IFS_RC_STARTED"
        fi
        ;;

    stop)
        stop_udhcpc

        # set return code
        case "$?" in
            0)
                exit "$IFS_RC_STOPPED"
                ;;
            1)
                exit "$IFS_RC_OK"
                ;;
            *)
                exit "$IFS_RC_ERROR"
                ;;
        esac
        ;;

    check-stop)
        # check if udhcpc is still running
        if start-stop-daemon --stop --quiet --signal 0 \
                --exec "$UDHCPC_BIN" --pidfile "$UDHCPC_PIDFILE"; then
                exit "$IFS_RC_STOPPED"
        else
                exit "$IFS_RC_OK"
        fi
        ;;

    *)
        logger -s -t ifstate-hook -- "unhandled action '$1'"
        exit 3
        ;;
esac
