; Install Distant Suns
; Prints out debugging info if run from CLI
(set ver "42")
(set ver_pt "4.2")
(set disk_name "DS_Install")
(set dat_name "DS42_data")
(set dat_disk (cat dat_name ":"))
(set @default-dest "")

(message "If you are installing Distant Suns to a floppy, then have two blank, "
	"formatted floppies ready to install to.")

(onerror
    (set clean_up ("ram:ds_c/delete %s all" rpath))
    (run (clean_up))
    (set clean_up ("ram:ds_c/delete ram:ds_c#? all" rpath))
    (run (clean_up))
    (message "Installation Error! If you need Technical Support, please call "
	" (805) 781-2250.  Double Click on the install-log-file icon, and be ready "
	"to read it to the support specialist."
    )
)

(transcript "Installing Distant Suns" ver_pt)

; Set up commands to be used in installation (the old copy to ram: trick).
;
(makedir "ram:ds_c")
(copyfiles
    (all)
    (source (cat disk_name ":c"))
    (dest "ram:ds_c")
    (safe)
)

; I set the user level in order to allow novice users to make an installed
; self-booting copy of Distant Suns.
;
(set old_level @user-level)
(user 1)

(set install_dir "")
(while (= "" install_dir)
    (set install_dir
	(askdir
	    (prompt "Where would you like to install Distant Suns " ver_pt "? "
		    "If you are installing Distant Suns to a floppy, put in the "
		    "Empty disk now, click on the Show Drives button, and select "
		    "Empty.")
	    (help "If you already have Distant Suns, and wish to upgrade, "
		  "then select directory where Distant Suns already exists "
		  "(where the two directories DATA1 and DATA2 are).  "
		  "If you wish to create a boot disk that starts Distant Suns "
		  "automatically, then select a floppy named Empty "
		  " as your destination.")
	    (default @default-dest)
	)
    )
)
(debug "Install dir " install_dir)

; This assumes that if you are installing to a device named Empty, it's a floppy.
(if (= install_dir "Empty:")
    (
	(set floppy_boot
	    (askbool
		(prompt ("Create a self-booting floppy for Distant Suns %s?" ver_pt))
		(help "If you select Yes, then this install will create a self-booting "
		      "floppy that starts Distant Suns automatically.")
		(choices "Yes" "No")
		(default 1)
	    )
	)
	(if (and
		(= floppy_boot 1)
		(>= (getdiskspace (install_dir)) 800000)
	    )
	    ((set confirm_type 1)(set install_type "Install"))
	    (abort "Not enough disk space to install Distant Suns on your floppy.  "
		   "For best results, start with a newly-formatted blank floppy.  "
		   "Remember, you are going to need another blank floppy for the data "
		   "disk as well. "
	    )
	)
    )
    ((set confirm_type 0))
)
(debug "Floppy boot" floppy_boot)
(user old_level)

(if (not floppy_boot)
    (set hard_drive
	(askbool
	    (prompt "Is the path " install_dir " on  a hard drive?")
	    (help @askbool-help)
	    (default 1)
	)
    )
    (set hard_drive 0)
)
(debug "Hard drive " hard_drive)

; For anyone that's interested, I ran into problems nesting the following logic,
; and that's why it looks like this. -TF

(set upgrade_from "")
(while (not confirm_type)
    (set ds_4.1 (exists (tackon install_dir "ds_4.1")))
    (debug "exists 4.1 " ds_4.1)
    (set ds_4.0 (exists (tackon install_dir "ds_4.0")))
    (debug "exists 4.0 " ds_4.0)
    (set ds_3.0 (exists (tackon install_dir "ds_3.0")))
    (debug "exists 3.0 " ds_3.0)
    (if (= 1 ds_3.0)
	((set upgrade_from "30")(set install_type "Upgrade")(set confirm_type 1))
	(debug "Couldn't find 3.0")
    )
    (if (= 1 ds_4.0)
	((set upgrade_from "40")(set install_type "Upgrade")(set confirm_type 1))
	(debug "Couldn't find 4.0")
    )
    (if (= 1 ds_4.1)
	((set upgrade_from "41")(set install_type "Upgrade")(set confirm_type 1))
	(debug "Couldn't find 4.1")
    )
    (if (not confirm_type)
	(
	    (set new_install
		(askbool
		    (prompt "It appears that Distant Suns is not already installed where you selected.  "
			    "Do you wish to install it at      " install_dir "?")
		    (help "The installer couldn't find Distant Suns where "
			  "you are installing it, and needs to know if this "
			  "is a new install.  If you have never installed Distant Suns before, "
			  "then select Yes.  If you are upgrading an earlier version "
			  "then select No, and you will be prompted for the directory that "
			  "contains Distant Suns.")
		    (default 0)
		)
	    )
	(if (= 0 @user-level) (set new_install 1) (debug "Not a novice, prompt for path. "))
	)
	(debug "Confirmed.")
    )
    (if (and (= 0 new_install) (= "" upgrade_from))
	(set install_dir
	    (askdir
		(prompt "Where would you like to install Distant Suns " ver_pt)
		(help "If you already have Distant Suns, and wish to upgrade, "
		      "then select the directory where Distant Suns already exists "
		      "(where the two directories DATA1 and DATA2 are).  "
		      "If you are installing for the first time to a floppy drive, "
		      "then select an empty floppy as your destination. ")
		(default install_dir)
	    )
	)
	((set install_type "Install")(set confirm_type 1))
    )
)
(set @default-dest install_dir)
(debug "Install dir" install_dir)
(debug "Install type " install_type)
(debug "Upgrade from " upgrade_from)

