; Install Distant Suns.
; Prints out debug info if run from CLI

;   Set inital global variables
(set product "DS50")
(set ffp "ffp")
(set fpu "fpu")
(set inst_name "DS_Install")
(set inst_disk (cat inst_name ":"))
(set data_name "ds_data")
(set data_disk (cat data_name ":"))
(set images_name "dx_fsi")
(set images_disk (cat images_name ":"))
(set aga_name "AGA_Images")
(set aga_disk (cat aga_name ":"))
(set aga_name2 (cat aga_name "2"))
(set aga_disk2 (cat aga_name2 ":"))
;(set old-level @user-level)
;(user 2) ; we'll set it back once we determine feasibility

(transcript "Install Distant Suns 5.0")
(transcript "Installer script ver 4.0 <--- VERY IMPORTANT INFO")

(set space_req 4096000) ; max hard drive space needed
(set where_msg "Installer had a problem in")

(debug "default dest " @default-dest)
(set mem (database "total-mem"))
(set chip (database "graphics-mem"))
(set mem (/ mem 1024000))
(set chip (/ chip 1024000))
(debug "mem " mem)
(debug "chip " chip)

(onerror
    (set @execute-dir (tackon inst_disk "c"))
    (set clean_up ("delete ram:ds#? all"))
    (debug clean_up)
    (run (clean_up))
    (message "Installation aborted or an error has occurred! If you need Technical Support, "
	     "please call (805) 545-8515.  Double Click on the install-log-file icon, "
	     "and be ready to read it to the support "
	     "specialist.  Rerun the install, and select the print log.  You "
	     "can also fax the log to: Tech Support 805 781 2259 (P.S. Do not "
	     "forget your name, address, and DAYTIME phone #.)"
    )
)

(if (< chip 1)
    (message "Your system has less than one meg of chip ram free at the moment.  "
	   "Distant Suns requires at least one meg of chip ram.  "
	   "It will run on machines with one meg of chip RAM, "
	   "but not in hi-res (or overscan), and may crash periodically, etc.\n\n"
    )
)

(if (< mem 1)
    (abort "This install requires at least 1 meg of free RAM.  Quit "
	   "all other programs and restart this install.  If that doesn't "
	   "work, try reducing your Workbench screen size/depth."
    )
)

(if (< mem 2)
    (message "Distant Suns 5.0 requires at least 2 megs of RAM to run.  "
	    "Your system has " mem " meg(s) free at the moment.  If you don't "
	    "have 2 megs of RAM, abort this install now.\n\n"
	    "Unexpanded A1200 users: You can run in hi-res (or vga) with all "
	    "functionality except the ability to view AGA images.  "
	    "Also, Distant Suns will speed up about 20 - 30% if you add more "
	    "memory to your system."
    )
)

(set workbench (getversion))
(set work (/ workbench 65536))
(set wb (if (>= work 37) "20" "13"))
(set AGA (if (>= work 39) 1 0))

(debug "workbench " workbench)
(debug "work " work)
(debug "wb " wb)
(debug "AGA " AGA)

(if (= wb "13")
    (abort "Commodore stopped supporting Workbench 1.3 on August of "
	   "1992.\n\n"
	   "This release of Distant Suns is NOT supported on 1.3."
    )
)

