<?xml version='1.0' standalone='yes'?>
<PLUGIN>

<!--
This Plugin installs and controls SABnzbd+ for UnRaid.
All dependencies are installed as needed and everything is controlable from the webgui
Also this plugin enables easy updates for SABnzbd, since the app doesn't have it's own update routine

Credits:
Benni-Chan: Originally creating the plug-in. Much of his code is still in

WW: Install & Data Directory size, some config page formatting.

I have probably missed some credits here, not intentional but I do not have a perfect memory!
-->


<FILE Name="/boot/packages/python-2.7.5-x86_64-1.txz" Run="upgradepkg --install-new">
<URL>http://slackware.cs.utah.edu/pub/slackware/slackware64-14.1/slackware64/d/python-2.7.5-x86_64-1.txz</URL>
<MD5>9b37a33a41b263c945fddb58949386f0</MD5>
</FILE>
<FILE Name="/boot/packages/python-cheetah-2.4.4-x86_64-1alien.tgz" Run="upgradepkg --install-new">
<URL>http://www.slackware.com/~alien/slackbuilds/python-cheetah/pkg64/14.1/python-cheetah-2.4.4-x86_64-1alien.tgz</URL>
<MD5>f2e4ab251fe05c3c5e03bb5ed20b3b7a</MD5>
</FILE>
<FILE Name="/boot/packages/python-yenc-0.3-x86_64-1alien.tgz" Run="upgradepkg --install-new">
<URL>http://www.slackware.com/~alien/slackbuilds/python-yenc/pkg64/14.1/python-yenc-0.3-x86_64-1alien.tgz</URL>
<MD5>98f9590709a0a77b656d72c33ae4639b</MD5>
</FILE>
<FILE Name="/boot/packages/sqlite-3.7.17-x86_64-1.txz" Run="upgradepkg --install-new">
<URL>http://slackware.cs.utah.edu/pub/slackware/slackware64-14.1/slackware64/ap/sqlite-3.7.17-x86_64-1.txz</URL>
<MD5>730417ec4d8158c6bb5862417107b9f4</MD5>
</FILE>
<FILE Name="/boot/packages/unrar-4.2.4-x86_64-1alien.tgz" Run="upgradepkg --install-new">
<URL>http://www.slackware.com/~alien/slackbuilds/unrar/pkg64/14.1/unrar-4.2.4-x86_64-1alien.tgz</URL>
<MD5>09dbf1a095efaa1213f9a945562fbc2f</MD5>
</FILE>
<FILE Name="/boot/packages/infozip-6.0-x86_64-2.txz" Run="upgradepkg --install-new">
<URL>http://slackware.cs.utah.edu/pub/slackware/slackware64-14.1/slackware64/a/infozip-6.0-x86_64-2.txz</URL>
<MD5>9fc6fcedc67a27dd555e64575847fb7b</MD5>
</FILE>
<FILE Name="/boot/packages/pyopenssl-0.11-x86_64-1alien.tgz" Run="upgradepkg --install-new">
<URL>http://www.slackware.com/~alien/slackbuilds/pyopenssl/pkg64/14.1/pyopenssl-0.11-x86_64-1alien.tgz</URL>
<MD5>a2d3d4631868eac88708450b998ff8d4</MD5>
</FILE>
<FILE Name="/boot/packages/par2cmdline-0.4-x86_64-1alien.tgz" Run="upgradepkg --install-new">
<URL>http://www.slackware.com/~alien/slackbuilds/par2cmdline/pkg64/14.1/par2cmdline-0.4-x86_64-1alien.tgz</URL>
<MD5>e8f68f908496a1944040198a7e014a4c</MD5>
</FILE>
<FILE Name="/boot/config/plugins/sabnzbd/sabnzbd.png">
<URL>--no-check-certificate https://github.com/downloads/W-W/unRAID/sabnzbd.png</URL>
</FILE>
<FILE Name="/boot/config/plugins/images/device_status.png">
<URL>--no-check-certificate https://github.com/downloads/Influencer/UNplugged/device_status.png</URL>
</FILE>
<FILE Name="/boot/config/plugins/images/new_config.png">
<URL>--no-check-certificate https://github.com/downloads/Influencer/UNplugged/new_config.png</URL>
</FILE>
<FILE Name="/boot/config/plugins/images/information.png">
<URL>--no-check-certificate https://github.com/downloads/Influencer/UNplugged/information.png</URL>
</FILE>

