#!/bin/sh
### Copyright 2010 b-rad.cc
### http://wdlxtv.com & http://b-rad.cc & http://nextdimension.cc
### GPLv3 - full license located @ /usr/share/LICENSE
### Stipulations:
### - the full copyright notices in /osd/setup_about.xml must be left intact, as it is the publicly displayed copyright portion of this software as dictated by the GPLv3
### - this entire header must be left intact

case "$1" in
start)
	CONF_VERSION=4
	logfile=/tmp/autores.log
	if [ -d /apps/autores ]; then
  		echo "Loading plugin version ..." | tee -a $logfile
  		cp "$APATH/autores.conf" /tmp/autores.conf >/dev/null 2>&1 && dos2unix /tmp/autores.conf && chmod 666 /tmp/autores.conf
  		if [ ! -f /tmp/autores.conf ]; then
    		echo "ERROR: found no autores.conf file on usb stick" | tee -a $logfile
			logger -t autores -p alert "ERROR: found no autores.conf file on usb stick"
    		exit 0
  		fi
  		cp $AROOT/usr/bin/* /tmp
  		cp $AROOT/usr/lib/* /tmp
 		chmod 777 /tmp/autores.sh
  		chmod 666 /tmp/autores.conf
  		/tmp/patch_dmaosd.sh &
	else
		echo "Loading firmware version ..." | tee -a $logfile
		eval `grep AUTORES /conf/config`
  		eval `grep DEFAULT_ZOOM /conf/config`
  		if [ "$AUTORES" == "ON" -o "$AUTORES" == "on" ]; then
    		AUTORES=1
  		fi
  		if [ "$DEFAULT_ZOOM" == "" ]; then
    		DEFAULT_ZOOM=0
  		fi
  		if [ "$AUTORES" != "1" -a "$DEFAULT_ZOOM" == "0" ]; then
    		echo "AUTORES=OFF and DEFAULT_ZOOM=0 -> plugin disabled" | tee -a $logfile
    		exit 0
  		fi
  		if [ -f /conf/autores.conf ]; then
    		eval `grep CONFIGVER /conf/autores.conf`
    		if [ "$CONFIGVER" != "$CONF_VERSION" ]; then
      			echo "The version of /conf/autores.conf is too old, creating new config file" | tee -a $logfile
      			mv /conf/autores.conf /conf/autores.conf.bak
      			cp /etc/autores.conf /conf
    		fi
  		else
    		cp /etc/autores.conf /conf
  		fi
  		cp /usr/bin/autores.sh /tmp
  		chmod 777 /tmp/autores.sh
  		chmod 666 /conf/autores.conf
  		/usr/bin/patch_dmaosd.sh &
	fi
esac
