#!/bin/bash
########################################################################
####  Script Name: smxi-stub
####  stub installer for smxi scripts
####  Only supports true Debian based distros.
####  version: 3.0
####  Date: 24 February 2010
########################################################################
####  Copyright (C) Harald Hope, sidux team members 2007-2010
####
####  This program is free software; you can redistribute it and/or modify it under
####  the terms of the GNU General Public License as published by the Free Software
####  Foundation; either version 2 of the License, or (at your option) any later version.
####
####  This program is distributed in the hope that it will be useful, but WITHOUT
####  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
####  FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
####
####  Get the full text of the GPL here: http://www.gnu.org/licenses/gpl.txt
########################################################################

# Please do not change the logic here, it makes it too hard to read linearly:
# [ ! -f somefile ] && do something is far easier to read than [ -f somefile ] || do something
# tempfile primary cleanup is now handled by called scripts, so no need to complicate this
# simple stub with the cleanup stuff. Now it just cleans up after itself each time.

# To get the full power and user friendliness for this stub installer
# rename it to: smxi
# place it in /usr/sbin, that is, as /usr/sbin/smxi, make it executable: chmod +x /usr/sbin/smxi
# add the following symbolic links in /usr/sbin pointing to /usr/sbin/smxi:
# ln -s /usr/sbin/smxi /usr/sbin/inxi
# ln -s /usr/sbin/smxi /usr/sbin/sgfxi
# ln -s /usr/sbin/smxi /usr/sbin/svmi
# because of how default $PATH works, /usr/sbin will always be checked first for the
# scripts, and if they are missing, this stub will download and execute them, otherwise
# the request will simply be passed along to /usr/local/bin location of actual scripts

# this will handle stub for any other script also
CALLER="$( basename $0 )"
PREFIX='/usr/local/bin/'
DOWNLOAD_URL=''
DOWNLOAD_URL_INXI='http://inxi.googlecode.com/svn/trunk/'
DOWNLOAD_URL_SGFXI='http://smxi.org/sg/'
DOWNLOAD_URL_SMXI='http://smxi.org/sm/'
DOWNLOAD_URL_SVMI='http://smxi.org/sv/'
FILE_DATA_TEMP='' # will handle failed downloads
IS_GOOD=''
# this is the tester for ##**EOF**## endings present in scripts, change this
# if you want to extend this stub to other scripts, smxi/svmi/inxi/sgfxi all use this
SCRIPT_EOF='##\*\*EOF\*\*##'

# args: $1 - error number; $2 - extra data
error_handler()
{
	local message=''
	case $1 in
		1)	message='You must be root to run this script!'
			;;
		2)	message="Script download exited with errors.\nThe following download url failed with wget error: $2/n$DOWNLOAD_URL$SCRIPT"
			;;
		3)	message='Failed to download script properly. The file data is corrupted or incomplete!'
			;;
		4)	message="$PREFIX$SCRIPT exec command failed with error number: $2\nPlease try again."
			;;
		5)	message="$PREFIX$SCRIPT not executable!"
			;;
		6)	message="Unknown script requested: $CALLER"
			;;
		*)	message='Unknown error, exiting now.'
			;;
	esac
	echo -e "Error No: $1 - $message"
	exit $1
}

# set the SCRIPT/DOWNLOAD_URL/SCRIPT_EOFvalues
case $CALLER in
	sgfxi)
		SCRIPT="$CALLER"
		DOWNLOAD_URL=$DOWNLOAD_URL_SGFXI
		;;
	smxi)
		SCRIPT="$CALLER"
		DOWNLOAD_URL=$DOWNLOAD_URL_SMXI
		;;
	svmi)
		SCRIPT="$CALLER"
		DOWNLOAD_URL=$DOWNLOAD_URL_SVMI
		;;
	inxi)
		SCRIPT="$CALLER"
		DOWNLOAD_URL=$DOWNLOAD_URL_INXI
		;;
	*)
		error_handler 6
		;;
esac

# if $SCRIPT exists, it will handle the root tests. Root tests cannot
# be run first because they kill the -h help function
if [ -x "$PREFIX$SCRIPT" ];then
	# I'm just going to put a test for basically everything that can go wrong
	exec "$PREFIX$SCRIPT" "$@" || error_handler 4 "$?"
else
	# only in case of requirement to download script do root test, and just exit
	# if not root, anything else is counterintuitive I think for users.
	if [ "$( id -u )" -ne 0 ];then
		error_handler 1
	fi
	# downloading to overwrite existing file, but to temp variable, which we then test
	FILE_DATA_TEMP="$( wget -q -O - $DOWNLOAD_URL$SCRIPT )" || error_handler 2 "$?"
	IS_GOOD=$( grep -si "$SCRIPT_EOF" <<< "$FILE_DATA_TEMP" )
	# test to make sure downloaded data has EOF marker, if yes, it's complete
	if [ -z "$IS_GOOD" ];then
		error_handler 3
	else
		# now that the data is tested and not corrupted move to real file
		echo "$FILE_DATA_TEMP" > $PREFIX$SCRIPT
		# make new file executable
		chmod 705 "$PREFIX$SCRIPT"
		# then run it with an error handler for unknown weirdness which I'm sure will appear.
		if [ -x "$PREFIX$SCRIPT" ];then
			# If for some reason it errors on script execution, exit here, but that could be
			# something as simple as bad options used
			exec "$PREFIX$SCRIPT" "$@" || error_handler 4 "$?"
		else
			error_handler 5
		fi
	fi
fi

