; Install VistaPro
; Prints out debugging info if run from CLI
(set ver "30")
(set ver_pt "3.0")
(set disk_name "VP_Install")
(set dem_name1 "VP_DEM1")
(set dem_name2 "VP_DEM2")
(set dem_disk1 (cat dem_name1 ":"))
(set dem_disk2 (cat dem_name2 ":"))
;(set @default-dest "")
(set ram_c "ram:vp_c")
(set rpath ("ram:VP%s" ver))
(makedir rpath (safe))
(set mem (database "total-mem"))
(set mem (/ mem 1024000))
(debug "mem " mem)
(if (< mem 4)
    (abort "Your system only has " mem " meg(s) of ram.  VistaPro requires at least four "
	    "megs of ram.  ")
)

(set workbench (getversion))
(set work (/ workbench 65536))
(set wb (if (>= work 37) 2 1))
(debug "WB: " wb)
(if (< work 2)
    (abort "This release of VistaPro requires at least version Workbench 2.04!  Please "
	   "call VRLI at (805) 781 2250 for details on the Workbench 1.3 release."
    )
)

(message "If you wish to install VistaPro to floppies, you will need at least 4 "
	 "formatted blank disks named Empty.")

(onerror
    (set clean_up ("%s/delete %s all" ram_c rpath))
    (run (clean_up))
    (set clean_up ("%s/delete %s all" ram_c ram_c))
    (run (clean_up))
    (message "Installation aborted or an error has occurred! If you need Technical Support, "
	     "please call (805) 781-2250.  Double Click on the install-log-file icon, "
	     "(found on the installation disk) and be ready to read it to the support "
	     "specialist.  Rerun the install, and select the print log."
    )
)

(transcript "Installing VistaPro" ver_pt )

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

(procedure
    get_path (
	(set old_level @user-level)
	(user 1)
	(set install_dir "")
	(while (= install_dir "")
		(set install_dir
		    (askdir
			(prompt "Where would you like to install VistaPro " ver_pt "? "
				"Floppy users: Put in an empty floppy now, then click on the "
				"Show Drives button.  From the list of drives that appears, "
				"select the drive named Empty.  Note: your floppies must formatted "
				"in advance.")
			(help @askdir-help)
			(default @default-dest)
			(disk)
		    )
		)
		(if (not (exists install_dir))(set install_dir ""))
		(if (= @default-dest install_dir)
		    (
			(set install_dir (tackon install_dir (cat "VistaPro" ver_pt)))
			(if (exists install_dir)
			    (
				(message "VistaPro" ver_pt " is already installed at " install_dir "! "
				     "Please pick a new path."
				)
				(set install_dir "")
			    )
			    (makedir install_dir)
			)
		    )
		)
	)
	(debug "Install dir " install_dir)
	(user old_level)
    )
)

; This assumes that if you are installing to a device named Empty, it's a floppy.
(procedure set_floppy
    (
	(if (= install_dir "Empty:")
	    (
		; floppy requires approx 800000 bytes free
		(set floppy 1)
		(set space_req 800000)
	    )
	    (
		; hard drive requires approx 3 megs free.
		(set floppy 0)
		(set space_req 3000000)
	    )
	)
    )
)

(procedure
    check (
	(while (< (getdiskspace install_dir) space_req)
	    (message "The disk you selected doesn't have enough room for the DEMs! "
		     "Please pick another disk, or reformat this one for more room. "
	    )
	    (get_path)
	)
    )
)


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

(procedure disp
    (
	(set disp
	    (askoptions
		(prompt "Please check any of the following display devices you intend to "
			"use with VistaPro" ver_pt)
		(help @askoptions-help)
		(choices "HAM-E" "DCTV")
		(default 0)
	    )
	)
	(debug disp)
	(if (bitand disp 2)
	    (
		(debug "User has DCTV.")
		(set file "libs:DCTV.library")
		(set destination "libs:")
		(ensure)
	    )
	)
	(if (bitand disp 1)
	    (
		(debug "User has Ham-E.")
		(set file "libs:HAME.library")
		(set destination "libs:")
		(ensure)
		(set file "libs:renderhame.library")
		(set destination "libs:")
		(ensure)
	    )
	)
    )
)

(procedure ensure
    (
	(debug "Checking " file)
	(if (not (exists file))
	    (
		(set stat (cat "Installing " file))
		(working stat)
		(set archive (cat disk_name ":support" ))
		(set file (fileonly file))
		(debug "ensure extracting " file)
		(extract)
		(set installed n)
	    )
	)
    )
)

(procedure verify
    (
	(set n 0)
	(while (< n 6)
	    (
		(set file
		    (select n "fonts:topaz.font" "fonts:topaz/11" "libs:diskfont.library"
			      "libs:mathieeedoubbas.library" "libs:mathieeedoubtrans.library"
			      "s:vistapro.prefs"
		    )
		)
		(set destination (pathonly file))
		(if (not (exists destination))(makedir destination))
		(ensure)
		(if (= installed 1)
		    (
			(set fix (tackon ram_c "fixfonts"))
			(run fix)
		     )
		)
	    (set n (+ n 1))
	    )
	)
	(if (>= wb 2)
	    (
		(set file "libs:asl.library")
		(set destination (pathonly file))
		(ensure)
	    )
	)
    )
)

