#!/bin/sh

#	Version 0.1
#	Date: 2004-02-16
#	Author: Tim Niiler
#	Copyright to Tim Niiler 2004
#	Distributable under the GPL

#	Purpose: to allow for easy installation of source code OR tgz packages on a Vector Linux system
#	Prerequisites/Dependencies:
#		You should be running bash
#		Xdialog
#		checkinstall (if you want to build your own packages)
#		Vector Linux OS (if you want to use the vecpkg command 
#			to install packages

doconfig() {
	# this is EXTREMELY simple.  It only works IF the unholy trio of 
	# compilation is setup: (./configure;make;make install)
	# First check to see that a configure file exists
        cd $1
        if [ -e configure ]; then
		(
                ./configure 
		echo -E "Done Configuring"
		) | Xdialog --title "Configuring" --tailbox "-" 10 70
		# Note that there is no error checking!  If configuration fails,
		# it is up to the user to notice.
		# Put a dialog box in here to keep program from progressing
		# in the event of a configuration failure.
        else
                Xdialog --title "Alert" --icon caution.xpm --infobox "There is no configure file.  Aborting installation." 0 0 2000
                exit
        fi
        return
}

domake() {
	(
	make
	echo -E "Done Compiling"
        ) | Xdialog --title "Compiling" --tailbox "-" 10 70
	#
	# Once more there is no error checking here
	# it is up to the user to determine if make worked based on
	# error messages or the lack thereof
	#
        return
}

doinstall() {
        # need to login as su first!
        xterm -T "Installing" -geometry 70x10 -e su -c \
                "make install"
	#
	# Error checking needed!
	#
        return
}

doclean() {
        make clean | Xdialog --title "Cleaning Up" --tailbox "-" 10 70
        return
}

docheckinstall() {
        # need to login as su first!
	if [ -e "/usr/local/sbin/checkinstall" ]; then
	        xterm -T "Creating Package" -geometry 70x10 -e su -c \
                "checkinstall"
	else
		Xdialog --title "Alert" --icon caution.xpm --infobox "You do not have checkinstall on your system.  Aborting." 0 0 2000
	fi	
        return
}

startinstall() {
        doconfig $1
        domake
        Xdialog --title "Ready to Install" \
--icon go.xpm \
--radiolist "Your software has been successfully compiled. " 18 72 2 \
"Install"  "Just install the software (usual choice)" on \
"Package" "Install software and create a package for distribution" off \
"Cancel"  "Quit and install nothing" off 2>/tmp/location.$$

        retval=$?
        choice=`cat /tmp/location.$$`
        rm -f /tmp/location.$$
        case $retval in
        0)
                if [ $choice = "Install" ]; then
                        doinstall
			Xdialog --title "Finished" --infobox "Your package has been installed"  0 0 2000
                fi
                if [ $choice = "Package" ]; then
                        docheckinstall
			Xdialog --title "Finished" --infobox "Your package has been installed"  0 0 2000
                fi
                if [ $choice = "Cancel" ]; then
			Xdialog --title "Aborted" --infobox "Your package was not installed"  0 0 2000
                        exit
                fi
        ;;
        1)
                echo "Cancel pressed."
                exit 1
        ;;
        255)
                echo "Escape pressed."
                exit 1
        ;;
        esac
	return
}


selectfile() {
	theuser=`whoami`
        FILE=`Xdialog --stdout --title "Please choose a file ending in tar.gz" --fselect /home/$theuser 0 0`

        case $? in
        0)
	#
	#	First test extensions and use appropriate utility to uncompress archive
	#
	f1=${#FILE}    # FILE length (string)
	base1p=f1-3	# for *.tar
	base2p=f1-2	# for *.gz
	base3p=f1-6	# for *.tar.gz
	exttar=${FILE:$base1p}
	echo "$exttar"
	extgz=${FILE:$base2p}
	echo "$extgz"
	exttargz=${FILE:$base3p}
	echo "$exttargz"

	if [ "$exttar" = "tar" ]; then
	        echo "This is a tar file"
		tar -xvf $FILE > log.txt
	elif [ "$extgz" = "gz" ]; then
	        if  [ "$exttargz" = "tar.gz" ]; then
	                echo "This is a tar.gz file"
			tar -xzvf $FILE > log.txt
	        elif [ "$exttar" = "tgz" ]; then
			echo "This is a tgz file"
			Xdialog --title "tgz File!" --yesno "This may be an installable tgz package \nthat does not need compiling. \n\nDo you want to continue and compile (YES) \nor install using installpkg (NO). \nClosing this dialog will return you to the previous menu\nand take no other action." 0 0
			case $? in
			  0)
			    echo "Yes chosen."
				tar -xzvf $FILE > log.txt
				;;
			  1)
			    echo "No chosen."
				xterm -T "Login as root" -geometry 70x10 -e su -c installpkg $FILE
				return
				;;
			  255)
			    echo "Box closed."
				return
				;;
			esac
		else 
	                echo "This is a gz file"
			gunzip -v $FILE > log.txt
	        fi
	else
	        echo "unrecognized file type"
			Xdialog --title "Error" --infobox "Unrecognized file type" 0 0 2000
			return
	fi

        dir=`head -1 log.txt`
        startinstall $dir
	rm -r log.txt
        ;;
        1)
                echo "Cancel pressed.";;
        255)
                echo "Box closed.";;
        esac
	return
}


main() {
Xdialog --title "Software Installer" \
--icon kpackage.xpm \
--radiolist "This installer allows you to either install \n \
*.tgz packages for your Vector Linux system or compile source \n \
code and install it in a user friendly manner. \n \n \
Please choose whether to \
install or compile new software" 18 72 3 \
"Install"  "Install/Uninstall a package (usual)" on \
"Compile"  "Compile and install software (advanced)" off \
"Cancel"  "Quit and install nothing" off 2>/tmp/location.$$

        retval=$?
        choice=`cat /tmp/location.$$`
        rm -f /tmp/location.$$
        case $retval in
        0)
                if [ $choice = "Install" ]; then
			xhost + localhost
			xterm -T "Login as root" -geometry 40x5 -e su -l -c /usr/sbin/vecpkg
			xhost - localhost
			main
                fi
                if [ $choice = "Compile" ]; then
                        selectfile
			main
                fi
                if [ $choice = "Cancel" ]; then
                        exit 1
                fi
        ;;
        1)
                echo "Cancel pressed."
                exit 1
        ;;
        255)
                echo "Escape pressed."
                exit 1
        ;;
        esac
}

main
