#!/bin/sh
#
# Stop file. You can use it interactively or within some
# /etc/rc?-d/K* stop script.
#
# See the file "license.txt" for information on usage and
# redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
# ----------------------------------------------------------------------
#

#
# Get some platform stuff
#

case `uname -s` in
    Linux)
        ps="ps -ef"
        id=/usr/bin/id
        kill=/bin/kill
        cf=/etc/synoinfo.conf
        if test -f $cf -a -x /bin/get_key_value; then
            case `/bin/get_key_value $cf unique` in
                synology*)
                    . /etc.defaults/VERSION
                    if test $majorversion -le 5; then
                        ps="ps -w"
                    else
                        ps="ps -ef"
                    fi
                ;;
            esac
        fi
    ;;
    Darwin)
        ps="ps -axl"
        id=/usr/bin/id
        kill=/bin/kill
    ;;
    FreeBSD)
        ps="ps -axl"
        id=/usr/bin/id
        kill=/bin/kill
    ;;
    SunOS)
        ps="ps -ef"
        id=/usr/xpg4/bin/id
        kill=/usr/bin/kill
    ;;
    *)
        echo "Unsupported platform `uname -s`" >&2
        exit 130
    ;;
esac

#
# Test for privileged user
#

if test `$id -u` -ne 0; then
    echo "Stopping P5 can only be done as user root!" >&2
    exit 129
fi

#
# Get home directory
#

dir=`dirname "$0"`
cwd=`pwd`
cd "$dir"
LDIR=`pwd`; export LDIR
cd "$cwd"

echo "Stopping P5 application server, be patient..."

stopped=0
srv=lexxsrv
nsd="$LDIR"/bin/nsd

#
# Politely tries to stop the running server
# by signalling its pid.
#

pid=`cat "$LDIR/log/$srv.pid" 2>/dev/null`
if test $? -eq 0; then
    rm "$LDIR/log/$srv.pid"
    case "`uname -s`" in
        Darwin)
            ntry=30
            pl=/Library/LaunchDaemons/com.archiware.presstore.$srv.plist
            launchctl unload -w $pl 2>/dev/null
            rm -f $pl
        ;;
        *)
            ntry=30
            $kill $pid 2>/dev/null
            if test $? -ne 0; then
                ntry=0
            fi
        ;;
    esac
    while test $ntry -gt 0; do
        $kill -s 0 $pid 2>/dev/null
        if test $? -ne 0; then
            stopped=`expr $stopped + 1`
            ntry=1
            echo "$srv pid: $pid (stopped)."
        else
            sleep 1
        fi
        ntry=`expr $ntry - 1`
    done
fi

#
# Kills anything that looks as our server
#

for pid in `$ps | grep "$nsd" | grep -v grep | awk '{print $2;}'`; do
    $kill -s KILL $pid 2>/dev/null
    pids="$pids $pid"
done
if test ! -z "$pids"; then
    sleep 2
    oldp=$pids
    pids=""
    for pid in `$ps | grep "$nsd" | grep -v grep | awk '{print $2;}'`; do
        pids="$pids $pid"
    done
    if test ! -z "$pids"; then
        sleep 5
        pids=""
        for pid in `$ps | grep "$nsd" | grep -v grep | awk '{print $2;}'`; do
            pids="$pids $pid"
        done
    fi
    if test ! -z "$pids"; then
        echo "Could not stop $srv pid(s): $pids" >&2
        exit 133
    fi
    echo "$srv pid: $oldp (stopped)."
    stopped=`expr $stopped + 1`
fi
if test $stopped -eq 0; then
    echo "$srv: no running server found."
fi

exit 0

