; Installer script for Professional File System 3
; (c) 1999 GREat Effects Development
; written by Michiel Pelt
;

(transcript "Professional File System 3 Installation")

; check upgrade
(procedure pfs_upgrade
  (message "This is the upgrade version of PFS3. Make sure PFS2 is "
		"installed before installing. You will need the PFS2 CD-key "
		"to finish this installation"
  )
  (if (or (exists "l:pfs2") 
		(or (exists "l:pfs2ds") 
		  (or (exists "l:mupfs2") (exists "l:mupfs2ds"))))
    (
	 (while (= 0 (BITAND #licence 16))
      (set #cdkey2
        (asknumber
    	  (prompt "Upgrade: Please enter the PFS2 CD-key")
    	  (help (cat "The CD-Key is the number on the sticker on the front of the CD case."
    		    "The PFS2 key is expected here, not the PFS3 key")
		  )
		  (default 0)
	    )
	  )
      (set #licence 
	    (run "c/getserial" #cdkey #cdkey2)
      )
      (if (= 0 (BITAND #licence 16))
	    (message "Invalid CD-Key")
      )
	 )
	)
	(abort "PFS2 is not installed"
	)
  )
)


(procedure install_fs #installfile #sourcefile #help
  (copyfiles
    (prompt "Copying " #installfile)
    (help #help)
    (source (tackon (tackon #mainpath "fs") #sourcefile))
    (dest "l:")
	(newname #installfile)
	(optional "force" "askuser")
  )

  (protect #installfile 0)

  (set #ok 0)
  (while (= #ok 0)
	(debug "call is " #cdkey #cdkey2 #installfile #username)
    (set #rc
	  (run "c/getserial" #cdkey #cdkey2 (tackon "l:" #installfile) #username)
    )
    
	(debug "rc is " #rc)

	(if (= #rc 4)
	  (pfs_upgrade)
	)

	(if (= #rc 3)
	  (abort "Installation failed")
	)

	(if (= #rc 2)
	  (set #ok 1)
	)
  )
)

(procedure dopatch #patchfile #target
  (if (askbool
	    (prompt "Patching " #target)
	    (help "Choose proceed to patch " #target ". The patch makes "
		  "it possible to use PFS2 with multiuser. For the patch to "
		  "succeed the original, not previously patched, version "
		  "of " #target " is needed")
	    (default 1)
	    (choices "Proceed" "Cancel"))

	(if (exists #target)
	  (if (= 0 (run "c/spatch" "-ot:temp" (cat "-p" #patchfile) #target
		    (prompt "Patching " #target)))
		(copyfiles
		  (source "t:temp")
		  (dest (pathonly #target))
		  (newname (fileonly #target))
		  (optional "force")
   	 	)
		(abort #target " could not be patched. Please copy the original,"
		  " not patched, multiuser.library to libs: and try again"
		)
  	  )
	  (abort (fileonly #target) " not found")
	)
  )
)

;-----------------------
; Welcome
;
(welcome "Welcome to the Professional File System 3 Installation"
)


;-----------------------
; Ask for serial number
;
(set #tmp @user-level)
(set #licence 0)
(set #cdkey2 0)
(if (= @user-level 0)
  (user 1)
)
(while (= #licence 0)
  (set #username
	(askstring
		(prompt "Please enter your full name")
		(help "To register PFS3 your name is needed")
	)
  )

  (set #cdkey
	(asknumber
		(prompt "Please enter your CD-key")
		(help "The CD-Key is the number on the sticker at the back of the CD case.")
		(default 0)
	)
  )

  (set #licence 
	(run "c/getserial" #cdkey)
  )

  (if (= #licence 0)
	(message "Invalid CD-Key")
  )
)

;--------------------------
; upgrade
;
(if (<> 0 (BITAND #licence 8))
  (pfs_upgrade)
)

(set #langnr (BITAND #licence 7)
)

(set #language "eng"
)

(if (= #langnr 2)
  ( 
	(set #language "ger")
    (message "Key accepted. Installing German version"
	  (all)
    )
  )
)

(if (= #langnr 3)
  ( 
	(set #language "fra")
    (message "Key accepted. Installing French version"
	  (all)
    )
  )
)

(if (= #langnr 4)
  (
	(set #language "ita")
	(message "Key accepted. Installing Italian version"
	  (all)
	)
  )
)

(if (= #langnr 1)
  (
    (set #language "eng")

	(if (> @user-level 1)
      (
		(set #langnr
          (askchoice
            (prompt "Which language do you prefer")
 	        (help @askchoice-help) 
            (choices
              "English"
              "Italian"
 	        )
 	        (default 0)
 	      )
		)
 	    (if (= #langnr 1)
 	      (set #language "ita")
 	    )
	  )
    )
  )
)

(user #tmp)

;--------------------------
; Some defaults
;
(set #root "")
(set #mainpath (tackon #root (tackon "data/" #language)))

;--------------------------
; Ask where to install
;
(set #destdir
  (askdir
    (prompt "Select a directory where you would like to install PFS3 "
    		"documentation and tools. A directory 'PFS3' will be created "
			"here")
    (help	@askdir-help)
	(default "Workbench:tools")
  )
)

(set #destdir (tackon #destdir "pfs3"))
(if (not (exists #destdir))
  (makedir #destdir 
	(infos)
  )
)
(set @default-dest #destdir)
	    		
(set #docdir (tackon #destdir "doc"))
(set #tooldir (tackon #destdir "tools"))
(set #driverdir ("sys:Storage/DosDrivers"))

;--------------------------
; Get CPU
;
(set cpu_any	0)
(set cpu_68020	1)
(set cpu_68040	2)
(set cpu_68060	3)
(set #cpu 0)
(set #cputext (database "cpu"))
(if (patmatch "68000|68010" #cputext) (set #cpu cpu_any))
(if (patmatch "68020|68030" #cputext) (set #cpu cpu_68020))
(if (patmatch "68040" #cputext) (set #cpu cpu_68040))
(if (patmatch "68060" #cputext) (set #cpu cpu_68060))

; Ask expert user for CPU
(if (> @user-level 1)
  (set #cpu
	(askchoice 
	  (prompt
		(cat "Select for which processor you would like to install: " #cputext))
	  (help @askchoice-help) 
	  (choices
		"For 68000 Or 68010"
   		"For 68020 Or 68030"
		"For 68040"
		"For 68060"
      )
	  (default #cpu)
	)
  )
)

(set #cpuext "");
(if (= #cpu cpu_68020) (set #cpuext "-020"))
(if (= #cpu cpu_68040) (set #cpuext "-040"))
(if (= #cpu cpu_68060) (set #cpuext "-060"))

;-------------------------
; Get filesystem options
;
(set mask_normal	1)
(set mask_ds		2)
(set mask_multiuser 4)
(set mask_dsmultiuser 8)
(set mask_floppy	16)
(set #fs_options -1)
(if (not (exists "libs:multiuser.library"))
  (set #fs_options 
	(BITAND #fs_options (BITNOT (BITOR mask_multiuser mask_dsmultiuser)))
  )
)
(if (> @user-level 1)
  (set #fs_options
 	(askoptions
	  (prompt "Select which versions you would like to install:")
	  (help @askoptions-help)
	  (choices
		"Normal version"
		"DirectScsi version"
		"Multiuser version"
		"DirectScsi Multiuser version"
		"Floppy version"
	  )
	)
  )
)



;-------------------------
; Get documentation
;
(set mask_guide 1)
(set #manual -1)
(if (> @user-level 1)
  (set #manual
	(askoptions
	  (prompt "Select the documentation to install:")
	  (help @askoptions-help)
	  (choices
	    "Amigaguide documentation"
	  )
	)
  )
)
	
;-------------------------
; Install PFS3
;
(if (<> 0 (BITAND #fs_options mask_normal))
  ( (install_fs
	  "pfs3"
	  (cat "PFS3" #cpuext)
	  "Installation of default version"
	)
  )
)

;-------------------------
; Install PFS3ds
;
(if (<> 0 (BITAND #fs_options mask_ds))
  ( (install_fs
	  "pfs3ds"
	  (cat "PFS3" #cpuext "ds")
	  "Installation of direct scsi version"
	)
  )
)

;-------------------------
; Install multiuser patch
;
(if (<> 0 (BITAND #fs_options (BITOR mask_multiuser mask_dsmultiuser)))
  (dopatch
	(cat "patch/multiuser" #cpuext ".pch")
	  "libs:multiuser.library"
  )
)

;-------------------------
; Install muPFS3
;
(if (<> 0 (BITAND #fs_options mask_multiuser))
  ( (install_fs
	  "mupfs3"
	  (cat "muPFS3" #cpuext)
	  "Installation of multiuser version"
	)
  )
)

;-------------------------
; Install muPFS3ds
;
(if (<> 0 (BITAND #fs_options mask_dsmultiuser))
  ( (install_fs
	  "mupfs3ds"
	  (cat "muPFS3" #cpuext "ds")
	  "Installation of direct scsi multiuser version"
	)
  )
)

;-------------------------
; Install floppyPFS
;
(if (<> 0 (BITAND #fs_options mask_floppy))
  ( 
	(install_fs
	  "floppyPFS"
	  "floppyPFS"
	  "Installation of floppy version"
	)

    (copyfiles
	  (prompt "Installing dosdrivers")
	  (help @copyfiles-help)
	  (source (tackon #root "devs"))
	  (dest #driverdir)
	  (all)
	  (infos)
	  (confirm)
    )
  )
)

;-------------------------
; Install Tools
;
(copyfiles
  (prompt "Installing tools")
  (help @copyfiles-help)
  (source (tackon #root "tools"))
  (dest #tooldir)
  (all)
  (infos)
  (confirm)
)

;-------------------------
; Install docs
;
(if (<> 0 (BITAND #manual mask_guide))
  (copyfiles
    (prompt "Installing documentation in guide form")
    (help "The AmigaGuide files installed are the usermanual, "
    	  "the tools manuals and the programming information"
    )
    (source (tackon #mainpath "doc"))
    (dest #docdir)
    (all)
	(infos)
    (confirm)
  )
)

;-------------------------
; Install progress library
;
(copylib
  (prompt "Installing progress gadget")
  (help @copylib-help)
  (source (tackon #root "classes/gadgets/progress.gadget"))
  (dest "sys:classes/gadgets")
  (optional 'force')
)

;-------------------------
; Install HDInstTools
;
(copyfiles
  (prompt "Installing HDInstTools")
  (help @copyfiles-help)
  (source (tackon #root "HDInstTools/HDInstTools"))
  (dest #destdir)
  (infos)
  (confirm)
)

(copyfiles
  (prompt "Installing HDInstTools documentation")
  (help @copyfiles-help)
  (source (tackon #root "HDInstTools/Docs"))
  (dest #docdir)
  (all)
  (infos)
  (confirm)
)

(exit
)