(procedure
    yo
    (
	(message
	    "VRLI Install script 4.0 for Distant Suns 5.0. "
	    "This install will install Distant Suns to your hard drive "
	    "(floppy drives are NOT supported).  "
	    "If you are ready, click on Proceed."
	)
	(set space_v
	    (askbool
		(prompt "Is Space Visions installed on your system? "
		    "Space Visions is a 26 disk set of images available for "
		    "Distant Suns.  Click on Help for important information."
		)
		(help "If Space Visions is installed on your system:\n\n"
		    "Answer Yes to this question, and pick Intermediate as your "
		    "User level.\n\n"
		    "Don't check the old images at the next prompt.\n\n"
		    "When the installer asks where you would like Distant "
		    "Suns installed, select the directory where you have the "
		    "old version installed.\n\n"
		    "Note: You'll also need the Space Visions Install disk "
		    "(S11).\n\n"
		)
	    )
	)
	(set def_img (if (= AGA 1) 2 0))
	(set def_img (+ def_img (if (= space_v 1) 0 1)))
	(set image
	    (askoptions
		(prompt "Select the image disks you wish to install (click "
			"on help for an explanation)."
		)
		(help "Old images:\n\n"
			"If you already have an existing 4.0 or better version on "
			"your system, then you don't need the 4.0 images installed "
			"again.  If you are installing for the first time, or are "
			"upgrading from a version of Distant Suns previous to 4.0, "
			"select the old images.  This selection will overwrite "
			"Space Visions if you have it installed.\n\n"
			"New 256 color images:\n"
			"These are images new to 5.0, and require an AGA "
			"machine."
		)
		(choices "Old DS 4.0 images"
			"New 256 color images"
		)
		(default def_img)
	    )
	)
	(debug "image" image)
	(if (= AGA 1)
	    (
		(set mchoice
		    (askchoice
			(prompt "Which screen mode would you like Distant Suns to "
			    "run in?\n\n"
			    "Attention A1200 users: you must click on Help "
			    "for details."
			)
			(help "IMPORTANT NOTE: If you have a 1200 and one of "
				"following monitors, DO NOT select VGA or DOUBLE:\n"
				"        Any 1080 series monitor\n"
				"        Television set\n"
				"        Any non-multisync monitor\n"
				"The Double mode is 640 x 200 at 15 kHz (med)\n"
				"       VGA mode is 640 x 400 at 29 kHz (hi)\n"
				"        Pre-AGA is 640 x 400 interlaced (hi)\n"
				"                or 640 x 200 noninterlaced (med)\n"
				"You can change the screen mode after "
				"installation by changing the ds_5.0 icon's "
				"screen tooltype MONITOR=double|vga|(nothing)\n"
				"(the | symbol means 'or')."
			)
			(choices "vga"
				 "double"
				 "Pre-AGA"
			)
		    )
		)
	    )
	    (
		(set mchoice 2)
	    )
	)
	(debug "mchoice = " mchoice)
	(set monitor (select mchoice "vga" "double" "Pre-AGA"))
	(if (= monitor "Pre-AGA")
	    (
		(set res
		    (select
			(askbool
			    (prompt "Would you like Distant Suns to start "
				"up in hi-res?\n\n"
				"Click on help if you have less than 2 megs "
				"of Chip RAM."
			    )
			    (help "We strongly suggest that you run in medium "
				"resolution (answer No to this prompt).  Your machine will not be able to handle the running "
				"Distant Suns in hi-res.  To change the startup display mode "
				"after installing, change the icon's RES tooltype to med or hi (must "
				"be lowercase)."
			    )
			)
			"med"
			"hi"
		    )
		)
	    )
	    (set res (select mchoice "hi" "med" "med"))
	)
    )
)

(procedure extract
    (
;	 (set @special-msg (cat where_msg " extract"))
	(set rextract ("lharc -m x %s %s/ %s" archive rpath file))
	(debug "Extract " rextract)
	(transcript "Extracting file " file " from archive " archive " to " rpath)
	(if (= @pretend 0)
	    (
		(if (not (= (run (rextract)) 0))
		    (abort "Fatal Error ocurred while extracting:\n"
			   "file: " file " from " archive " to " rpath
		    )
		)
		(copyfiles
		    (all)
		    (source rpath)
		    (dest destination)
		    (infos)
		    (fonts)
		    (prompt "Copy the following files to your hard drive...")
		    (help @copyfiles-help)
		)
	    )
	    (message "Pretended to extract file " file " from archive " archive ", and "
		     "copy it to " destination)
	)
	(set clean_up ("delete %s/#? all" rpath))
	(debug clean_up)
	(transcript "Cleaning up " rpath " with " clean_up)
	(if (= @pretend 0)
	    (if (not (= (run clean_up) 0))
		(abort "Fatal error!\n\n"
		       "Installer had problems cleaning up " rpath
		)
	    )
	)
    )
)

(procedure ensure
    (
;	 (set @special-msg (cat where_msg " ensure"))
	(debug "Checking " file)
	(if (not (exists (tackon destination file) (noreq)))
	    (
		(if (= @pretend 1)
		    (message "Pretend install could not find " file
			" and will now pretend to extract it."
		    )
		)
		(transcript file " not found on system, attempting to install.")
		(set stat (cat "Installing " file))
		(working stat)
		(set archive (tackon inst_disk (cat product "_support" )))
		(debug "ensure extracting " file)
		(extract)
		(set installed n)
	    )
	)
    )
)

