#!/bin/csh -f

onintr TimeToGo

set path=( . $path )
clear

if ( `whoami` == "root" ) then
	set installdir="/LocalApps"
	set libdir="/LocalLibrary/Converters"
else
	set installdir="~/Apps"
	set libdir="~/Library/Converters"
endif

set mydir = `pwd`

echof "Running this script will install GraphicsWorkshop on your system. It give you many oportunities to specify the arrangement of the install. You also be asked if you'd like to compile the various objects, even though all the objects come precompiled and ready to install. These are here in the event that you have a full developer's release and have modified code.\n\n"
echof "Please note that this install script works in cbreak mode, so you do not nead to hit return after single character input. Also, the input lines are editbable in a manner similar to tcsh. Here's some quick notes, arrow keys more left right, ^b back, ^f forward, ESC b back word, ESC f, forward word, ESC B first char, ESC F EOL, ^i toggle insert, ESC u Convert Word to uppers, ESC l Convert Word to lowers, and ^X delete line.\n\n"

echof "First of all, would you like the code built in debug mode? Doing this will generate profuse amounts of messages to the console window, that can help a lot when debugging. If you're just installing, say 'n'. Please enter y or n: "
set temp=`getyn`
echof "\n\n"
if ( $temp == "y" ) then
	set flags = "-DDEBUG"
else
	set flags = ""
endif

echof "You need to enter the name of the a directory telling where you want to install the software. The default is $libdir, but you are free to change this location. Please note, however, that the location must be ~/Library/Converters, /LocalLibrary/Converters, or /NextLibrary/Converters. However, since most people should not insert files into /NextLibrary, one of the other two is recommended. Also note, you will probably need to be root to install items into /LocalLibrary.\n\n"
echo -n "Your Directory Choice? "
set temp=`input -l 1024 -i "$libdir"`
echof "\n\n"

if ( $temp != "" ) then
   set libdir = $temp
endif

if ( -e $libdir ) then
   echof "\abThe directory $libdir already exists.\an\n\n"
else
   echof "Creating directory $libdir..."
   mkdir $libdir
   echof "done.\n\n"
endif

echof "The next questions are concerned with the converters. These are the actual objects that read and write bitmaps. You must have at least on in your library's converters folder for the program to function properly.\n\n"

echof "Would you like to compile the converters? (y or n)? "
set temp=`getyn`
echof "\n\n"
if ( $temp == "y" ) then
   echof "Please wait while I compile the converters."
      cd ./Converters
      make > /dev/null
      cd ..
   echof "done.\n\n"
endif

echof "Would you like move the converters into $libdir? (y or n)? "
set temp=`getyn`
echof "\n\n"
if ( $temp == "y" ) then
   echof "Moving objects into place..."
     	cp ./Converters/*.bcvt $libdir/
	echof "done.\n\n"
endif

echof "The next object deals with controlling the converters. This must also be installed on your system for things to work properly.\n\n"

echof "Would you like to compile the Bitmap Control Object? (y or n)? "
set temp=`getyn`
echof "\n\n"
if ( $temp == "y" ) then
   echof "Please wait while I compile it..."
      ./makecontrols $flags > /dev/null
   echof "done.\n\n"
endif

echof "Would you like to move the Bitmap Control object into $libdir? (y or n)? "
set temp=`getyn`
echof "\n\n"
if ( $temp == "y" ) then
   echof "Moving object into place..."
     	cp ./Bitmap.controls $libdir/
	echof "done.\n\n"
endif

echof "The next object deals with various common converters used by the converters. The program will run without this object on the system, however, only the tiff converter will work without it.\n\n"

echof "Would you like to compile Image Tools? (y or n)? "
set temp=`getyn`
echof "\n\n"
if ( $temp == "y" ) then
   echof "Please wait while I compile it..."
      ./maketools $flags > /dev/null
   echof "done.\n\n"
endif

echof "Would you like to move the Image Tools into $libdir? (y or n)? "
set temp=`getyn`
echof "\n\n"
if ( $temp == "y" ) then
   echof "Moving object into place..."
 		cp ./Bitmap.tools $libdir/
	echof "done.\n\n"
endif

echof "Finally, this is the main program. It's located in an application wrapper. You may wish to look around the original distributions wrapper, since this will create a stripped down, just runnable version without things like excess documentation.\n\n"

echof "Would you like to build GraphicsWorkshop? (y or n)? "
set temp=`getyn`
echof "\n\n"
if ( $temp == "y" ) then
	echof "Please wait, this can take a little while..."
		./makecontrol $flags > /dev/null
		if ( $flags == "-DDEBUG" ) then
			make debug > /dev/null
			mv GraphicsWorkshop.debug GraphicsWorkshop
		else
			make > /dev/null
		endif
	echof "done.\n\n"
endif

echof "Would you now like to modify GraphicWorkshop's icon header to include bitmaps of the known converters? (y or n)? "
set temp=`getyn`
echof "\n\n"
if ( $temp == "y" ) then
	./AddBitmaps
	echof "\n"
endif

echof "Finally, would you like GraphicsWorkshop.app installed on your system? (y or n)? "
set temp=`getyn`
echof "\n\n"
if ( $temp == "y" ) then
	echof "Please specify the location where you'd like the program installed or return to to accept the default? "
	set temp=`input -l 1024 -i "$installdir"`
	echof "\n\n"
	if ( $temp != "" ) then
		set installdir = $temp
	endif
	if ( -e $installdir/GraphicsWorkshop.app ) then
		InstallLoopBackI:
		echof "I see a previous version already exists, should I remove it, or should I back up this version? (r or b)? "
		set temp=`getch`
		echof "\n\n"
		if ( $temp == "r" ) then
			echof "Removing previous version..."
				rm -rf $installdir/GraphicsWorkshop.app
			echof "done.\n\n"
		else if ( $temp == "b" ) then
			echof "Backing up GraphicsWorkshop.app to GraphicsWorkshop.app~..."
				cp $installdir/GraphicsWorkshop.app $installdir/GraphicsWorkshop.app~
			echof "done.\n\n"
		else
			echof "I don't understand you input, try again.\n\n"
			goto InstallLoopBackI
		endif
	endif
	echof "Creating directory..."
		mkdir $installdir/GraphicsWorkshop.app
	echof "done.\n"
	echof "Copying nib files..."
		cp *.nib $installdir/GraphicsWorkshop.app/
	echof "done.\n"
	echof "Copying help files..."
		cp -r Help $installdir/GraphicsWorkshop.app/
	echof "done.\n"
	echof "Copying icons..."
		cp *.tiff $installdir/GraphicsWorkshop.app/
	echof "done.\n"
	echof "Stripping and copying executable..."
		strip -x GraphicsWorkshop
		cp GraphicsWorkshop $installdir/GraphicsWorkshop.app
	echof "done.\n\n"
	echof "Installation of GraphicsWorkshop.app is now complete.\n\n"
endif

echof "Installation is now complete. You should be able to run GraphicsWorkshop. However, if you experience any problems, and checking the documentation does not help, please feel free to send mail to alex@data.ACS.CalPoly.EDU.\n\n"

exit 0

TimeToGo:
	cd $mydir
	echof "\n\n\abAn unexpected interrupt has occured. GraphicsWorkshop may not be properly installed.\n\n\an"