<!-- clean up previous install -->
<FILE Name="/tmp/sabnzbd-cleanup" Run="/bin/bash">
<INLINE>
<![CDATA[
[ -d /usr/local/emhttp/plugins/sabnzbd ] && rm -f -R /usr/local/emhttp/plugins/sabnzbd
[ -f /etc/rc.d/rc.sabnzbd ] && rm -f /etc/rc.d/rc.sabnzbd
[ -f /boot/config/plugins/sabnzbd/plgver.txt ] && rm -f /boot/config/plugins/sabnzbd/plgver.txt
rm /tmp/sabnzbd-cleanup
]]>
</INLINE>
</FILE>

<FILE Name="/boot/config/plugins/sabnzbd/sabnzbd.cfg">
<INLINE>
<![CDATA[
# SABnzbd configuration:
SERVICE="disable"
BETA="no"
INSTALLDIR="/usr/local/sabnzbd"
DATADIR="/usr/local/sabnzbd"
RUNAS="nobody"
PLG_STORAGESIZE="yes"
PLG_DATACHECK="yes"
]]>
</INLINE>
</FILE>

<FILE Name="/tmp/checkconfig" Run="/bin/bash">
<INLINE>
<![CDATA[
#!/bin/sh
test=`cat /boot/config/plugins/sabnzbd/sabnzbd.cfg | grep BETA`
if [[ $test = "" ]]; then
	echo "" >> /boot/config/plugins/sabnzbd/sabnzbd.cfg
	echo "BETA=\"no\"" >> /boot/config/plugins/sabnzbd/sabnzbd.cfg
fi
rm /tmp/checkconfig
]]>
</INLINE>
</FILE>

<FILE Name="/boot/config/plugins/sabnzbd/plgver.txt">
<INLINE>
<![CDATA[
3.0.2
]]>
</INLINE>
</FILE>

<FILE Name="/boot/config/plugins/sabnzbd/sabnzbd.ini">
<INLINE>
<![CDATA[
[misc]
port = 8081
]]>
</INLINE>
</FILE>

<FILE Name="/usr/local/emhttp/plugins/webGui/unplugged.page">
<INLINE>
<![CDATA[
Author="Influencer"
Version="1.0.0"
Title="Unplugged PLG"
Menu="Settings"
Type="menu"
]]>
</INLINE>
</FILE>

