#!/bin/sh
#
#	Install script for Vector Linux Extras
#	
#	Version: 0.5.1
#	Date: 2004-02-18
#	Author: Tim Niiler
#	
#	Purpose:
#		This script was written to be used in place of the
#		Vector Linux 4.0 SOHO edition on older computers
#		for the West Chester, PA Community Computing project.
#		If you have a newer computer, please support our
#		base distribution software team by buying the 
#		Vector Linux 4.0 SOHO edition.  You can get this at:
#		http://www.vectorlinux.com
#
version=0.5.1
minHD=0.7	# Minimum HD space (GB) needed for Extras
medHD=1.4	# Next amount needed (for medium sized drive)
minSpeed=300.0	# CPU speed (MHz) required before OOv1.1.0 is installed
minRAM=128000.0  # RAM required before OOv1.1.0 or Mozilla is installed
enoughSpace=0	# Flags designating above requirements since
enoughSpeed=0	# comparison in Bash is so expensive
enoughMem=0

finalConfig() {
	DIALOG=Xdialog

	$DIALOG --title "INPUT BOX" \
	        --inputbox "What is the username of the system's primary user?" 20 45 2> /tmp/inputbox.tmp.$$

	retval=$?

	userName=`cat /tmp/inputbox.tmp.$$`
	rm -f /tmp/inputbox.tmp.$$

	case $retval in
	  0)
	Xdialog --title "Progress" --infobox "Configuring dfmDesk" 8 30 1000
        cp -R /mnt/cdrom/configs/wm/.dfmdeskSmall /home/$userName/.dfmdesk
        chown $userName -R /home/$userName/.dfmdesk
	chgrp users -R /home/$userName/.dfmdesk
	chmod 777 -R /home/$userName/.dfmdesk
        cp -R /mnt/cdrom/configs/wm/.dfminfo /home/$userName/
        chown $userName /home/$userName/.dfminfo
	chgrp users -R /home/$userName/.dfminfo
	chmod 777 -R /home/$userName/.dfminfo

	Xdialog --title "Progress" --infobox "Configuring IceWM" 8 30 1000
        cp -R /mnt/cdrom/configs/wm/.icewm /home/$userName/
        chown $userName /home/$userName/.icewm
	chgrp users -R /home/$userName/.icewm
	chmod 777 -R /home/$userName/.icewm

	Xdialog --title "Progress" --infobox "Configuring Fluxbox" 8 30 1000
        cp -R /mnt/cdrom/configs/wm/.fluxbox /home/$userName/
        chown $userName /home/$userName/.fluxbox
	chgrp users -R /home/$userName/.fluxbox
	chmod 777 -R /home/$userName/.fluxbox

	Xdialog --title "Progress" --infobox "Configuring KDE Desktop" 8 30 1000
	mkdir /home/$userName/Desktop
	if [ "$1" = "small" ]; then
	        cp -r /mnt/cdrom/configs/wm/DesktopSmall/* /home/$userName/Desktop
	elif [ "$1" = "medium" ]; then
	        cp -r /mnt/cdrom/configs/wm/DesktopMedium/* /home/$userName/Desktop
		#
		# It may be necessary to rip out these test files before 
		# distributing.  The short clips may not fall under fair use.
		#
		cp /mnt/cdrom/apps/MultiMedia/test.* /home/$userName/MultiMedia
		if [ "$2" -eq 1 ]; then
			if [ "$3" -eq 1 ]; then
				cp /mnt/cdrom/configs/wm/Fire* /home/$userName/Desktop
				cp /mnt/cdrom/configs/wm/Calc /home/$userName/Desktop
				cp /mnt/cdrom/configs/wm/Impress /home/$userName/Desktop
				cp /mnt/cdrom/configs/wm/Writer /home/$userName/Desktop
			else 
				cp /mnt/cdrom/configs/wm/Fire* /home/$userName/Desktop
				cp /mnt/cdrom/configs/wm/K* /home/$userName/Desktop
			fi
		fi
	else 
	        cp -r /mnt/cdrom/configs/wm/DesktopLarge/* /home/$userName/Desktop
		#
		# Also configure WINE
		#
		mkdir /home/$userName/.wine
		mkdir /home/$userName/.wine/C
		cp -R /mnt/cdrom/apps/Windows/*.exe /home/$userName/.wine/C/
		# Setup dosgames icon
		cp /mnt/cdrom/configs/wm/DOS* /usr/X11R6/share/dfm/icons
		# 
		mkdir /home/$userName/MultiMedia
		#
		# It may be necessary to rip out these test files before 
		# distributing.  The short clips may not fall under fair use.
		#
		cp /mnt/cdrom/apps/MultiMedia/test.* /home/$userName/MultiMedia
		# Copy proper office icons to the desktop
		if [ "$2" -eq 1 ]; then
			if [ "$3" -eq 1 ]; then
				cp /mnt/cdrom/configs/wm/Fire* /home/$userName/Desktop
				cp /mnt/cdrom/configs/wm/Calc /home/$userName/Desktop
				cp /mnt/cdrom/configs/wm/Impress /home/$userName/Desktop
				cp /mnt/cdrom/configs/wm/Writer /home/$userName/Desktop
				cp /mnt/cdrom/configs/wm/Mozilla* /home/$userName/Desktop
			else 
				cp /mnt/cdrom/configs/wm/Fire* /home/$userName/Desktop
				cp /mnt/cdrom/configs/wm/K* /home/$userName/Desktop
			fi
		fi
	fi
	
        chown $userName -R /home/$userName/Desktop
	chgrp users -R /home/$userName/Desktop
	chmod 777 -R /home/$userName/Desktop
        cp -R /mnt/cdrom/configs/wm/Wallpapers /home/$userName/
        chown $userName -R /home/$userName/Wallpapers
	chgrp users -R /home/$userName/Desktop
	chmod 777 -R /home/$userName/Desktop

	Xdialog --title "Progress" --infobox "Configuring extra user help" 8 30 1000
	cp -R /mnt/cdrom/HowTo /opt/
	chmod 777 -R /opt/HowTo

	cp /mnt/cdrom/configs/sys/Xsession /etc/X11/xdm/
	cp /mnt/cdrom/configs/sys/ld.so.conf /etc/
	cp /mnt/cdrom/configs/sys/.xsession /home/$userName/
	ldconfig
	Xdialog --title "Progress" --infobox "All Done.  You may install more packages using vecpkg" 0 0 2000
	;;
	  1)
	    echo "Cancel pressed.";;
	  255)
	    echo "Box closed.";;
	esac

}

smallKDE() {
	# The idea here is to install minimal KDE stuff for easy configuration
	# of the system even from IceWM
	#
	pkglist="kdeadmin*.tgz kdebase*.tgz kdebindings*.tgz kdegraphics*.tgz kdelibs*.tgz kdenetwork*.tgz kdeutils*.tgz qt*.tgz"
	for pkgi in $pkglist
	do 
		xterm -T "Installing $pkgi" -geometry 70x10 -e installpkg /mnt/cdrom/apps/KDE/$pkgi 
	done
}

installGnumeric() {
	xterm -T "Installing Gnumeric" -geometry 10x70 -e installpkg /mnt/cdrom/apps/Gnome/*.tgz
}

installMozilla() {
	Xdialog --title "Progress" --infobox "Copying files for Mozilla Installation" 8 30 1000
	cp -R /mnt/cdrom/apps/Mozilla /tmp
	sh /tmp/Mozilla/mozilla-installer
	Xdialog --title "Progress" --infobox "Removing Mozilla Setup Files" 8 30 1000
	rm -R /tmp/Mozilla
}

installOpenOffice() {
	Xdialog --title "Progress" --infobox "Copying files for OpenOffice Installation" 8 30 1000
	cp -R /mnt/cdrom/apps/OpenOffice /tmp
	/tmp/OpenOffice/setup
	Xdialog --title "Progress" --infobox "Removing OpenOffice Setup Files" 8 30 1000
	rm -R /tmp/OpenOffice
	xterm -T "Installing OO QuickStarter" -geometry 70x10 -e installpkg /mnt/cdrom/apps/OpenOffice/ooo*.tgz
}

smallHD() {
	xterm -T "Installing dosfstools" -geometry 70x10 -e installpkg /mnt/cdrom/apps/Extras/dos*.tgz
	xterm -T "Installing Acrobat" -e installpkg /mnt/cdrom/apps/Acrobat/*.tgz
	xterm -T "Updating GAIM" -geometry 70x10 -e installpkg /mnt/cdrom/apps/Gaim/*.tgz
	xterm -T "Installing IceWM Tools" -geometry 70x10 -e installpkg /mnt/cdrom/apps/IceWM/*.tgz
	smallKDE		# install basic configuration apps
	installGnumeric		# install small fast Gnumeric spreadsheet
	xterm -T "Installing Printing Libs" -geometry 70x10 -e installpkg /mnt/cdrom/apps/Printing/*.tgz
	Xdialog --title "Progress" --infobox "Configuring Window Managers" 8 30 1000
	finalConfig small $1 $2
	return
}


mediumHD() {
	xterm -T "Installing dosfstools" -geometry 70x10 -e installpkg /mnt/cdrom/apps/Extras/dos*.tgz
	if [ "$1" -eq 1 ]; then
		# Install Firefox if speed is sufficient
		# Mozilla will not be installed bc of slow start times
		#
		Xdialog --title "Progress" --infobox "Installing FireFox" 8 30 1000
		cp -r /mnt/cdrom/apps/firefox 8 30 1000 /opt
	fi
	xterm -T "Installing Acrobat" -e installpkg /mnt/cdrom/apps/Acrobat/*.tgz
	xterm -T "Updating GAIM" -geometry 70x10 -e installpkg /mnt/cdrom/apps/Gaim/*.tgz
	xterm -T "Installing Graphics Appkications" -geometry 70x10 -e installpkg /mnt/cdrom/apps/Graphics/*.tgz
	xterm -T "Installing IceWMTools" -geometry 70x10 -e installpkg /mnt/cdrom/apps/IceWM/*.tgz
	xterm -T "Installing Printing Libraries" -geometry 70x10 -e installpkg /mnt/cdrom/apps/Printing/*.tgz
	xterm -T "Installing KDE Libraries" -geometry 70x10 -e installpkg /mnt/cdrom/apps/KDE/kde*.tgz
	xterm -T "Installing Qt Widget Set" -geometry 70x10 -e installpkg /mnt/cdrom/apps/KDE/qt*.tgz
	xterm -T "Installing Sound" -geometry 70x10 -e installpkg /mnt/cdrom/apps/KDE/art*.tgz
	xterm -T "Installing MultiMedia Libraries" -geometry 70x10 -e installpkg /mnt/cdrom/apps/MultiMedia/*.tgz
	if [ "$1" -eq 0 ]; then 
		# Computer is slow, stick with KOffice
		xterm -T "Installing KOffice" -geometry 70x10 -e installpkg /mnt/cdrom/apps/KDE/koff*.tgz
		xterm -T "Installing KOffice Filters" -geometry 70x10 -e installpkg /mnt/cdrom/apps/KDE/wv*.tgz
	else
		if [ "$2" -eq 0 ]; then
			# Computer is faster but doesn't have enough
			# memory to make installation of OpenOffice 
			# worthwhile
			xterm -T "Installing KOffice" -geometry 70x10 -e installpkg /mnt/cdrom/apps/KDE/koff*.tgz
			xterm -T "Installing KOffice Filters" -geometry 70x10 -e installpkg /mnt/cdrom/apps/KDE/wv*.tgz
		else 
			# Computer is faster, try OpenOffice
			Xdialog --title "Progress" --infobox "Installing OpenOffice" 8 30 1000
			installOpenOffice
		fi
	fi
	Xdialog --title "Progress" --infobox "Configuring Window Managers" 8 30 1000
	finalConfig medium $1 $2
	return
}


largeHD() {
	xterm -T "Installing dosfstools" -geometry 70x10 -e installpkg /mnt/cdrom/apps/Extras/dos*.tgz
	if [ "$1" -eq 1 ]; then
		# Install Firefox if speed is sufficient
		# Mozilla will not be installed bc of slow start times
		#
		xterm -T "Installing FireFox Browser" -geometry 70x10 -e cp -r /mnt/cdrom/apps/firefox /opt
		if [ "$2" -eq 1 ]; then
		# Install Mozilla as well
		#
			Xdialog --title "Progress" --infobox "Installing Mozilla" 8 30 1000
			installMozilla
		fi
	fi
	xterm -T "Installing Acrobat" -e installpkg /mnt/cdrom/apps/Acrobat/*.tgz
	xterm -T "Updating GAIM" -geometry 70x10 -e installpkg /mnt/cdrom/apps/Gaim/*.tgz
	xterm -T "Installing Graphics Applications" -geometry 70x10 -e installpkg /mnt/cdrom/apps/Graphics/*.tgz
	xterm -T "Installing IceWMTools" -geometry 70x10 -e installpkg /mnt/cdrom/apps/IceWM/*.tgz
	xterm -T "Installing Printing Libraries" -geometry 70x10 -e installpkg /mnt/cdrom/apps/Printing/*.tgz
	xterm -T "Installing KDE Libraries" -geometry 70x10 -e installpkg /mnt/cdrom/apps/KDE/kde*.tgz
	xterm -T "Installing Qt Widget Set" -geometry 70x10 -e installpkg /mnt/cdrom/apps/KDE/qt*.tgz
	xterm -T "Installing MultiMedia Libraries" -geometry 70x10 -e installpkg /mnt/cdrom/apps/MultiMedia/*.tgz
	xterm -T "Installing Sound" -geometry 70x10 -e installpkg /mnt/cdrom/apps/KDE/art*.tgz
	xterm -T "Installing Scanning Libraries" -geometry 70x10 -e installpkg /mnt/cdrom/apps/Scanning/*.tgz
	xterm -T "Installing Specialized Libraries" -geometry 70x10 -e installpkg /mnt/cdrom/apps/Specialized/*.tgz
	xterm -T "Installing WINE" -geometry 70x10 -e installpkg /mnt/cdrom/apps/Windows/*.tgz
	xterm -T "Installing Emulation Capabilities" -geometry 70x10 -e installpkg /mnt/cdrom/apps/Emulation/*.tgz
		cp /mnt/cdrom/apps/Emulation/dosgames /usr/bin/dosgames
		chmod 755 /usr/bin/dosgames
	if [ "$1" -eq 0 ]; then 
		# Computer is slow, stick with KOffice
		xterm -T "Installing KOffice" -geometry 70x10 -e installpkg /mnt/cdrom/apps/KDE/koff*.tgz
		xterm -T "Installing KOffice Filters" -geometry 70x10 -e installpkg /mnt/cdrom/apps/KDE/wv*.tgz
	else
		if [ "$2" -eq 0 ]; then
			# Computer is faster but doesn't have enough
			# memory to make installation of OpenOffice 
			# worthwhile
			xterm -T "Installing KOffice" -geometry 70x10 -e installpkg /mnt/cdrom/apps/KDE/koff*.tgz
			xterm -T "Installing KOffice Filters" -geometry 70x10 -e installpkg /mnt/cdrom/apps/KDE/wv*.tgz
		else 
			# Computer is faster, try OpenOffice
			Xdialog --title "Progress" --infobox "Installing OpenOffice" 8 30 1000
			installOpenOffice
		fi
	fi
	Xdialog --title "Progress" --infobox "Configuring Window Managers" 8 30 1000
	finalConfig large $1 $2
	return
}

retVal=
compareFP() {
	var1a=`echo "$1" | awk -F. '{print $1}'`
	var1b=`echo "$1" | awk -F. '{print $2}'`
	var2a=`echo "$2" | awk -F. '{print $1}'`
	var2b=`echo "$2" | awk -F. '{print $2}'`
	if [ "$var1a" -lt "$var2a" ]; then
		vara=-1
	elif [ "$var1a" -gt "$var2a" ]; then
		vara=1
	else 
		vara=0
	fi	
	b1Len=${#var1b}
	b2Len=${#var2b}
	zero=0
	if [ "$b1Len" -lt "$b2Len" ]; then
		diff=$b2Len-$b1Len
		for (( i=0; i < diff; i++))
		do
			var1b=${var1b}$zero
		done
	else
		diff=$b1Len-$b2Len
		for (( i=0; i < diff; i++))
		do
			var2b=${var2b}$zero
		done
	fi
	
	if [ "$var1b" -lt "$var2b" ]; then
		varb=-1
	elif [ "$var1b" -gt "$var2b" ]; then
		varb=1
	else 
		varb=0
	fi	

	if [ "$vara" -eq 0 ]; then
		if [ "$varb" -eq 0 ]; then
			retVal=0
		elif [ "$varb" -lt 0 ]; then
			retVal=-1
		else
			retVal=1
		fi	
	elif [ "$vara" -lt 0 ]; then
		retVal=-1
	else 
		retVal=1
	fi
}

#	1.2 GB needed for small install or 0.6 GB if base package installed
#	2.0 GB needed for medium install or 1.4 GB if base package installed

checkhardware() {
	space=`df -h | grep /dev/hda | awk '{print $4}' | awk -FG '{print $1}'`
	speed=`cat /proc/cpuinfo | grep MHz | awk '{print $4}'`
	memory=`cat /proc/meminfo | grep MemTotal | awk '{print $2}'`
	compareFP $space $minHD
	comp1=$retVal
	compareFP $space $medHD
	comp2=$retVal
	compareFP $space 0.5
	comp3=$retVal
	if [ "$comp3" -lt 0 ]; then
		Xdialog --title "Warning" --infobox "You do not have enough space to install anything!" 0 0 1000
		Xdialog --title "Suggestion" --infobox "Try installing individual packages instead." 0 0 1000
		/usr/sbin/vecpkg
		exit
	fi

	if [ "$comp1" -lt 0 ]; then 
		echo "Small Harddrive: $space"
	elif [ "$comp2" -lt 0 ]; then
		echo "Medium Harddrive: $space"
		enoughSpace=1
	else 
		echo "Large Harddrive: $space"
		enoughSpace=2
	fi

	compareFP $speed $minSpeed
	comp1=$retVal
	if [ "$comp1" -lt 0 ]; then
		echo "Slow Processor: $speed"
	else 
		echo "Fast Processor: $speed"
		enoughSpeed=1
	fi

	compareFP $memory $minRAM
	comp1=$retVal
	if [ "$comp1" -lt 0 ]; then
		echo "Low Memory: $memory"
	else 
		echo "High Memory: $memory"
		enoughMem=1
	fi

	DIALOG=Xdialog

	$DIALOG --wrap --title "Hardware Detected" \
	--icon harddrive.xpm \
        --yesno "The following hardware capabilities were detected: \n \n \
Remaining Disk Space GB: $space \n \
CPU Speed MHz: $speed \n \
Memory bytes RAM: $memory \n \n \
If this is not correct, you may be using a distribution other than \
Vector Linux 4.0 and this installer may not work for you. \n \n \
Do you want to continue?" 420x250

	case $? in
	  0)
	    echo "Yes chosen."
		doinstall $enoughSpace $enoughSpeed $enoughMem
		;;
	  1)
	    echo "No chosen.";;
	  255)
	    echo "Box closed.";;
	esac
	return

}

doinstall() {
	# All installs will include printing libraries	
	if [ "$1" = 0 ]; then
		# If the HD is small, no way we should do a full install
		# limit KDE to very basics for easy sys-config
		# use Abiword and Gnumeric
		# and based on CPU speed and RAM determine whether to 
		# bother with Firefox or leave Dillo in place
		smallHD $2 $3
	elif [ "$1" = 1 ]; then
		# With a medium size HD, the default office suite will
		# be KOffice and Gnumeric and supporting libraries
		# will not be installed. All of KDE will be installed.
		# Otherwise, there will also be Graphics programs
		# Mozilla and or Firefox and some MultiMedia programs
		# such as Xine will be installed.
		mediumHD $2 $3
	else 	
		# Install the kitchen sink
		# Include BOTH Open Office and KOffice based on CPU speed
		# Include scanning libraries (just in case)
		# Include all of KDE and Audacity, Xine, and possibly some
		# other stuff (TBA).
		largeHD $2 $3
	fi
	return
}


main () {
DIALOG=Xdialog

me=`whoami`
if [ "$me" != "root" ]; then
	$DIALOG --title "Alert" --infobox "You must run this program as root!" 0 0 5000
	exit
fi

file=/root/vladdons.txt
if [ -e $file ]; then
	$DIALOG --title "Alert" --infobox "Addons already installed. \n Reverting to vecpkg." 0 0 2000
	/usr/sbin/vecpkg
	exit
else

$DIALOG --wrap --title "Install VL Addons" \
	--icon kpackage.xpm \
        --yesno "This program allows you to install addons to your \
base Vector Linux 4.0 system based on the speed and configuration \
of your machine.  These packages include Office, Graphics, Audio, \
Multimedia, Games, Browsing, and much more. Do you want to \
continue?" 300x200

case $? in
  0)
    echo "Yes chosen."
	checkhardware	
	echo "Vector Linux Addons Version $version is installed." > /root/vladdons.txt
	/usr/sbin/vecpkg
	;;
  1)
    echo "No chosen.";;
  255)
    echo "Box closed.";;
esac

fi

}

main
