#!/bin/sh
##
##  gift -- giftd(8) server control script
##

#   command line check
if [ $# -lt 1 ]; then
    echo "** ERROR: Usage: gift {start|cui|gui|stop}" 1>&2
    exit 1
fi

#   dispatch into command
cmd="$1"
shift
case "$cmd" in
    start )
        #   make sure the giftd(8) user run-time environment exists   
        if [ ! -d $HOME/.giFT ] ; then
            echo "++ creating giFT home directory \"$HOME/.giFT\""
            if ! mkdir $HOME/.giFT; then
                echo "** ERROR: creation of \"$HOME/.giFT\" failed!" 1>&2
                exit 1
            fi
        fi

        #   run giFT server
        echo "++ starting giFT server"
        ( nohup @l_prefix@/bin/giftd \
          </dev/null >/dev/null 2>&1 &
          echo $! >$HOME/.giFT/giftd.pid
        ) >/dev/null 2>&1
        ;;
    cui )
        #   run giFTcurs console user interface
        if [ -f @l_prefix@/bin/giFTcurs ]; then
            echo "++ entering giFT console user interface (giFTcurs)"
           exec @l_prefix@/bin/giFTcurs ${1+"$@"}
        fi
        echo "** ERROR: giFT CUI not available" 1>&2
        exit 1
        ;;
    gui )
        #   run giFToxic/giFTui graphical user interfaces
        if [ -f @l_prefix@/bin/giFToxic ]; then
           echo "++ entering giFT graphical user interface (giFTtoxic)"
           exec @l_prefix@/bin/giFToxic ${1+"$@"}
        elif [ -f @l_prefix@/bin/giFTui ]; then
           echo "++ entering giFT graphical user interface (giFTui)"
           exec @l_prefix@/bin/giFTui ${1+"$@"}
        fi
        echo "** ERROR: giFT GUI not available" 1>&2
        exit 1
        ;;
    stop )
        #   send giFT server the kill command
        echo "++ stopping giFT server"
        if [ -f $HOME/.giFT/giftd.pid ]; then
            kill -TERM `cat $HOME/.giFT/giftd.pid` >/dev/null 2>&1 || true
        fi
        ;;
    * )
        echo "** ERROR: invalid command \"$cmd\"" 1>&2
        exit 1
        ;;
esac