<FILE Name="/etc/rc.d/rc.sabnzbd" Mode="0770">
<INLINE>
<![CDATA[
#!/bin/sh
# start|stop|restart|enable|disable|install|update|storagesize|datacheck SABnzbd.

sabnzbd_start()
{
	# no-op if not enabled
	if [ $SERVICE != "enable" ]; then
		sed -i "s/"disable"/"enable/"" $CONFIG
	fi
  
	# no-op if already running
	if [ -r /var/run/sabnzbd/sabnzbd-$PORT.pid ]; then
		return
	fi

	# if directory doesn't exist or SABnzbd is not found, install it
	if [[ "$INSTALLDIR" != "" &&  ! -e "$INSTALLDIR/SABnzbd.py" ]]; then
		sabnzbd_install
	fi
    
	if [ ! -r $INSTALLDIR/sabnzbd/api.pyo ]; then
	CMDLINE="sudo -u $RUNAS python2.7 -OO $INSTALLDIR/SABnzbd.py -d -s 0.0.0.0:$PORT --config-file $DATADIR --pid /var/run/sabnzbd/ > /dev/null 2>&1"
	else
	CMDLINE="sudo -u $RUNAS python2.7 $INSTALLDIR/SABnzbd.py -d -s 0.0.0.0:$PORT --config-file $DATADIR --pid /var/run/sabnzbd/ > /dev/null 2>&1"
	fi
	
	echo "Starting SABnzbd: $CMDLINE"
	sleep 1
	$CMDLINE
	
  if [ -f $DATADIR/sabnzbd.ini ]; then
		chmod 777 "$DATADIR/sabnzbd.ini"
		TIMER=0
		HTTPS=`cat "$DATADIR/sabnzbd.ini" | grep 'enable_https' | cut -d' ' -f3`
			if [ $HTTPS = 0 ]; then
				while [ ! -e /var/run/sabnzbd/sabnzbd-$PORT.pid  ]; do
				sleep 1
				let TIMER=$TIMER+1
       				echo -n $TIMER
       				if [ $TIMER -gt 10 ]; then
        			        echo -n "sabnzbd-$PORT.pid not created for some reason"
        			        break
		        	fi
				
				done
			elif [ $HTTPS = 1 ]; then
				PORT=`cat "$DATADIR/sabnzbd.ini" | grep 'https_port' | cut -d' ' -f3`
				while [ ! -e /var/run/sabnzbd/sabnzbd-$PORT.pid  ]; do
				sleep 1
				let TIMER=$TIMER+1
				if [ $TIMER -gt 10 ]; then
					echo -n "sabnzbd-$PORT.pid not created for some reason"
					break
				fi
				done
			fi
	fi
}

sabnzbd_stop()
{
	# no-op if not running
	if [ ! -r /var/run/sabnzbd/sabnzbd-$PORT.pid ]; then
		return
	fi
	
	echo "Stopping SABnzbd..."
	sleep 1
	
	if [ -d $DATADIR ] && [ -f $DATADIR/sabnzbd.ini ]; then
		APIKEY=`grep -w api_key $DATADIR/sabnzbd.ini | cut -d " " -f3`
		IP=$(ifconfig  | grep 'inet addr:' | grep -v '127.0.0.1' | cut -d: -f2 | grep -v '^5' | awk '{ print $1}')
		RES=$(wget -qO - "http://$IP:$PORT/sabnzbd/api?mode=shutdown&apikey=$APIKEY")
		if [[ $RES != "ok" ]]; then
			kill $(cat /var/run/sabnzbd/sabnzbd-$PORT.pid)
		fi
	else
		kill $(cat /var/run/sabnzbd/sabnzbd-$PORT.pid)
	fi
	sleep 3

	if [ -e /var/run/sabnzbd/sabnzbd-$PORT.pid ]; then
		kill -9 $(cat /var/run/sabnzbd/sabnzbd-$PORT.pid )
		rm /var/run/sabnzbd/sabnzbd-$PORT.pid
	fi
	echo "... OK"
	sleep 1
}

sabnzbd_restart()
{
	sabnzbd_stop
	sleep 3
	sabnzbd_start
}

sabnzbd_buttonstart()
{
echo "Enabling Sabnzbd"
	CONFIG="/boot/config/plugins/sabnzbd/sabnzbd.cfg"
	if [ -f $CONFIG ]; then
		sed -i "s/"disable"/"enable/"" $CONFIG	
		sleep 3
		echo "Starting sabnzbd"
	sabnzbd_start
	fi
}
sabnzbd_update()
{
	sabnzbd_stop
	sleep 1
	sabnzbd_install
	sleep 1
	sabnzbd_start
}

write_config()
{
	echo "# SABnzbd configuration:" > /boot/config/plugins/sabnzbd/sabnzbd.cfg
	echo "SERVICE=\"$SERVICE\"" >> /boot/config/plugins/sabnzbd/sabnzbd.cfg
	echo "BETA=\"$BETA\"" >> /boot/config/plugins/sabnzbd/sabnzbd.cfg
	echo "INSTALLDIR=\"$INSTALLDIR\"" >> /boot/config/plugins/sabnzbd/sabnzbd.cfg
	echo "DATADIR=\"$DATADIR\"" >> /boot/config/plugins/sabnzbd/sabnzbd.cfg
	echo "RUNAS=\"$RUNAS\"" >> /boot/config/plugins/sabnzbd/sabnzbd.cfg
	echo "PLG_STORAGESIZE=\"$PLG_STORAGESIZE\"" >> /boot/config/plugins/sabnzbd/sabnzbd.cfg
	echo "PLG_DATACHECK=\"$PLG_DATACHECK\"" >> /boot/config/plugins/sabnzbd/sabnzbd.cfg
	if [ -e $DATADIR/sabnzbd.ini ]; then sed -i 's!port = '"`cat "$DATADIR/sabnzbd.ini" | grep '^port' | awk 'NR==1' | cut -d' ' -f3`"'!port = '"$PORT"'!' "$DATADIR/sabnzbd.ini"; else sed -i 's!port = '"`cat /boot/config/plugins/sabnzbd/sabnzbd.ini | grep '^port' | awk 'NR==1' | cut -d' ' -f3`"'!port = '"$PORT"'!' /boot/config/plugins/sabnzbd/sabnzbd.ini; fi

}

sabnzbd_change_settings()
{
	PORT="$2"
	INSTALLDIR="$3"
	DATADIR="$4"
	
	if [ $DATADIR == "" ]; then
		$DATADIR="$INSTALLDIR"
	fi
	
	if [ $RUNAS != $1 ]; then
		chown -R $1:users "$INSTALLDIR" "$DATADIR"
	fi
	
	RUNAS="$1"
	
	PLG_STORAGESIZE="$5"
	PLG_DATACHECK="$6"
	BETA="$7"
	
	# create SABnzbd user if necessary
	if [[ $RUNAS != "nobody" && $RUNAS != "root" ]]; then
		if ( ! grep -q ^$RUNAS /etc/passwd ); then
	  		useradd -r -g users -d / -s /bin/false $RUNAS &> /dev/null
	  		cp /etc/passwd /etc/shadow /boot/config
		fi
	fi
}

sabnzbd_enable()
{
	SERVICE=enable
	    sabnzbd_stop
		sabnzbd_change_settings $1 $2 $3 $4 $5 $6 $7 $8
		write_config
		sabnzbd_start
}

sabnzbd_disable()
{
	sabnzbd_stop
	SERVICE=disable
	sabnzbd_change_settings $1 $2 $3 $4 $5 $6 $7 $8
	write_config
}

sabnzbd_install()
{ set -x
	echo "Installing latest version of SABnzbd..."

	if [ ! -e "$INSTALLDIR" ]; then 
		mkdir -p "$INSTALLDIR"
	fi
	
	if [ ! -e "$DATADIR" ]; then 
		mkdir -p "$DATADIR"
	fi
	
	if [ ! -e "$DATADIR/sabnzbd.ini" ]; then
		cp /boot/config/plugins/sabnzbd/sabnzbd.ini $DATADIR
	fi	
	
	version=`sabnzbd_newver`
	echo "installing latest Version ($version)"
	
	if [[ $(echo $version | grep "Beta") != "" || $(echo $version | grep "RC") != "" ]]; then
		beta="-beta"
	else
		beta=""
	fi
	
	cd /tmp 
	wget http://downloads.sourceforge.net/project/sabnzbdplus/sabnzbdplus${beta}/${version}/SABnzbd-${version}-src.tar.gz
	tar -xf /tmp/SABnzbd-${version}-src.tar.gz
	cp -Rf /tmp/SABnzbd-${version}/* $INSTALLDIR
	chown -R $RUNAS:users "$INSTALLDIR"		
	rm -Rf /tmp/SABnzbd-${version} /tmp/SABnzbd-${version}-src.tar.gz;

	chown -R $RUNAS:users "$INSTALLDIR" "$DATADIR"
	sleep 3
	echo "... OK"
	sleep 1
}

sabnzbd_storagesize() 
{
	if [ $1 ] && [ $2 ]; then
		if [ -d $1 ]; then
			INST=`du -sh $1 | cut -f1`
		elif [ -f $1 ]; then
			INST=`ls -lah $1 | awk '{ print $5}'`
		fi
	
		if [ -d $2 ]; then
			DAT=`du -sh $2 | cut -f1`
		elif [ -f $2 ]; then
			DAT=`ls -lah $2 | awk '{ print $5}'`
		fi
		
		if [ $1 == $2 ]; then
			echo "<p style="margin-left:10px\;">Storage memory usage: install + data directory: <b>$INST</b></p>"
		else
			echo "<p style="margin-left:10px\;">Storage memory usage: install directory: <b>$INST</b>, data directory: <b>$DAT</b></p>"
		fi
	elif [ $1 ]; then
		if [ -d $1 ]; then
			INST=`du -sh $1 | cut -f1`
		elif [ -f $1 ]; then
			INST=`ls -lah $1 | awk '{ print $5}'`
		fi
		echo "<p style="margin-left:10px\;">Storage memory usage: <b>$INST</b></p>"
	fi
}

sabnzbd_datacheck() 
{
	array=( ramfs proc tempfs sysfs )
	fs=$( stat -f -c '%T' $DATADIR )
	for i in "${array[@]}"
	do
		if [ $i = $fs ]; then
		    echo "<p style="color:red\;margin-left:10px\;margin-right:10px\;"><b>WARNING:</b> Your data directory is not persistent and WILL NOT survive a reboot. Please locate Data Directory on disk for persistency</p>"
			break
		elif [ $i = msdos ]; then
			echo "<p style="color:red\;margin-left:10px\;margin-right:10px\;"><b>WARNING:</b> Your data directory is located on your flash drive. This will decrease your flash drives life span!</p>"
			break
		else
			echo "<p style="color:green\;margin-left:10px\;margin-right:10px\;">Your data will persist after a reboot</p>"
			break
		fi
	done
}

sabnzbd_updateplg()
{
	cd /boot/config/plugins
	[ -f sabnzbd_unplugged.64bit.plg.old ] && rm -f sabnzbd_unplugged.64bit.plg.old
	if [ -f sabnzbd_unplugged.64bit.plg ]; then
	mv sabnzbd_unplugged.64bit.plg sabnzbd_unplugged.64bit.plg.old
	echo "Updating plugin"
	wget -q --no-check-certificate https://github.com/sacretagent/Unplugged_64Bit_Unraid.6/raw/master/sabnzbd_unplugged.64bit.plg
	/usr/local/sbin/installplg /boot/config/plugins/sabnzbd_unplugged.64bit.plg
	else
	echo "Updating plugin"
	wget -q --no-check-certificate https://github.com/sacretagent/Unplugged_64Bit_Unraid.6/raw/master/sabnzbd_unplugged.64bit.plg
	/usr/local/sbin/installplg /boot/config/plugins/sabnzbd_unplugged.64bit.plg
	fi
}
sabnzbd_downgradeplg()
{
	cd /boot/config/plugins
	if [ -f sabnzbd_unplugged.64bit.plg.old ]; then
	rm -f /sabnzbd_unplugged.64bit.plg
	mv sabnzbd_unplugged.64bit.plg.old sabnzbd_unplugged.64bit.plg
	echo "downgrading plugin"
	/usr/local/sbin/installplg /boot/config/plugins/sabnzbd_unplugged.64bit.plg
	fi
}
sabnzbd_newver()
{
stable_version=($(wget -q -O - http://sabnzbdplus.sourceforge.net/version/latest | awk '/0/{gsub(/\015/,"");print}'))
if [[ $BETA = "" || $BETA = "no" ]]; then
	version=$stable_version
elif [[ $BETA = "yes" ]]; then
	beta_version=($(wget -q -O - http://sabnzbdplus.sourceforge.net/version/latest | awk '/Beta/{gsub(/\015/,"");print}'))
	rc_version=($(wget -q -O - http://sabnzbdplus.sourceforge.net/version/latest | awk '/RC/{gsub(/\015/,"");print}'))
	if [[ $beta_version = "" && $rc_version = "" ]]; then
		version=$stable_version #use stable version, if no beta or rc was found
	else
		if [[ $beta_version != "" && $stable_version < $beta_version && $(echo $beta_version | cut -d'.' -f3 | cut -c-1) != $(echo $stable_version | cut -d'.' -f3) ]]; then
			version=$beta_version
		elif [[ $rc_version != "" && $stable_version < $rc_version && $(echo $rc_version | cut -d'.' -f3 | cut -c-1) != $(echo $stable_version | cut -d'.' -f3) ]]; then
			version=$rc_version
		else
			version=$stable_version
		fi
	fi
	
fi
echo $version
}

# read our configuration
source /boot/config/plugins/sabnzbd/sabnzbd.cfg
if [ -e $DATADIR/sabnzbd.ini ]; then
	PORT=`cat "$DATADIR/sabnzbd.ini" | grep '^port' | awk 'NR==1' | cut -d' ' -f3 | tr -d '\r'`
else
	PORT=`cat /boot/config/plugins/sabnzbd/sabnzbd.ini | grep '^port' | awk 'NR==1' | cut -d' ' -f3 | tr -d '\r'`
fi

case "$1" in
	'start')
		sabnzbd_start
	;;
	'stop')
		sabnzbd_stop
	;;
	'restart')
		sabnzbd_restart
	;;
	'enable')
		sabnzbd_enable $2 $3 $4 $5 $6 $7 $8
	;;
	'disable')
		sabnzbd_disable $2 $3 $4 $5 $6 $7 $8
	;;
	'install')
		sabnzbd_install
	;;	
	'update')
		sabnzbd_update
	;;	
	'storagesize')
		sabnzbd_storagesize $2 $3
	;;
	'datacheck')
		sabnzbd_datacheck
	;;
	'updateplg')
		sabnzbd_updateplg
	;;
	'downgradeplg')
		sabnzbd_downgradeplg
	;;
	'buttonstart')
		sabnzbd_buttonstart
	;;
	'newversion')
		sabnzbd_newver
	;;
	*)
		echo "usage $0 start|stop|restart|enable|disable|install|update|storagesize|datacheck|updateplg|downgradeplg"
esac
]]>
</INLINE>
</FILE>

<FILE Name="/usr/local/emhttp/plugins/sabnzbd/sabnzbd.page">
<INLINE>
<![CDATA[
Menu="unplugged"
Icon="sabnzbd.png"
Version="1.1"
Author="Benjamin Waller; Influencer; Wojciech Walaszek"
Type="php"
Title="SABnzbd"
]]>
</INLINE>
</FILE>

<FILE Name="/usr/local/emhttp/plugins/sabnzbd/sabnzbd.png">
<LOCAL>/boot/config/plugins/sabnzbd/sabnzbd.png</LOCAL>
</FILE>

<FILE Name="/usr/local/emhttp/plugins/sabnzbd/device_status.png">
<LOCAL>/boot/config/plugins/images/device_status.png</LOCAL>
</FILE>

<FILE Name="/usr/local/emhttp/plugins/sabnzbd/new_config.png">
<LOCAL>/boot/config/plugins/images/new_config.png</LOCAL>
</FILE>

<FILE Name="/usr/local/emhttp/plugins/sabnzbd/information.png">
<LOCAL>/boot/config/plugins/images/information.png</LOCAL>
</FILE>

<FILE Name="/usr/local/emhttp/plugins/sabnzbd/sabnzbd.php">
<INLINE>
<![CDATA[
<?PHP
$sabnzbd_cfg = parse_ini_file( "/boot/config/plugins/sabnzbd/sabnzbd.cfg" );
$sabnzbd_installed = file_exists( $sabnzbd_cfg["INSTALLDIR"] . "/SABnzbd.py" ) ? "yes" : "no";
$sabnzbd_rollback = file_exists( "boot/config/plugins/sabnzbd_unplugged.plg.old" ) ? "yes" : "no";
$sabnzbd_plgver = shell_exec ( "cat /boot/config/plugins/sabnzbd/plgver.txt" );
$sabnzbd_configfile = $sabnzbd_cfg["DATADIR"] . "/sabnzbd.ini";
if (file_exists("$sabnzbd_configfile"))
$https = trim(shell_exec( "cat \"$sabnzbd_configfile\" | grep 'enable_https =' | cut -d' ' -f3" ));
if ($sabnzbd_installed=="yes") 
{
	if( (file_exists("$sabnzbd_configfile")) && ($https=="1")) {
		$sab_port = trim(shell_exec( "cat \"$sabnzbd_configfile\" | grep 'https_port =' | cut -d' ' -f3" ));
	} else { 
		$sab_port = trim(shell_exec( "cat \"$sabnzbd_configfile\" | grep '^port' | awk 'NR==1' | cut -d' ' -f3" ));
			}
	$sabnzbd_running = file_exists( "/var/run/sabnzbd/sabnzbd-".$sab_port.".pid") ? "yes" : "no";
			
	if ($sabnzbd_cfg[PLG_STORAGESIZE]=="yes") {
		$sabnzbd_datasize = shell_exec ( "/etc/rc.d/rc.sabnzbd storagesize $sabnzbd_cfg[INSTALLDIR] $sabnzbd_cfg[DATADIR]" );
		}
	if ($sabnzbd_cfg[PLG_DATACHECK]=="yes") {
		$sabnzbd_datacheck = shell_exec ( "/etc/rc.d/rc.sabnzbd datacheck" );
		}
	$sabnzbd_curversion = shell_exec ( "python2.7 $sabnzbd_cfg[INSTALLDIR]/SABnzbd.py --version | sed '/SABnzbd.py*/!d' | cut -c12-21" );
	$sabnzbd_newversion = shell_exec ( "/etc/rc.d/rc.sabnzbd newversion" );
} else {
	if (file_exists("/boot/config/plugins/sabnzbd/sabnzbd.ini")) {
		$sab_port = trim(shell_exec( "cat /boot/config/plugins/sabnzbd/sabnzbd.ini | grep '^port' | awk 'NR==1' | cut -d' ' -f3" ));
	}
}
?>

<div style="width: 49%; float:left">
	<div id="title">
		<span class="left">Status:&#32;<img src='/plugins/sabnzbd/device_status.png'>
			<?if ($sabnzbd_installed=="yes"):?>	
				<?if ($sabnzbd_running=="yes"):?>
					<a href="http://<?=$var['NAME'];?>:<?=$sab_port;?>" target="_blank"><span class="green"><b>RUNNING</b></span></a><span style="font-size:12px;"> with version: <b><?=$sabnzbd_curversion?></b></span>
				<?else:?>
					<span class="red"><b>STOPPED</b></span>
				<?endif;?>
			<?else:?>
				<span class="red"><b>NOT INSTALLED</b></span>
			<?endif;?>	
		</span>
	</div>	
	<?if ($sabnzbd_installed=="yes"):?>
		<?if ($sabnzbd_running=="yes"):?>
			<div style="position:relative;float:left;width:50%;text-align:right; margin-bottom:24px">
				<form name="sabnzbd_start_stop" method="POST" action="/update.htm" target="progressFrame">
					<input type="hidden" name="cmd" value="/etc/rc.d/rc.sabnzbd stop">
					<input type="submit" name="runCmd" value="Stop">
				</form>
			</div>
			<div style="position:relative;float:left;width:50%;margin-bottom:24px">
				<form name="sabnzbd_restart" method="POST" action="/update.htm" target="progressFrame">
					<input type="hidden" name="cmd" value="/etc/rc.d/rc.sabnzbd restart">
					<input type="submit" name="runCmd" value="Restart">
				</form>
			</div>
		<?else:?>
			<div style="position:relative;float:left;width:100%;text-align:center;margin-bottom:24px">
				<form name="sabnzbd_start" method="POST" action="/update.htm" target="progressFrame">
					<input type="hidden" name="cmd" value="/etc/rc.d/rc.sabnzbd buttonstart">
					<input type="submit" name="runCmd" value="Start">
				</form>
			</div>
		<?endif;?>
	<?else:?>
		<div style="position:relative;float:left;width:100%;text-align:center;margin-bottom:24px">
			<form name="sabnzbd_install" method="POST" action="/update.htm" target="progressFrame">
				<input type="hidden" name="cmd" value="/etc/rc.d/rc.sabnzbd install">			
				<input type="submit" name="runCmd" value="Install">
			</form>
		</div>
	<?endif;?>
	<? if ($sabnzbd_rollback=="yes"): ?>
	<div style="position:relative;float:left;width:50%;text-align:right;margin-bottom:24px">
			<form name="sabnzbd_update1" method="POST" action="/update.htm" target="progressFrame">
					<input type="hidden" name="cmd" value="/etc/rc.d/rc.sabnzbd updateplg">
					<input type="submit" name="runCmd" value="Update PLG">
			</form>
	</div>
	<div style="position:relative;float:left;width:50%;text-align:left;margin-bottom:24px">
		<form name="sabnzbd_downgrade" method="POST" action="/update.htm" target="progressFrame">
					<input type="hidden" name="cmd" value="/etc/rc.d/rc.sabnzbd downgradeplg">
					<input type="submit" name="runCmd" value="Downgrade PLG">
			</form>
	</div>
	<?else:?>
	<div style="position:relative;float:left;width:100%;text-align:center;margin-bottom:24px">
			<form name="sabnzbd_update" method="POST" action="/update.htm" target="progressFrame">
				<input type="hidden" name="cmd" value="/etc/rc.d/rc.sabnzbd updateplg">			
				<input type="submit" name="runCmd" value="Update PLG">
			</form>
		</div>
	<?endif;?><br/>
	<? if ($sabnzbd_installed=="yes"): ?>	
		<center><hr size="3" width="50%" color="grey"></center>
		<? if ($sabnzbd_cfg[PLG_STORAGESIZE]=="yes"): ?>
			<?=$sabnzbd_datasize?>
		<? endif; ?>
		<? if ($sabnzbd_cfg[PLG_DATACHECK]=="yes"): ?>
			<?=$sabnzbd_datacheck?>
		<? endif; ?>
		<p style="margin-left:10px;"><b>Plug-in Version: <?=$sabnzbd_plgver;?></b></p>
		<? if (strcmp($sabnzbd_curversion, $sabnzbd_newversion)!=0): ?>
			<center>
				<form name="sabnzbd_settings" method="POST" action="/update.htm" target="progressFrame">
					<input type="hidden" name="cmd" value="/etc/rc.d/rc.sabnzbd update">
					<p style="color:blue;margin-left:10px;">Update available to version: <b><?=$sabnzbd_newversion;?></b></p>
					<hr size="3" width="50%" color="grey" style="margin-bottom:15px">
					<input type="submit" name="runCmd" value="Update">
				</form>	 
			</center>
		<? endif; ?>
	<? endif; ?>
</div>
<div style="width: 49%; float:right">
	<div id="title">
		<span class="left">Configuration:&#32;<img src='/plugins/sabnzbd/new_config.png'></span>
	</div>
	<form name="sabnzbd_settings" method="POST" action="/update.htm" target="progressFrame">
		<input type="hidden" name="cmd" value="/etc/rc.d/rc.sabnzbd">
		<table class="settings">
			<tr>
				<td>Enable SABnzbd:</td>
				<td>
					<select name="arg1" size="1">
						<?=mk_option($sabnzbd_cfg['SERVICE'], "disable", "No");?>
						<?=mk_option($sabnzbd_cfg['SERVICE'], "enable", "Yes");?>
					</select>
				</td>
			</tr>
			<tr>
				<td>Install directory:</td>
				<td><input type="text" name="arg4" maxlength="60" value="<?=$sabnzbd_cfg['INSTALLDIR'];?>"></td>
			</tr>
			<tr>
				<td>Install Beta:</td>
				<td>
					<select name="arg8" size="1">
						<?=mk_option($sabnzbd_cfg['BETA'], "no", "No");?>
						<?=mk_option($sabnzbd_cfg['BETA'], "yes", "Yes");?>
					</select>
				</td>
			</tr>
			<tr>
				<td>Data directory:</td>
				<td><input type="checkbox" name="use_data" <?=($sabnzbd_cfg['DATADIR']!=$sabnzbd_cfg['INSTALLDIR'])?"checked=\"checked\"":"";?> onChange="checkDATADIR(this.form);"> <input type="text" name="arg5" style="width:86%" maxlength="60" value="<?=$sabnzbd_cfg['DATADIR'];?>"></td>
			</tr>
			<tr>
				<td>Port:</td>
				<td><input type="text" name="arg3" maxlength="40" value="<?=$sab_port;?>"></td>
			</tr>
			<tr>
				<td>Run as user:</td>
				<td>
					<select name="runas" size="1" onChange="checkUSER(this.form);">
						<?=mk_option($sabnzbd_cfg['RUNAS'], "nobody", "nobody");?>
						<?=mk_option($sabnzbd_cfg['RUNAS'], "root", "root");?>
						<option value='other'<?=($sabnzbd_cfg['RUNAS'] != "root" && $sabnzbd_cfg['RUNAS'] != "nobody")?" selected=yes":"" ;?>>other</option>
					</select>
					<input type="hidden" name="arg2" style="width:66%" maxlength="40" value="<?=$sabnzbd_cfg['RUNAS'];?>">
				</td>
			</tr>
			<tr><td>---</td></tr>
			<tr>
				<td>Show storage memory usage:</td>
				<td>
					<select name="storagesize" size="1" onChange="checkSTORAGE(this.form);">
						<?=mk_option($sabnzbd_cfg['PLG_STORAGESIZE'], "yes", "Yes");?>
						<?=mk_option($sabnzbd_cfg['PLG_STORAGESIZE'], "no", "No");?>
					</select>
					<input type="hidden" name="arg6" value="<?=$sabnzbd_cfg['PLG_STORAGESIZE'];?>">
				</td>
			</tr>
			<tr>
				<td>Show data persistency information:</td>
				<td>
					<select name="datacheck" size="1" onChange="checkDATA(this.form);">
						<?=mk_option($sabnzbd_cfg['PLG_DATACHECK'], "yes", "Yes");?>
						<?=mk_option($sabnzbd_cfg['PLG_DATACHECK'], "no", "No");?>
					</select>
					<input type="hidden" name="arg7" value="<?=$sabnzbd_cfg['PLG_DATACHECK'];?>">
				</td>
			</tr>
		</table>
		<div align="center">
			<hr size="3" align="center" width="75%" color="grey" style="margin-top:20px;margin-bottom:18px" >
			<input type="submit" name="runCmd" value="Apply" style="margin-bottom:35px">
			<button type="button" style="margin-bottom:35px" onClick="done();">Done</button>
		</div>
	</form>
	<br />
</div>

<script type="text/javascript">
function checkUSER(form)
{
	if (form.runas.selectedIndex < 2 )
	{
		form.arg2.value = form.runas.options[form.runas.selectedIndex].value;
		form.arg2.type = "hidden";
	}
	else
	{
		form.arg2.value = "<?=$sabnzbd_cfg['RUNAS'];?>";
		form.arg2.type = "text";
	}
}

function checkDATADIR(form)
{
	if (form.use_data.checked == false )
	{
		form.arg5.value = form.arg4.value;
		form.arg5.type = "hidden";
	}
	else
	{
		form.arg5.value = "<?=$sabnzbd_cfg['DATADIR'];?>";
		form.arg5.type = "text";
	}
}

function checkSTORAGE(form)
{
	form.arg6.value = form.storagesize.options[form.storagesize.selectedIndex].value;
}

function checkDATA(form)
{
	form.arg7.value = form.datacheck.options[form.datacheck.selectedIndex].value;
}

checkUSER(document.sabnzbd_settings);
checkDATADIR(document.sabnzbd_settings);
checkSTORAGE(document.sabnzbd_settings);
checkDATA(document.sabnzbd_settings);
</script>
]]>
</INLINE>
</FILE>