(procedure verify
    (
;	 (set @special-msg (cat where_msg " verify"))
;	 (set @special-msg "")
	(set n 0)
	(while (< n 8)
	    (
		(set file
		    (select n
			"flow.font" "flow/8"
			"flow_thin.font" "flow_thin/8"
			"symbols.font" "symbols/9"
			"star_image.font" "star_image/12"
		    )
		)
		(debug "file = " file)
		(set destination "fonts:")
		(ensure)
		(set n (+ n 1))
	    )
	)
    )
)

(procedure check
    (
	(set space (getdiskspace install_dir))
	(if (< space space_req)
	    (abort "You have " space " bytes on your hard drive. "
		     "Distant Suns requires " space_req
		     " bytes.  Clean up your hard drive and reinstall."
	    )
	)
    )
)

(procedure
    get_path (
	(set install_dir "")
	(while (= install_dir "")
	    (set install_dir
		(askdir
		    (prompt "Where would you like to install Distant Suns? ")
		    (help @askdir-help)
		    (default @default-dest)
		)
	    )
	    (if (not (exists install_dir))(set install_dir ""))
	    (if (= @default-dest install_dir)
		(
		    (set install_dir (tackon install_dir product))
		    (if (not (exists install_dir))
			(makedir install_dir)
		    )
		)
	    )
	)
	(debug "Install dir " install_dir)
    )
)

;(set @special-msg (cat where_msg " preinstallation check"))
(yo)
;(user old-level)
(welcome "Welcome to the Distant Suns Installation program.")
(debug "user level = " @user-level)

; Set up temp dir for extracting files:

(set rpath "ram:ds_temp")
(if (= @pretend 0)(makedir rpath))

; Set up commands to be used in installation (the old copy to ram: trick).

(set ram_c "ram:ds_c")
(set @execute-dir ram_c)

(if (= @pretend 0)  ; if installing for real, then:
    (
	(makedir ram_c)
	(copyfiles
	    (all)
	    (source (tackon inst_disk "c"))
	    (dest ram_c)
	    (safe)
	    (nogauge)
	)
    )
    (message "Installer pretended to make a directory in RAM: called "
	     ram_c ", then copied all files in " (cat inst_disk c) " to "
	     ram_c "."
    )
)

(if (= @user-level 0)
    (
	(set install_dir (tackon @default-dest product))
	(debug install_dir)
	(if (not (exists install_dir)) (makedir install_dir))
	(check)
    )
    (
	(set install_dir "")
	(get_path)
	(check)
	(set del_old_files (cat "delete " (tackon install_dir "ds#?") " all"))
	(run del_old_files)
    )
)

(set sys (database "cpu"))
(debug "System: " sys)
(transcript "CPU " sys)
(set prob_fpu (if (or (= sys "68000") (= sys "68020")) 0 1))

(set math
    (if
	(askchoice
	    (prompt "Does your system have a math co-processor?")
	    (help "If you have an Amiga 500, 600, 1200 or 2000, and haven't ever added "
		  "a faster processor, you probably don't have a math coprocessor. "
		  "If you have an Amiga 3000 or 4000, you have a math coprocessor. "
		  "If you aren't sure, then select no.")
	    (default prob_fpu)
	    (choices "No" "Yes")
	)
	"fpu"
	"ffp"
    )
)

;(set @special-msg (cat where_msg " main"))
(debug "Math: " math)
(set file (cat wb math))
(debug "file " file)
(set archive (tackon inst_disk (cat product "wb" wb)))
(debug "Archive: " archive)
(set destination install_dir)
(transcript "Extracting Distant Suns.")
(working "Extracting Distant Suns to " rpath)
(extract)

(rename
    (tackon install_dir file)
    (tackon install_dir "ds_5.0")
    (prompt "Rename " file " to ds_5.0?")
    (help "Do this!")
)
(complete 10)

