; 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 "")

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

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

(set rpath ("ram:DS%s" ver))
(makedir rpath (safe))

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

(onerror
    (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).
;

(set ram_c "ram:ds_c")
(makedir ram_c)
(if (< wb 2)
    (set c ":c_13")
    (set c ":c")
)

(copyfiles
    (all)
    (source (cat disk_name c))
    (dest ram_c)
    (safe)
    (nogauge)
)

; 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)

(procedure ensure
    (
	(debug "Checking " file)
	(if (not (exists (cat destination file)))
	    (
		(set stat (cat "Installing " file))
		(working stat)
		(set archive (tackon dat_disk ("ds%s_dir" ver)))
		(debug "ensure extracting " file)
		(extract)
		(set installed n)
	    )
	)
    )
)

(procedure verify
    (
	(set old_rpath rpath)
	(set rpath "ram:support")
	(makedir rpath)
	(set n 0)
	(while (< n 6)
	    (
		(set file
		    (select n "fonts/flow.font" "fonts/flow/8"
			      "fonts/flow_thin.font" "fonts/flow_thin/8"
			      "fonts/symbols.font" "fonts/symbols/9"
		    )
		)
		(set destination "sys:")
		(ensure)
	    (set n (+ n 1))
	    )
	)
	(set rpath old_rpath)
    )
)

(procedure extract
    (
	(set rextract ("%s/lharc -m x %s %s/ %s" ram_c archive rpath file))
	(debug "Extract " rextract)
	(run (rextract))
	(debug "Copying " rpath " to " destination)
	(copyfiles
	    (all)
	    (source rpath)
	    (dest destination)
	    (safe)
	)
	(set clean_up ("%s/delete %s/#? all" ram_c rpath))
	(debug clean_up)
	(run clean_up)
    )
)

(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 upgrade 0))
	    (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)

(set old-level @user-level)
(user 1)
(if (= hard_drive 1)
    (
	(set upgrade
	    (askchoice
		(prompt "Are you upgrading from a previous version of Distant Suns? "
			"If you want this version to replace the old version, select "
			"Upgrade.  If you want to install this version as if Distant "
			"Suns had never been installed on your system, then select "
			"Install. "
		)
		(choices "Install" "Upgrade")
		(help @askchoice-help)
	    )
	)
    )
)
(debug "upgrade is " upgrade)

(if (= upgrade 1)
    (set upgrade_from
	(askchoice
	    (prompt "Which version of Distant Suns are you upgrading from?")
	    (help @askchoice-help)
	    (choices "3.0" "4.0 or higher")
	)
    )
)

(if (and (= upgrade 1) (= upgrade_from 0))
    (message "If you didn't receive an images disk with your upgrade from Distant Suns "
	    "3.0, please call us at 805 545 8515.  You may complete this Install without "
	    "the image disk it is optional.")
)

(user old-level)

(if (= hard_drive 1)
    (
	(if (= upgrade 0)
	    (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 one meg "
		   "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 (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, 1000, 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, you have a math coprocessor.  Certain "
		  "inexpensixe versions of the 4000 don't have a math co-processor. "
		  "If you aren't sure, then select no.")
	    (default prob_fpu)
	    (choices "No" "Yes")
	)
	"fpu"
	"ffp"
    )
)
(debug "Math: " math)

(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 "Insert 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)
	)
	(set clean_up ("ram:ds_c/delete %s/data1#? all" rpath))
	(debug clean_up)
	(run (clean_up))
	(set clean_up ("ram:ds_c/delete %s/#?" rpath))
	(debug clean_up)
	(run (clean_up))
    )
)

(if (= 1 floppy_boot)
    (
	(askdisk
	    (dest dat_name)
	    (prompt "Insert the Distant Suns 4.2 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 "Insert the Distant Suns data disk.")
	    (help @askdisk-help)
	)
	(askdisk
	    (dest "Empty")
	    (prompt "Insert 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 "Reinsert the first blank floppy disk (this will be your new 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 "Insert the Distant Suns 4.2 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)
	)
	(set clean_up ("ram:ds_c/delete %s/#? all" rpath))
	(debug clean_up)
	(run (clean_up))
    )
)

(if (and (= upgrade 0)(= 1 hard_drive))
    (
	(set old-level @user-level)
	(user 1)
	(message "Please insert the Distant Suns image disk into df0:, and click on the "
		 "Proceed button.  If you are upgrading from a previous version, use "
		 "the image disk from that version (they are the same)."
	)
	(user old-level)
	(transcript "Copying images to " install_dir)
	(copyfiles
	    (all)
	    (source "df0: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/fsi_images" install_dir))
	)
    )
)

(verify)

(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 "Insert the first blank disk again.  This will be your 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)
	)
	(run clean_up)
	(user 1)
	(message "When you want to use Distant Suns 4.2, reboot with the new boot disk in "
	    "floppy drive df0:.  The new boot disk is the first blank floppy that was "
	    "used in this install.  "
	)
    )
)

(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))
