#!/bin/bash

# This is the initial script that the router will download.
# This script will download the current version of the router setup script and run it.  This is step
#  4 in the process.
#  Setup process for all V18:
#  1. Router flashed with stock OpenWRT image containing one custom MSI script: /setup (comes from base-files)
#  2. Router finishes initializing and connected to network.
#  3. User runs "/setup ML-XXX"
#  4. /setup downloads http://router-config.terrasls.com/v18/cloud-setup via wget
#  5. cloud-setup then downloads the current version of the setup script (i.e. 10.0.0) and runs it
#  6. The current version of setup script actually sets up the router.

VERSION=18.0.1

cat <<EOF
 __  __ _                                 
|  \/  | |  Router Version ${VERSION}
| \  / | |     ___   __ _  __ _  ___ _ __ 
| |\/| | |    / _ \ / _\` |/ _\` |/ _ \ '__|
| |  | | |___| (_) | (_| | (_| |  __/ |   
|_|  |_|______\___/ \__, |\__, |\___|_|   
                     __/ | __/ |          
                    |___/ |___/   

TerraSLS Inc. Router Setup Program

EOF

cd /

if [ -f /setup-$VERSION ] ; then
 rm -f setup-$VERSION
fi

wget http://router-config.terrasls.com/v18/$VERSION/setup-$VERSION -U "Wget/router $1" 1>/dev/null 2>/dev/null
chmod a+x /setup-$VERSION

if [ ! -x /setup-$VERSION ] ; then
 echo "SETUP SYSTEM ERROR - NO SETUP SCRIPT FOUND - EXITING"
 exit
fi

if [ -z /setup-$VERSION ] ; then
 echo "SETUP SYSTEM ERROR - NO SETUP SCRIPT FOUND - EXITING"
 exit
fi

echo "Setting up Router Version $VERSION."

./setup-$VERSION $1 $VERSION

