#!/bin/bash

# Config update script version 10.04-20121016
# JEO MSI 20121016

# Downloads custom configs specific to this router.

#  1. cron runs "/update-config" every 6 hours
#  2. /update-config downloads http://router-config.terrasls.com/configs/$BOX.tar.md5
#  3. /update-config checks if the md5sum of downloaded $BOX.tar.md5 matches the one we already have
#  4. If not, /update removes /$BOX.tar and downloads http://router-config.terrasls.com/configs/$BOX.tar
#  5. Verify md5sum again of course. If it matches 
#	(which means we have the same version of cloud-setup as the MD5), then untar it in the root directory

cd /

# Before we begin, handle the case where the updater itself needs updating. The new updater
#  should show up as a /update-config.new script in the root directory
if [ -s /update-config.new ] ; then
 rm -f /update-config && cp -f /update-config.new update-config
 chmod a+x /update-config 

  if [ -s /update-config ] ; then
    if [ -x /update-config ] ; then
      # The update-config exists and has nonzero size and is executable, so clean up & use that next time.
      rm -f /update-config.new
    fi
  fi
 exit
fi

if [ ! -s /etc/ML-BOX ] ; then
# -s means the file exists and has nonzero size
# We can't apply custom router configs to an unknown router, so exit
  echo "ERROR: SETUP SYSTEM - CAN'T APPLY CUSTOM CONFIG TO UNKNOWN BOX. Exiting..."
  exit 1
else
 BOX=`cat /etc/ML-BOX`
fi

if [ "x${BOX}" == "x" ] ; then
  echo "ERROR: SETUP SYSTEM - CAN'T APPLY CUSTOM CONFIG TO UNKNOWN BOX. Exiting..."
  exit 1
fi

# This is a new setup or new router version & we should try to download the tarball and MD5
if [ ! -s /${BOX}.tar.md5 ] ; then

  wget http://router-config.terrasls.com/configs/${BOX}.tar.md5 -U "Wget/router ${BOX}" 1>/dev/null 2>/dev/null
  if [ ! -s /${BOX}.tar.md5 ] ; then
   # No custom config exists on the server (404 not found) therefore nothing was downloaded by wget
   exit 
  fi

  wget http://router-config.terrasls.com/configs/${BOX}.tar -U "Wget/router ${BOX}" 1>/dev/null 2>/dev/null
  if [ ! -s /${BOX}.tar ] ; then
   # Don't try to un-tar a nonexistent or empty tarball
   exit 
  fi

  cd /
  tar xf /${BOX}.tar
  #exit
  # JEO 20121017 decided it would be better to reboot so the changes take effect immediately
  echo The router will now reboot  
  reboot

else

  # If we got here then the box has a custom config and we should check if it needs to be updated
  # Use the /tmp directory to avoid writing to the flash unnecessarily

  cd /tmp
  rm -f /tmp/${BOX}.tar.md5
  wget http://router-config.terrasls.com/configs/${BOX}.tar.md5 -U "Wget/router ${BOX}" 1>/dev/null 2>/dev/null
  if [ ! -s /tmp/${BOX}.tar.md5 ] ; then
   echo "ERROR: SETUP SYSTEM - UNABLE TO DOWNLOAD MD5SUM FOR CUSTOM CONFIG. Exiting..."
   exit 
  else

   # We got a md5sum check file, compare if it's different from the one we already had
   /usr/bin/cmp /tmp/${BOX}.tar.md5 /${BOX}.tar.md5 1>/dev/null 2>/dev/null || {
    # MD5 has changed, get the new tarball & apply changes
    cd /
    rm -f /${BOX}.tar
    wget http://router-config.terrasls.com/configs/${BOX}.tar -U "Wget/router ${BOX}" 1>/dev/null 2>/dev/null
    if [ ! -s /${BOX}.tar ] ; then
     echo "ERROR: SETUP SYSTEM - UNABLE TO DOWNLOAD CUSTOM CONFIG. Exiting..."
     exit 
    fi

    cd /
    tar xf /${BOX}.tar
    #exit
    # JEO 20121017 decided it would be better to reboot so the changes take effect immediately
    echo The router will now reboot  
    reboot
    }

    #/usr/bin/cmp /tmp/${BOX}.tar.md5 /${BOX}.tar.md5 1>/dev/null 2>/dev/null && {
    #echo "MD5s are the same"
    #}

  fi
fi
