#!/bin/sh
QPKG_NAME="SABnzbdplus"
QPKG_DIR=$(/sbin/getcfg $QPKG_NAME Install_Path -f /etc/config/qpkg.conf) #root of the qpkg dir
LOG_DIR=$(/sbin/getcfg misc log_dir -f $QPKG_DIR/Config/sabnzbd.ini) #log dir where the TAG files are stored
DAEMON="/opt/bin/python"
DAEMON_PACKAGE="$QPKG_DIR/Repository/lib/python"
DAEMON_OPTS=" SABnzbd.py -f /${QPKG_DIR}/Config/sabnzbd.ini --browser 0 --daemon --pid /tmp" #watch out that --pid only sets the path to the pidfile, not the name
PID_NAME="/tmp/sabnzbd" #am a bit lazy to code the pidpath in a variable too, so change path here to path set by --pid in the line above
PID_FILE="$PID_NAME-$(/sbin/getcfg Misc port -f $QPKG_DIR/Config/sabnzbd.ini).pid" #sabnzbd itself names it's pidfile to sabnzbd-<portnr-listening-on> can be http or https port
QDOWNLOAD=$(/sbin/getcfg SHARE_DEF defDownload -d Qdownload -f /etc/config/def_share.info)
#The url to the git repository we're going to install
GIT_URL=git://github.com/clinton-hall/nzbToMedia.git
GIT_URL1=
http://github.com/clinton-hall/nzbToMedia.git
#Determin Arch
ver="none"
if /bin/uname -m | grep "armv5tejl"; then ver="arm"; fi
if /bin/uname -m | grep "armv5tel"; then ver="arm"; fi
if /bin/uname -m | grep "i686"; then ver="x86"; fi
if /bin/uname -m | grep "x86_64"; then ver="x86"; fi
if /bin/uname -m | grep "armv7l"; then ver="x31"; fi
arch="$(/bin/uname -m)"
[ $ver = "none" ] && err_log "Could not determine architecture $arch"
if [ -z "$LANG" ]; then
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8
export LC_CTYPE=en_US.UTF-8
fi
export PYTHONPATH=$DAEMON_PACKAGE
export PATH=${QPKG_DIR}/${ver}/bin-utils:/Apps/bin:$PATH
export LD_LIBRARY_PATH=${QPKG_DIR}/${ver}/lib:/Apps/lib:$LD_LIBRARY_PATH
CheckForUpgrade() { #checks if there is a new sab version available and installs it
if [ ! -d ${QPKG_DIR}/sabnzbd/.git ]; then
git clone
https://github.com/sabnzbd/sabnzbd.git ${QPKG_DIR}/sabnzbd || git clone git://github.com/sabnzbd/sabnzbd.git ${QPKG_DIR}/sabnzbd
cd ${QPKG_DIR}/sabnzbd
git checkout master
fi
cd ${QPKG_DIR}/sabnzbd
git pull
}
UpdateScripts() {
cd /share/$QDOWNLOAD/nzbToMedia
if [ ! -d /share/$QDOWNLOAD/nzbToMedia/.git ]; then
#git clone the qpkg in a temp dir ($$ returns the pid we are running under, should be random enough)
git clone $GIT_URL /share/$QDOWNLOAD/nzbToMedia || git clone $GIT_URL1 /share/$QDOWNLOAD/nzbToMedia
fi
#check for success else log error and breakoff install
if [ ! -d /share/$QDOWNLOAD/nzbToMedia/.git ]; then
/bin/echo "Could not git clone $GIT_URL"
fi
git reset --hard
git pull
sleep 10
}
CheckQpkgEnabled() { #Is the QPKG enabled? if not exit the script
if [ $(/sbin/getcfg ${QPKG_NAME} Enable -u -d FALSE -f /etc/config/qpkg.conf) = UNKNOWN ]; then
/sbin/setcfg ${QPKG_NAME} Enable TRUE -f /etc/config/qpkg.conf
elif [ $(/sbin/getcfg ${QPKG_NAME} Enable -u -d FALSE -f /etc/config/qpkg.conf) != TRUE ]; then
/bin/echo "${QPKG_NAME} is disabled."
exit 1
fi
if [ `/sbin/getcfg "git" Enable -u -d FALSE -f /etc/config/qpkg.conf` = UNKNOWN ]; then
/sbin/setcfg "git" Enable TRUE -f /etc/config/qpkg.conf
elif [ `/sbin/getcfg "git" Enable -u -d FALSE -f /etc/config/qpkg.conf` != TRUE ]; then
echo "git is disabled."
exit 1
fi
if [ `/sbin/getcfg "Python" Enable -u -d FALSE -f /etc/config/qpkg.conf` = UNKNOWN ]; then
/sbin/setcfg "Python" Enable TRUE -f /etc/config/qpkg.conf
elif [ `/sbin/getcfg "Python" Enable -u -d FALSE -f /etc/config/qpkg.conf` != TRUE ]; then
echo "Python is disabled."
exit 1
fi
[ -x /Apps/bin/git ] || /etc/init.d/git.sh restart && sleep 2
[ -x $DAEMON ] || /etc/init.d/python.sh restart && sleep 2
}
CheckForGit(){ #Does git exist?
/bin/echo -n " Checking for git..."
if [ ! -f /Apps/bin/git ]; then
if [ -x /etc/init.d/git.sh ]; then
/bin/echo " Starting git..."
/etc/init.d/git.sh start
sleep 2
else #catch all
/bin/echo " No git qpkg found, please install it"
/sbin/write_log "Failed to start $QPKG_NAME, no git found." 1
exit 1
fi
else
/bin/echo " Found!"
fi
}
CheckQpkgRunning() { #Is the QPKG already running? if so, exit the script
#exit if the file exists, else grab the https port and recheck
[ -f $PID_FILE ] && exit 1 #Normal HTTP port
PID_FILE="$PID_NAME-$(/sbin/getcfg Misc https_port -f $QPKG_DIR/Config/sabnzbd.ini).pid"
[ -f $PID_FILE ] && exit 1 #HTTPS port
#ok, we survived so sabnzbd should not be running
}
CheckEnvironment() { #Checks all (Download)paths, and links some libraries
/bin/echo " Checking environment"
#we assume the downloadshare will allways be used so it must be available
if [ ! -d $(/sbin/getcfg ${QDOWNLOAD} path -f /etc/config/smb.conf -d /ERROR) ] || [ ! -d "/share/${QDOWNLOAD}" ]; then
echo " ${QDOWNLOAD} is not found, aborting qpkg start."
exit 1
fi
/bin/echo -n " Checking if all configured directories exist"
#read all config directories from the config file
LOG_DIR=$(/sbin/getcfg misc log_dir -f $QPKG_DIR/Config/sabnzbd.ini)
DIRSCAN_dir=$(/sbin/getcfg misc dirscan_dir -f $QPKG_DIR/Config/sabnzbd.ini)
NZB_backup_dir=$(/sbin/getcfg misc nzb_backup_dir -f $QPKG_DIR/Config/sabnzbd.ini)
DOWNLOAD_dir=$(/sbin/getcfg misc download_dir -f $QPKG_DIR/Config/sabnzbd.ini)
CACHE_dir=$(/sbin/getcfg misc cache_dir -f $QPKG_DIR/Config/sabnzbd.ini)
COMPLETE_dir=$(/sbin/getcfg misc complete_dir -f $QPKG_DIR/Config/sabnzbd.ini)
SCRIPT_dir=$(/sbin/getcfg misc script_dir -f $QPKG_DIR/Config/sabnzbd.ini)
/bin/echo -n "."
#Walk the directories, set rights and create if needed
[ -d ${LOG_DIR} ] || /bin/mkdir -p ${LOG_DIR}
/bin/chmod 777 ${LOG_DIR}
/bin/echo -n "."
[ -d ${DIRSCAN_dir} ] || /bin/mkdir -p ${DIRSCAN_dir}
/bin/chmod 777 ${DIRSCAN_dir}
/bin/echo -n "."
[ -d ${NZB_backup_dir} ] || /bin/mkdir -p ${NZB_backup_dir}
/bin/chmod 777 ${NZB_backup_dir}
/bin/echo -n "."
[ -d ${DOWNLOAD_dir} ]|| /bin/mkdir -p ${DOWNLOAD_dir}
/bin/chmod 777 ${DOWNLOAD_dir}
/bin/echo -n "."
[ -d ${CACHE_dir} ] || /bin/mkdir -p ${CACHE_dir}
/bin/chmod 777 ${CACHE_dir}
/bin/echo -n "."
[ -d ${COMPLETE_dir} ] || /bin/mkdir -p ${COMPLETE_dir}
/bin/chmod 777 ${COMPLETE_dir}
/bin/echo -n "."
[ -d ${SCRIPTS_dir} ] || /bin/mkdir -p ${SCRIPTS_dir}
/bin/chmod 777 ${SCRIPT_dir}
/bin/echo " Done"
#Create symbolic links for required bin-utils and library files
/bin/echo -n " Creating Symbolic links for required utils"
/bin/echo -n "."
#copy the scripts over
SCRIPTS_FOLDER="/share/$QDOWNLOAD/nzbToMedia"
[ ! -f $SCRIPTS_FOLDER/CharTranslator.py ] && /bin/ln -sf "${QPKG_DIR}/Repository/scripts/CharTranslator.py" "${SCRIPTS_FOLDER}/CharTranslator.py"
/bin/chmod 777 ${QPKG_DIR}/${ver}/bin-utils/par2
/bin/chmod 777 ${QPKG_DIR}/${ver}/bin-utils/ionice
/bin/chmod 777 ${QPKG_DIR}/${ver}/bin-utils/nice
/bin/chmod 777 ${QPKG_DIR}/${ver}/bin-utils/unrar
/bin/echo " Done"
}
ConfigPython(){ #checks if the daemon exists and will link /usr/bin/python to it
#python dependency checking
if [ ! -x $DAEMON ]; then
/sbin/write_log "Failed to start $QPKG_NAME, $DAEMON was not found. Please re-install the Pythton qpkg." 1
exit 1
fi
[ -d $QPKG_DIR/Repository/lib ] || /bin/mkdir $QPKG_DIR/Repository/lib
[ -d $QPKG_DIR/Repository/lib/python ] || ln -sf $QPKG_DIR/${ver}/python $QPKG_DIR/Repository/lib/python
[ -d $QPKG_DIR/Repository/bin ] || ln -sf $QPKG_DIR/${ver}/bin $QPKG_DIR/Repository/bin
if [ $ver = "x31" ]; then
PY_DIR=$(/sbin/getcfg Python Install_Path -f /etc/config/qpkg.conf)
[ -f ${PY_DIR}/lib/python2.7/lib-dynload/_ctypes.so ] || cp $QPKG_DIR/${ver}/lib/_ctypes.so ${PY_DIR}/lib/python2.7/lib-dynload/_ctypes.so
[ -f ${PY_DIR}/lib/python2.7/lib-dynload/_sqlite3.so ] || cp $QPKG_DIR/${ver}/lib/_sqlite3.so ${PY_DIR}/lib/python2.7/lib-dynload/_sqlite3.so
[ -f ${PY_DIR}/lib/python2.7/lib-dynload/_ssl.so ] || cp $QPKG_DIR/${ver}/lib/_ssl.so ${PY_DIR}/lib/python2.7/lib-dynload/_ssl.so
[ -f ${PY_DIR}/lib/python2.7/lib-dynload/zlib.so ] || cp $QPKG_DIR/${ver}/lib/zlib.so ${PY_DIR}/lib/python2.7/lib-dynload/zlib.so
fi
ln -sf $DAEMON ${QPKG_DIR}/${ver}/bin-utils/python2 && ln -sf $DAEMON ${QPKG_DIR}/${ver}/bin-utils/python && ln -sf $DAEMON ${QPKG_DIR}/${ver}/bin-utils/python2.7
}
AddCron(){ #adds the once a day restart of sabnzbd to the crontab if tagged
#if not yet added to crontab, and feature is tagged, add a restart schedule for SABnzbdplus, every day
[ -f $LOG_DIR/AUTO_RESTART ] && /bin/echo -n " AUTO_RESTART tag found. Checking if crontab needs to be altered..."
if [ -z "$(/bin/cat /etc/config/crontab | /bin/grep "sabnzbd.sh restart")" ] && [ -f $LOG_DIR/AUTO_RESTART ]; then
/bin/echo -n " Yes, "
/bin/echo "5 3 * * * /etc/init.d/sabnzbd.sh restart" >> /etc/config/crontab
#restart crontab
/usr/bin/crontab /etc/config/crontab
fi
/bin/echo "Done"
}
RemoveCron(){ #removes the once a day restart of sabnzbd to the crontab, if found
if [ -n "$(/bin/cat /etc/config/crontab | /bin/grep "sabnzbd.sh restart")" ]; then
/bin/echo -n "Removing restart from crontab... "
/bin/sed -i '/sabnzbd.sh restart/d' /etc/config/crontab
#restart crontab
/usr/bin/crontab /etc/config/crontab
/bin/echo " Done"
fi
}
StartQpkg(){ #starts the QPKG
/bin/echo -n "Starting $QPKG_NAME"
cd ${QPKG_DIR}/sabnzbd
PATH=${PATH} ${DAEMON} ${DAEMON_OPTS}
/bin/sleep 3
/bin/echo " Done ($(/bin/date))"
}
ShutdownQPKG() { #kills a proces based on a PID in a given PID file
if [ -f $PID_FILE ]; then
#grab pid from pid file
Pid=$(/bin/cat $PID_FILE)
i=0
/bin/kill $Pid
/bin/echo -n " Waiting for ${QPKG_NAME} to shut down: "
while [ -d /proc/$Pid ]; do
sleep 1
let i+=1
/bin/echo -n "$i, "
if [ $i = 45 ]; then
/bin/echo -n " Tired of waiting, killing ${QPKG_NAME} now"
/bin/kill -9 $Pid
/bin/rm -f $PID_FILE
#clean up stuff we've linked to
/bin/echo " Done ($(/bin/date))"
exit 1
fi
done
/bin/rm -f $PID_FILE
#clean up stuff we've (might have) linked to
/bin/echo "Done ($(/bin/date))"
else
/bin/echo "${QPKG_NAME} is not running? ($(/bin/date))"
fi
}
case "$1" in
start)
{
/bin/echo "Performing SABnzbdplus prestartup checks at $(/bin/date)..."
CheckQpkgEnabled #Check if the QPKG is enabled, else exit
CheckQpkgRunning #Check if the QPKG is not running, else exit
CheckForGit #Check for git, start qpkg if needed
ConfigPython
UpdateScripts #Ensure scripts are up-to-date
CheckEnvironment #Check for all needed environment thingies
AddCron #Add a restart (thus auto upgrade check) to the crontab at 03:05 every night, if tagged
CheckForUpgrade #Do an upgrade check if wanted
StartQpkg #Start sabnzbd... (finaly
)
} 2>&1 | /usr/bin/tee $LOG_DIR/$QPKG_NAME-QPKG.log #Dump all output into the logfile
;;
stop)
{
/bin/echo "Shutting down ${QPKG_NAME} at $(/bin/date)... "
RemoveCron #removes the restart script if found. (should always remove regardless if tag exists or not)
/bin/touch $LOG_DIR/$QPKG_NAME-QPKG.log #make sure the logfile exists
#check if the pidfile exists, else grab the https port and fire shutdown routine
[ -f $PID_FILE ] || PID_FILE="$PID_NAME-$(/sbin/getcfg Misc https_port -f $QPKG_DIR/Config/sabnzbd.ini).pid"
ShutdownQPKG #shutdown the package, this routine also removes the cronjob
} 2>&1 | /usr/bin/tee -a $LOG_DIR/$QPKG_NAME-QPKG.log #append all output to the logfile
;;
restart)
$0 stop
$0 start
;;
*)
echo "Usage: $0 {start|stop|restart}"
esac