#!/bin/bash
#

AUTOUPDATES="`/bin/grep \"updates\" /var/cpanel/cleanbackups/cleanbackups.config | /bin/cut -d= -f2`"
UPDATETREE="`/bin/grep \"updatetree\" /var/cpanel/cleanbackups/cleanbackups.config | /bin/cut -d= -f2`"

if [ "$CRON" != "" ]; then
   if [ "$AUTOUPDATES" != "enabled" ]; then
      exit
   fi
fi

if [ -f "/var/cpanel/cleanbackups/latestversion" ]; then
   /bin/rm -f /var/cpanel/cleanbackups/latestversion
fi

if [ "$UPDATETREE" == "beta" ]; then
   UPDATETREE="beta"
else
   UPDATETREE="latest"
fi

/usr/bin/wget "http://download.ndchost.com/cleanbackups/version.php?updatetree=$UPDATETREE" -O /var/cpanel/cleanbackups/latestversion >/dev/null 2>&1

CV="`/bin/cat /var/cpanel/cleanbackups/version`"
LV="`/bin/cat /var/cpanel/cleanbackups/latestversion`"

/bin/rm -f /var/cpanel/cleanbackups/latestversion

if [ "$1" != "--force" ]; then
   if [ "$CV" == "$LV" ]; then
      exit
   fi
fi

cd /home
/bin/rm -f $UPDATETREE-cleanbackups
/usr/bin/wget http://download.ndchost.com/cleanbackups/$UPDATETREE-cleanbackups
/bin/sh $UPDATETREE-cleanbackups