<!-- event handler -->
<FILE Name="/usr/local/emhttp/plugins/sabnzbd/event/disks_mounted" Mode="0770">
<INLINE>
<![CDATA[
#!/bin/bash
source /boot/config/plugins/sabnzbd/sabnzbd.cfg
if [ $SERVICE = enable ]; then
/etc/rc.d/rc.sabnzbd start
fi
]]>
</INLINE>
</FILE>


<!-- event handler -->
<FILE Name="/usr/local/emhttp/plugins/sabnzbd/event/unmounting_disks" Mode="0770">
<INLINE>
<![CDATA[
#!/bin/bash
/etc/rc.d/rc.sabnzbd stop
]]>
</INLINE>
</FILE>

<FILE Name="/tmp/sabnzbd-install" Run="/bin/bash">
<INLINE>
<![CDATA[
# include our config vars
source /boot/config/plugins/sabnzbd/sabnzbd.cfg

# create sabnzbd-writable directory for pid file
if [ ! -e /var/run/sabnzbd ]; then
	mkdir /var/run/sabnzbd
	chown $RUNAS:users /var/run/sabnzbd
	chmod 0777 /var/run/sabnzbd
fi
rm /tmp/sabnzbd-install
]]>
</INLINE>
</FILE>

<FILE Name="/var/log/plugins/sabnzbd">
<INLINE>
<![CDATA[

]]>
</INLINE>
</FILE>

</PLUGIN>