(procedure labeldisk
    (run ("%s/relabel %s %s" ram_c dest_disk new_name))
    (transcript "Relabeling " dest_disk " as " new_name ".")
    (debug "Relabel " dest_disk new_name)
)

(get_path)
(set_floppy)
(check)

(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")
	)
	"881"
	"000"
    )
)
(debug "Math: " math)
(set file (cat "#?." math))
(debug "file " file)
(set archive ("%s:vp%s" disk_name ver))
(debug "Archive: " archive)
(set destination install_dir)
(transcript "Extracting VistaPro and Lyap2dem.")
(working "Installing files VistaPro and Lyap2DEM.")
(extract)

(set file "#?")
(set archive ("%s:readmes" disk_name))
(transcript "Extracting icons and readme files.")
(working "Extracting icons and readme files.")
(extract)

(set stack
    (asknumber
	(prompt "Would you like VistaPro to start with a high stack setting ")
	(help "The default value works fine for small landscapes.  If "
	      "you have 8-12 mb of ram, work with large or huge landscapes, "
	      "create rivers or lakes, or use the new 3D trees, then you might "
	      "want a higher stack value.  A simple guideline to use: "
	      "Small: 50000, Large 75000-100000, and Huge 150000. "
	      "If you have more questions about which stack setting you should use, "
	      "check the index of the VistaPro manual for more information.  "
	)
	(default 50000)
	(range 50000 200000)
    )
)

(set vp_icon (install_dir (cat "VistaPro" ver_pt)))
(tooltype
    (dest vp_icon)
    (setstack stack)
)

(if (= floppy 1)
    (
	(set install_dir "Empty:")
	(set dest_disk install_dir)
	(set new_name (cat "VistaPro" ver_pt))
	(set vp_disk new_name)
	(labeldisk)
	(set install_dir (cat new_name ":"))
    )
)

(set old (tackon install_dir ("VistaPro.%s" math)))
(debug "Old " old)
(set new (tackon install_dir ("VistaPro%s" ver_pt)))
(debug "New " new)
(rename old new)
(set old (tackon install_dir (cat "lyap2dem." math)))
(set new (tackon install_dir "Lyap2DEM"))
(debug old " to " new)
(rename old new)

(askdisk
    (dest dem_name1)
    (prompt "Enter the first VistaPro DEM disk.")
    (help @askdisk-help)
)

(if (= floppy 1)
    (
	(askdisk
	    (dest "Empty")
	    (prompt "Enter another blank disk named Empty for the first set of DEMs.")
	    (help @askdisk-help)
	)
	(set install_dir "Empty:")
	(check)
	(set dest_disk install_dir)
	(set new_name "DEM1")
	(labeldisk)
	(set install_dir (cat new_name ":"))
	(set destination install_dir)
    )
)

(set archive (tackon dem_disk1 "dem1"))
(debug "made it to dem1")
(set file "#?")
(transcript "Extracting first set of dems. ")
(working "Extracting first set of dems.")
(extract)

(askdisk
    (dest dem_name2)
    (prompt "Enter the second VistaPro DEM disk.")
    (help @askdisk-help)
)

(if (= floppy 1)
    (
	(askdisk
	    (dest "Empty")
	    (prompt "Enter another blank disk named Empty for the second set of DEMs.")
	    (help @askdisk-help)
	)
	(set install_dir "Empty:")
	(check)
	(set dest_disk install_dir)
	(set new_name "DEM2")
	(labeldisk)
	(set install_dir (cat new_name ":"))
	(set destination install_dir)
    )
)
(set archive (tackon dem_disk2 "dem2"))
(debug "made it to dem2")
(set file "#?")
(transcript "Extracting second set of dems. ")
(working "Extracting second set of dems.")
(extract)

(if (= floppy 1)
    (
	(askdisk
	    (dest "Empty")
	    (prompt "Enter the last blank disk.")
	    (help @askdisk-help)
	)
	(set install_dir "Empty:")
	(check)
	(set dest_disk install_dir)
	(set new_name "Vistapro_Extras")
	(labeldisk)
	(set install_dir (cat new_name ":"))
	(set destination install_dir)
    )
)

(set archive (tackon dem_disk2 "extras"))
(debug "made it to extras")
(set file "#?")
(transcript "Extracting scripts and tutorials.")
(working "Extracting scripts and tutorials.")
(extract)


(disp)
(verify)

(set @default-dest install_dir)
(set clean_up ("%s/delete %s all" ram_c rpath))
(run (clean_up))
(set clean_up ("%s/delete %s#? all" ramc ramc))
(run (clean_up))