(set file "#?")
(debug "file " file)
(set archive (tackon inst_disk (cat product "_dir")))
(debug "Archive: " archive)
(set destination install_dir)
(transcript "Extracting Distant Suns icons and state files.")
(working "Extracting icons and state files to " rpath)
(extract)
(complete 20)
(working "Setting tooltypes to " res ", " monitor)
(set n 0)
(while (< n 5)
    (
	(set icon
	    (tackon install_dir
		(select n "ds_5.0" "lunar_eclipse" "halleys_comet"
		    "july_91_eclipse" "saturn"
		)
	    )
	)
	(debug "icon " icon " = " res)
	(tooltype
	    (dest icon)
	    (settooltype "RES" res)
	)
	(debug "icon " icon " = " monitor)
	(tooltype
	    (dest icon)
	    (settooltype "MONITOR" monitor)
	)
	(set n (+ n 1))
    )
)

(verify)
;(set @special-msg (cat where_msg "main"))

(set old_data (tackon install_dir "data1/stars/ic_stars1.bin"))
(debug old_data)

(set n 0)
(while (< n 2)
    (
	(set expand_name
	    (cat "SkyMap "
		(select n "10,000" "20,000")
	    )
	)
	(set expand_size
	    (select n 125894 257750)
	)
	(if (= (getsize old_data) expand_size)
	    (
		(set old-level @user-level)
		(user 2)
		(set data1_alert
		    (askbool
			(prompt "Installer detected " expand_name
			    "expansion dataset.  Do you have a SkyMap "
			    "installed at " install_dir "?"
			)
			(help "If you have an expansion set, answer yes.  If "
			    "you don't, answer No."
			)
		    )
		)
		(user old-level)
	     )
	)
	(set n (+ n 1))
    )
)

(if (not data1_alert)
    (
	(set file "#?")
	(debug "file " file)
	(set archive (tackon inst_disk ("data1")))
	(debug "Archive: " archive)
	(set destination install_dir)
	(transcript "Extracting Data set 1 to " rpath)
	(working "Extracting Data Set 1 to " rpath)
	(extract)
    )
)

(set file "#?")
(debug "file " file)
(set archive (tackon inst_disk ("data3")))
(debug "Archive: " archive)
(set destination install_dir)
(transcript "Extracting Data set 3 (Guide Star Catalog) to " rpath)
(working "Extracting Data Set 3 (Guide Star Catalog) to " rpath)
(extract)

(askdisk
    (prompt "Insert the Distant Suns 5.0 data disk.")
    (help @askdisk-help)
    (dest data_name)
)

(if (< mem 2)
    (
	(working "Copying archive to hard drive for faster installation...")
	(set dtemp (tackon install_dir "dtemp"))
	(makedir dtemp)
	(copyfiles
	    (source data_disk)
	    (choices "data2.lzh")
	    (dest dtemp)
	    (safe)
	    (nogauge)
	)
	(set n 0)
	(while (< n 21)
	    (set file
		(cat "data2/"
		    (select n   "anims"
				"states1"
				"user"
				"tables"
				"states2"
				"sod"
				"rexx"
				"plt_orb_els"
				"planet_geo"
				"pix"
				"orbits"
				"mr_images"
				"misc"
				"meteor"
				"lscape"
				"interp"
				"hr_images"
				"gen_orb_els"
				"dso"
				"const"
				"comment"
		    )
		    "/#?"
		)
	    )
	    (debug "file " file)
	    (set archive (tackon dtemp ("data2")))
	    (debug "Archive: " archive)
	    (set destination install_dir)
	    (transcript "Extracting Data set 2, " file " to " rpath)
	    (set real (+ n 1))
	    (working "Extracting Data Set 2, " file " to " rpath "\n\n"
		"(" real " of 21)\n\n"
		"Note: Data2 could take up to 40 min to extract...\n\n"
		"This would be a good time to fill out your reg card.\n\n"
	    )
	    (extract)
	    (set n (+ n 1))
	)
	(set clean_up (cat "delete " dtemp "#? all"))
	(debug "Clean w/ " clean_up)
	(run clean_up)
    )
    (
	(set file "#?")
	(debug "file " file)
	(set archive (tackon data_disk ("data2")))
	(debug "Archive: " archive)
	(set destination install_dir)
	(transcript "Extracting Data set 2, to " rpath)
	(working "Extracting Data Set 2, to " rpath "\n\n"
	   "Note: Data2 could take up to 40 min to extract...\n\n"
	   "This would be a good time to fill out your reg card.\n\n"
	)
	(extract)
    )
)

