#!/bin/bash
############################################################
#  Easy Installer                                          #
############################################################
#  Version .10Beta                                         #
#  Written By: Kuro Daemon and Steven McGrath              #
############################################################
#  This script will automatically uncompress, configure,   #
#  make, and make install a tarball (either gzipped or     #
#  bzipped).  On error, it will display the errors and     #
#  will exit out without continuing.                       #
#                                                          #
#  NOTE:  You must have root access to the applications    #
#         in question in order for it to work.             #
############################################################

#  Application Variables
startlocation=`pwd`
errors=/var/log/ei.errors
SourceLocation=/usr/src
tripwire="0"
gzipped=`echo $1 | sed -n /.tar.gz/p`
bzipped=`echo $1 | sed -n /.tar.bz2/p`
Version='0.10 Beta'

# Help Option
if [ "$1" = "" ];then
	echo "  Easy Installer Version $Version"
	echo
	echo "  ei filename"
	echo
	echo "  Easy Installer will understand both gzip and bzip2"
	echo "  compression schemes."
else
# Main Application
	echo -e "####\n\n`date`\n$1\n----" >>$errors

	mv $1 $SourceLocation 2>>$errors 1>/dev/null || tripwire="1"

	if [ $tripwire == "1" ]; then
		echo 'problems (first make)'
	fi

	if [ $tripwire != "1" ]; then
		cd $SourceLocation 2>>$errors || tripwier="1"
		if [ $tripwire == "1" ]; then
			echo 'problems (first cd)'
		fi
	fi

	if [ $tripwire != "1" ]; then
		if [ $gzipped = "$1" ]; then
    			tar zxf $1 2>>$errors 1>/dev/null
    			cd `echo $1 | sed s/.tar.gz//` 2>>$errors 1>/dev/null
  		elif [ $bzipped = "$1" ]; then
    			tar jxf $1 2>>$errors 1>/dev/null
    			cd `echo $1 | sed s/.tar.bz2//` 2>>$errors 1>/dev/null
  		fi 2>/dev/null
	fi

	if [ $tripwire != "1" ]; then
		./configure 2>>$errors 1>/dev/null || tripwire="1"
		if [ $tripwire == "1" ]; then
			echo 'problems (./configure)'
  		fi
	fi

	if [ $tripwire != "1" ]; then
  		make 2>>$errors 1>/dev/null || tripwire="1"
  		if [ $tripwire == "1" ]; then
  			echo 'problems (make)'
  		fi
	fi

	if [ $tripwire != "1" ]; then
  		sudo make install 2>>$errors 1>/dev/null || tripwire="1"
  		if [ $tripwire == "1" ]; then
    			echo 'problems (sudo make install)'
  		fi
	fi

	if [ $tripwire != "1" ]; then
  		mv ../$1 ~/tarballs 2>>$errors 1>/dev/null || tripwire="1"
  		if [ $tripwire == "1" ]; then
  			echo 'problems (last mv)'
  		fi
	fi

	echo -e "\n####" >>$errors

	cd $startlocation
fi