#!/bin/bash
rriu_version=4
#
# Summary: 
# This script downloads the latest Rapid Recovery Installation tool automatically and runs it with normal parameters.
#
check_dl=$(which wget 2>/dev/null || which curl 2>/dev/null || echo "not_found")
if [ "$check_dl" = "not_found" ]; then
  echo "ERROR: Could not find 'wget' or 'curl'. Please install and try again." ; exit 1
else
  check_dl="$(basename $check_dl)"
fi

function download_script {

    case "$check_dl" in
        wget) wget -q http://aainfo5.s3.amazonaws.com/Linux/rriu/latest_build/rriu.sh -O rriu.sh || echo -e "ERROR: Could not download the 'rriu.sh' script.\nPlease make sure you have a working internet connection and try again.\n" ;;
        curl) curl -s http://aainfo5.s3.amazonaws.com/Linux/rriu/latest_build/rriu.sh -o rriu.sh || echo -e "ERROR: Could not download the 'rriu.sh' script.\nPlease make sure you have a working internet connection and try again.\n" ;;
    esac

}

if [ -f rriu.sh ]; then
    rriu_version_local="$(head -n 2 rriu.sh | grep version | awk -F= '{print $2}')"
    if [ $rriu_version = $rriu_version_local ]; then
        echo "Using local version of script."
    else
        download_script
    fi
else
    download_script
fi
bash rriu.sh $1 $2 $3 $4 $5 $6 $7 $8 #Passing maximum number of arguments to the script
rm -f rriu.sh &>/dev/null

exit 0