(complete 60)
(set fsi (tackon install_dir "data2/fsi_images"))
(if (bitand image 1)
    (
	(askdisk
	    (prompt "Please insert the first images disk (you don't need "
		    " to click on Proceed)."
	    )
	    (dest images_name)
	    (help @askdisk-help)
	)
	(transcript "Installing DS 4.0 images.")
	(set images (tackon images_disk "data2/fsi_images"))
	(copyfiles
	    (all)
	    (source images)
	    (dest fsi)
	    (prompt "Copy the following files to your hard drive...")
	    (help @copyfiles-help)
	)
	(set inst_imag 1)
	(if (= @pretend 1)
	    (message "Installer pretended to copy images to " fsi)
	)
    )
)

(complete 75)
(if (bitand image 2)
    (
	(askdisk
	    (prompt "Please insert the first AGA images disk (you don't need "
		    " to click on Proceed)."
	    )
	    (dest aga_name)
	    (help @askdisk-help)
	)
	(set images (tackon AGA_disk "data2/fsi_images"))
	(copyfiles
	    (all)
	    (source images)
	    (dest fsi)
	    (prompt "Copy the following files to your "
		    "hard drive..."
	    )
	    (help @copyfiles-help)
	)
	(complete 90)
	(askdisk
	    (prompt "Please insert the second AGA images disk (you don't need "
		    " to click on Proceed)."
	    )
	    (dest aga_name2)
	    (help @askdisk-help)
	)
	(set images (tackon AGA_disk2 "data2/fsi_images"))
	(copyfiles
	    (all)
	    (source images)
	    (dest (tackon install_dir "data2/fsi_images"))
	    (prompt "Copy the following files to your "
		    "hard drive..."
	    )
	    (help @copyfiles-help)
	)
	(if (bitand image 1)
	    (
		(delete (tackon fsi "mars"))
		(copyfiles
		    (source (tackon AGA_disk2 "exceptions"))
		    (dest fsi)
		    (choices "mars_both")
		    (nogauge)
		)
		(rename (tackon fsi "mars_both") (tackon fsi "mars"))
	    )
	)
	(set inst_imag 1)
	(if (= @pretend 1)
	    (message "Installer pretended to copy images disk to " images)
	)
    )
)

(if (and (= space_v 1) (bitand image 2))
    (
	(askdisk
	    (prompt "Insert the Space Visions Install disk (S11)")
	    (help @askdisk-help)
	    (dest "S11")
	)
	(set n 0)
	(while (< n 4)
	    (set text (select n "earth" "jupiter" "mars" "venus"))
	    (set new
		(select n
		    "adonarai.iff"
		    "jupiter.iff"
		    "marsface.iff\nmars.iff"
		    "venus.iff\nvenus2.iff"
		)
	    )
	    (delete (tackon fsi text))
	    (textfile
		(prompt "Merge " text " images with new images?")
		(help @textfile-help)
		(dest (tackon fsi text))
		(include (tackon "S11:S11" text))
		(append new)
	    )
	    (set n (+ n 1))
	)
    )
)

(complete 95)
(if (= @pretend 0)
    (
	(startup "Distant Suns"
	    (prompt "Install needs to add an assign for the full "
		    "screen images to your startup."
	    )
	    (help "Do this so Distant Suns will work properly.")
	    (command
		(cat "assign ds_fsi: "
		    (tackon install_dir "data2/fsi_images")
		)
;	    If this ever gets added back in, add a \n to the string above.
;		 (cat "assign ds_gsc: "
;		     (tackon install_dir "data3/GSC")
;		 )
	    )
	)
    )
    (
	(message "Installer pretended to write an assign statement in your "
		 "user-startup file."
	)
    )
)

(user 2)
(message "If you get a message that says 'unable to open window 0', quit "
	"Distant Suns, exit any programs and reboot.  Change "
	"your screen/resolution mode down to a lower level by setting ds_5.0 "
	"icon tooltypes to:\n"
	"      MONITOR=(blank)\n"
	"      RES=med\n"
)
(message "Before using Distant Suns, you must reboot.")

(set @default-dest install_dir)
(complete 100)
(set @execute-dir (tackon inst_disk "c"))
(set clean_up ("delete ram:ds#? all"))
(run (clean_up))
