#!/bin/bash
EXE_DIR=/usr/local/bin
CONF_DIR=/etc
Log_DIR=/var/log
RCDIR=/etc/rc.d/init.d
RC3DIR=/etc/rc.d/rc3.d
RC5DIR=/etc/rc.d/rc5.d

if [ ! -x "$RCDIR" ]; then
 RCDIR=/etc/init.d
 RC3DIR=/etc/rc3.d
 RC5DIR=/etc/rc5.d
fi

function installgnhost
{
        cp gnhostlinux $EXE_DIR/
        chmod u+xr $EXE_DIR/gnhostlinux


        if ! [ -f $CONF_DIR/gnhostlinux.conf ];then
         cp gnhostlinux.conf $CONF_DIR/
        fi
        cp gnhostlinuxd $RCDIR -f
        chmod a+xr $RCDIR/gnhostlinuxd

        rm  $RC3DIR/S99gnhostlinuxd -f
        ln -s $RCDIR/gnhostlinuxd $RC3DIR/S99gnhostlinuxd
        rm  $RC5DIR/S99gnhostlinuxd -f
        ln -s $RCDIR/gnhostlinuxd $RC5DIR/S99gnhostlinuxd
}

function main
{
        case "$1" in
                start)
                        $RC3DIR/S99gnhostlinuxd start
                        ;;
                stop)
                        $RC3DIR/S99gnhostlinuxd stop
                        ;;
                restart)
                        $RC3DIR/S99gnhostlinuxd restart
                        ;;
                install)
                        if [  -f $RC3DIR/S99gnhostlinuxd ];then
                         $RC3DIR/S99gnhostlinuxd stop
                        fi
                        if [ -f $CONF_DIR/gnhostlinux.conf ];then
                         rm -f  $CONF_DIR/gnhostlinux.conf
                        fi
                        installgnhost
                        ;;
                *)
                        echo "Usage: $0 {install|start|stop|restart}"
        esac
}

main $*