(if (= hard_drive 1)
    (
	(if (= install_type "Install")
	    (set space_needed 2048000) ; about two megs
	    (set space_needed 512) ; only about 512k of new stuff
	)
	(if (> space_needed (getdiskspace(install_dir)))
	    (abort "Not enough disk space on your hard drive.  Distant Suns needs "
		   "around two megs of hard drive space for a new install, and about 512k "
		   "for an upgrade of a previous version.  Clean up your hard drive, and "
		   "re-install."
	    )
	)
    )
)

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

(set math
    (if
	(askchoice
	    (prompt "Does your system have a math co-processor?")
	    (help "If you have an Amiga 500, 1000, 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"
    )
)
(debug "Math: " math)

(set workbench (getversion))
(set work (/ workbench 65536))
(set wb (if (>= work 37) "20" "13"))
(debug "WB: " wb)

(set rpath ("ram:DS%s" ver))
(if (exists rpath) (ram:ds_c/delete rpath) (debug "creating " rpath))
(makedir rpath (safe))

(set archive ("%s:ds%swb%s" disk_name ver wb))
(debug "Archive: " archive)

(set flavor ("%s%s" wb math))
(debug "Flavor: " flavor)

(set pextract ("ram:ds_c/lharc -m x %s %s/ ds/%s" archive rpath flavor))
(debug "Extract " pextract)

(transcript "Extracting version " flavor)
(working ("Extracting version %s." flavor))
(run (pextract))

(askdisk
    (dest dat_name)
    (prompt "Enter the Distant Suns data disk.")
    (help @askdisk-help)
)
(set archive (tackon dat_disk ("ds%s_dir" ver)))
(set dextract ("ram:ds_c/lharc -m x %s %s/ ds/#?" archive rpath))
(debug "Extract " dextract)

(transcript "Extracting data1. ")
(working "Extracting first set of data files.")
(run (dextract))

(set rpath (if (not floppy_boot) (tackon rpath "ds") (rpath)))

; Copy the appropriate program to the installation directory, and rename it
; to DS_X.X (where X.X is the version).

(if (not @pretend)
    (
	(copyfiles
	    (all)
	    (infos)
	    (source rpath)
	    (dest install_dir)
	)
    (run "ram:ds_c/delete rpath#? all")
    )
)

(if (= 1 floppy_boot)
    (
	(askdisk
	    (dest dat_name)
	    (prompt "Enter the Distant Suns data disk.")
	    (help @askdisk-help)
	)
	(set archive (tackon dat_disk ("disk_%s" wb)))
	(set fextract ("ram:ds_c/lharc -m x %s %s #?" archive install_dir))
	(debug "Extract " fextract)
	(transcript "Extracting floppy for " wb)
	(working "Extracting files for a bootable floppy...")
	(run (fextract))
	(set n 0)
	(while (< n 4)
	    (debug "Loop " n)
	    (set try (select n "df0" "df1" "df2" "df3"))
	    (debug "Trying " try)
	    (if (= install_dir (getassign try "d")) (set found try))
	    (set n (+ n 1))
	)
	(debug "Found " found)
	(set vol (cat found ":"))
	(run ("ram:ds_c/relabel %s ds4" install_dir))
	(set install_dir "ds4:")
	(run ("ram:ds_c/install %s" vol))
	(set @default-dest install_dir)
	(set archive (tackon dat_disk ("ds%s_dir" ver)))
	(makedir ("%sfonts" install_dir) (safe))
	(set dextract ("ram:ds_c/lharc -m x %s %s fonts/#?" archive install_dir))
	(debug "Extract " dextract)
	(working "Extracting fonts.")
	(transcript "Extracting fonts with " dextract)
	(run (dextract))
	(askdisk
	    (dest dat_name)
	    (prompt "Enter the Distant Suns data disk.")
	    (help @askdisk-help)
	)
	(askdisk
	    (dest "Empty")
	    (prompt "Enter your second blank floppy (this will be your new data disk).")
	    (help @askdisk-help)
	)
	(set archive (tackon dat_disk "full_data"))
	(set dextract ("ram:ds_c/lharc -m x %s Empty: #?" archive))
	(debug "Extract " dextract)
	(transcript "Extracting data2. ")
	(working "Extracting second set of data files.")
	(run (dextract))
	(run ("ram:ds_c/relabel Empty: ds_data"))
	(askdisk
	    (dest "ds4")
	    (prompt "Enter the new Distant Suns boot disk.")
	    (help @askdisk-help)
	)
    )
    (debug "Not a floppy-boot")
)

(set install_d (if (= 1 floppy_boot) (tackon install_dir "ds") (install_dir)))
(set old (tackon install_d ("%s%s" WB math)))
(debug "Old " old)
(set new (tackon install_d ("ds_%s" ver_pt)))
(debug "New " new)
(rename old new)

(if (= 1 hard_drive)
    (
	(askdisk
	    (dest dat_name)
	    (prompt "Enter the Distant Suns data disk.")
	    (help @askdisk-help)
	)
	(set ddest (tackon install_dir "data2"))
	(set dpath (tackon rpath "data2"))
	(debug "Making " dpath)
	(makedir dpath)
	(set archive (tackon dat_disk "full_data"))
	(set dextract ("ram:ds_c/lharc -m x %s %s/ #?" archive rpath))
	(debug "Extract " dextract)
	(transcript "Extracting data2. ")
	(working "Extracting second set of data files.")
	(run (dextract))
	(transcript "Copying data2 files to " install_dir)
	(debug "Attempting to copy " dpath " to " ddest)
	(copyfiles
	    (all)
	    (source dpath)
	    (dest ddest)
	    (infos)
	)
    )
)

(if (and ((= "Install" install_type)(= 1 hard_drive)))
    (
	(askdisk
	    (dest "dx_fsi")
	    (prompt "Enter the Distant Suns image disk.")
	    (help @askdisk-help)
	)
	(transcript "Copying image disk to " install_dir)
	(copyfiles
	    (all)
	    (source "dx_fsi:data2")
	    (dest ("%s/data2" install_dir))
	    (infos)
	)
	(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 ("assign ds_fsi: %s/data2/ds_fsi" install_dir))
	)
    )
)

(if (and (not (exists "fonts:flow.font")) (not floppy_boot))
    (
	(set archive (tackon dat_disk ("ds%s_dir" ver)))
	(set dextract ("ram:ds_c/lharc -m x %s %s fonts/#?" archive rpath))
	(debug "Extract " dextract)
	(run (dextract))
	(set rpath ("%s/fonts" rpath))
	(copyfiles
	    (all)
	    (fonts)
	    (source rpath)
	    (dest "fonts:")
	)
    )
)

(set res (select
	    (askbool
		(prompt "Would you like Distant Suns to start up in hi-res?")
		(help @askbool-help)
	    )
	    "med"
	    "hi"
	)
)

(if (= 1 floppy_boot)
    (
	(askdisk
	    (dest "ds4")
	    (prompt "Enter the new Distant Suns boot disk.")
	    (help @askdisk-help)
	)
	(makedir (tackon rpath "s"))
	(set run_ds ("run >nil: ds_%s -RES %s" ver_pt res))
	(set n 0)
	(set start_file "s/user-startup")
	(set dest_file (tackon rpath start_file))
	(while (< n 1)
	    (
		(set line (select n run_ds))
		(debug "Loop " n)
		(if (= 0 n)
		    (set ins_line (tackon install_dir start_file))
		    (set ins_line dest_file)                  ; this appends
		)
		(debug "inserting " ins_line)
		(debug "Line " line)
		(debug "into " dest_file)
		(textfile
		    (include ins_line)
		    (dest dest_file)
		    (append line)
		)
		(set n (+ n 1))
	    )
	)
	(set src dest_file)
	(set dst (tackon install_dir "s"))
	(if (not (exists dst)) (makedir dst))
	(debug "Source is " src)
	(debug "Dest is " dst)
	(copyfiles
	    (source src)
	    (dest dst)
	)
    )
)

(tooltype
    (dest ("%s/ds_%s" install_dir ver_pt))
    (settooltype "RES" res)
)

(set clean_up ("ram:ds_c/delete %s all" rpath))
(run (clean_up))
(set clean_up ("ram:ds_c/delete ram:ds_c#? all" rpath))
(run (clean_up))
