#!/bin/bash
# Copyright (c) 2008, RHINO EQUIPMENT CORP
# All rights reserved.
#
# Author: James Finstrom
# Date: 07-03-2008
# Name: install-ceros
SRVersion="1.0"
# Purpose: Install Ceros Customizations in boxes that have Rhino Drivers Native.
#
# Redistribution and use in source and binary forms, with or without modification, 
# are permitted provided that the following conditions are met:
#
#    * Redistributions of source code must retain the above copyright notice, 
#      this list of conditions and the following disclaimer.
#    * Redistributions in binary form must reproduce the above copyright notice,
#      this list of conditions and the following disclaimer in the documentation 
#      and/or other materials provided with the distribution.
#    * Neither the name of  RHINO EQUIPMENT nor the names of its contributors may be
#      used to endorse or promote products derived from this software without specific
#      prior written permission.
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
# THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES


function setup_ceros {
cd /usr/src/
              clear
              echo "========================================================"
              echo "                     CEROS SETUP                        "
              echo "     This mode assumes a clean install and will         "
              echo "     Overwrite files regardless of their origin         "
              echo "     Press CTRL+C to quit or any other key to continue  "
              echo "========================================================"
              if [ ! "$SCRIPT_MODE" ];then
              read -n1 choice
	      fi
pull_file http://dist.rhinoequipment.com/rsli/latest rsli.rpm
pull_file http://dist.rhinoequipment.com/ceros/latest ceros.rpm
rpm -e ceros
rpm -e rsli
rpm -ivh --replacefiles /usr/src/rsli.rpm
rpm -ivh --replacefiles /usr/src/ceros.rpm
}

function pull_file {
curl -A "setup-rhino ${SRVersion}" -f -L -o "$2" $1
curl_result=$?
 case "$curl_result" in
	0)
		 /bin/true;
	;;
	22)
		clear
		Could not locate: $1
		exit 22
	;;
	127)
		clear
		echo "There was a problem resolving the host for"
		echo "your Driver download Please update your resolv.conf"
		exit 127
	;;
	6)
		clear
		echo "setup-rhino can not resolve the host to download"
		echo "your drivers you should check your internet connection"
		echo "and your /etc/resolv.conf"
		exit 6
	;;
	*)
		clear
		echo "Something has gone terribly wrong curl returned $curl_result"
		echo "You may wish to do a google search for curl exit code $curl_result"
		exit $curl_result
	;;
esac
}

if [ $# -eq 0 ]; then
setup_ceros
exit 0
fi
####Process switches####

while [ $# -gt 0 ]; do
  case "$1" in
     -s)
               SCRIPT_MODE=1
	;; 
		;;
	*)
		echo "What"
		setup_ceros
	;;
	
  esac
 
  shift      # Check next set of parameters.
done