#!/bin/sh
CONF=/etc/config/qpkg.conf
QPKG_NAME="Jackett"
QPKG_ROOT=`/sbin/getcfg $QPKG_NAME Install_Path -f ${CONF}`
export QNAP_QPKG=$QPKG_NAME
export QPKG_ROOT
export QPKG_NAME
export SHELL=/bin/sh
export LC_ALL=en_US.UTF-8
export USER=admin
export LANG=en_US.UTF-8
export LC_CTYPE=en_US.UTF-8
export TERM=xterm
export HOME=$QPKG_ROOT
export DIR=$QPKG_ROOT
export DESC=Jackett
export MONO=/opt/Qmono/bin/mono
export PATH=/opt/Qmono/bin:$PATH
export PIDF=/var/run/jackett.pid
pid_check() {
# Check if process is already running
if [[ ! -f "$PIDF" ]]; then
PROCESS_PID=0
else
PROCESS_PID=`cat $PIDF`
fi
}
second_pid_check() {
# Check if process is already running
PROCESS_PID=0
for pid in $(/bin/pidof mono); do
# Try to find if the process
/bin/grep -q "${QPKG_NAME}" /proc/$pid/cmdline
if [ $? -eq 0 ]; then
# Process found, save PID and exit loop
PROCESS_PID=$pid
break
fi
done
}
ShutdownQPKG() { #kills a proces based on a PID in a given PID file
pid_check
if [[ $PROCESS_PID -eq 0 ]]; then
/bin/echo "${QPKG_NAME} is not running?"
exit 1
else
/bin/echo "Shutting down ${QPKG_NAME}... "
i=0
/bin/kill $PROCESS_PID || /bin/rm $PIDF
/bin/echo -n " Waiting for ${QPKG_NAME} to shut down: "
while [ -d /proc/$PROCESS_PID ]; do
/bin/sleep 1
let i+=1
/bin/echo -n "$i, "
if [ $i = 45 ]; then
/bin/echo " Tired of waiting, killing ${QPKG_NAME} now"
/bin/kill -9 $PROCESS_PID || /bin/rm $PIDF
exit 1
fi
done
/bin/echo "Done"
fi
}
do_start() {
pid_check
if [[ $PROCESS_PID -gt 0 ]]; then
second_pid_check
if [[ $PROCESS_PID -gt 0 ]]; then
/bin/echo "${QPKG_NAME} already running?"
exit 1
fi
/bin/rm $PIDF
fi
}
do_stop() {
ShutdownQPKG
sleep 2
if [[ -f "$PIDF" ]]; then
/bin/rm $PIDF
fi
}
case "$1" in
start)
ENABLED=$(/sbin/getcfg $QPKG_NAME Enable -u -d FALSE -f $CONF)
if [ "$ENABLED" != "TRUE" ]; then
echo "$QPKG_NAME is disabled."
exit 1
fi
do_start
/bin/ln -sf $QPKG_ROOT /opt/$QPKG_NAME
cd $QPKG_ROOT/Jackett
/opt/Qmono/bin/cert-sync ${QPKG_ROOT}/cacert.pem
sleep 5
/opt/Qmono/bin/mono --debug JackettConsole.exe --IgnoreSslErrors true &
echo $! > $PIDF
sleep 15
;;
stop)
do_stop
rm -rf /opt/$QPKG_NAME
;;
restart)
$0 stop
$0 start
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
esac
exit 0