; Installer script for Professional File System 3
; (c) 1999 GREat Effects Development
;

(transcript "Professional File System 3 to 3.3 Upgrade")

(procedure update_tool #updatefile
  (set #current (tackon #tooldir #updatefile))
  (if (exists #current)
    ( (delete #current (optional "force"))
      (do_patch
	    (tackon "patch" (cat #updatefile ".pch"))
	    (tackon (tackon #sourceroot "tools") #updatefile)
	    #current
      )
	)
  )
)

(procedure update_doc #updatefile
  (set #current (tackon #docdir #updatefile))
  (if (exists #current)
    ( (delete #current (optional "force"))
	  (do_patch
	    (tackon "patch" (tackon #language (cat #updatefile ".pch")))
	    (tackon (tackon #sourcemain "docs") #updatefile)
	    #current
	  )
	)
  )
)

; check upgrade
(procedure pfs_upgrade
  (message "This is the PFS2 to PFS3 upgrade version of PFS3. To "
        "install this upgrade the PFS2 files need to be installed "
        "in the l: directory. If you no longer have these files, "
        "they can be recreated by running the install script on "
        "your PFS2 CD. This does not effect you current PFS3 "
        "installation. "
        "NOTE: just run install, do NOT edit your RDB or format "
        "any disks!"
  )
  (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 "pfs3:c/getserial" #cdkey #cdkey2)
      )
      (if (= 0 (BITAND #licence 16))
	    (message "Invalid CD-Key")
      )
	 )
	)
	(abort "PFS2 is not installed"
	)
  )
)


(procedure install_fs #installfile #sourcefile #help
  (set #current (tackon "l:" #installfile))
  (if (exists #current)
    (delete #current (optional "force"))
  )
  (do_patch
    (cat (tackon "patch" (tackon #language #sourcefile)) ".pch")
    (tackon (tackon #sourcemain "fs") #sourcefile)
    #current
  )

  (protect #installfile 0)

  (set #ok 0)
  (while (= #ok 0)
    (set #rc
	  (run "pfs3:c/getserial" #cdkey #cdkey2 (tackon "l:" #installfile) #username)
    )
    
	(if (= #rc 4)
	  (pfs_upgrade)
	)

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

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

(procedure do_patch #patchfile #source #dest
  (if (exists #source)
    ( (set #temp (tackon "t:" (fileonly #dest)))
	  (if (= 0 (run "pfs3:c/spatch" (cat "-o" #temp) (cat "-p" #patchfile) #source
	      (prompt "Patching " #source)))
	    ( (copyfiles
	        (prompt (cat "Upgrading " #dest))
		    (source #temp)
		    (dest (pathonly #dest))
		    (newname (fileonly #dest))
		    (optional "force")
		  )
		  (delete #temp (optional "force"))
   	    )
	    (abort #dest " could not be patched. "
	    )
  	  )
	)
    (abort (fileonly #source) " not found")
  )
)

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

;-----------------------
; Get pfs3 disk
;
(while (not (exists "pfs3:" (noreq)))
  (message "Please insert your original PFS3 CD"
    (all)
  )
)

;-----------------------
; 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 "pfs3: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 #sourceroot "pfs3:")
(set #sourcemain (tackon #sourceroot (tackon "data/" #language)))

;--------------------------
; Ask where V5 is installed
;
(set #destdir "Workbench:tools/pfs3")
(set #destdir
  (askdir
    (prompt "Select the directory where PFS3 5.0 "
		  "documentation and tools are installed. ")
    (help	@askdir-help)
    (default #destdir)
  )
)
(while (not (exists #destdir))
  (set #destdir
    (askdir
      (prompt "Select the directory where PFS2 5.0 "
			  "documentation and tools are installed. ")
      (help	@askdir-help)
	  (default #destdir)
    )
  )
)

(set @default-dest #destdir)
	    		
(set #docdir (tackon #destdir "doc"))
(set #tooldir (tackon #destdir "tools"))

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

;-------------------------
; 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 muPFS3
;
(if (<> 0 (BITAND #fs_options mask_multiuser))
  ( (install_fs
	  "mupfs3"
	  (cat "muPFS3" #cpuext)
	  "Installation of multiuser version"
	)
  )
)

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


;-------------------------
; Update Tools
;
(update_tool "pfsdoctor")
; (update_tool "diskvalid")
; (update_tool "makelink")
; (update_tool "makerollover")
; (update_tool "pfs2ls")
; (update_tool "removedirentry")
; (update_tool "setrollover")

;-------------------------
; Update docs
;
; (update_doc "diskvalid.guide")
; (update_doc "pfs2.guide")
; (update_doc "pfs2.h")
; (update_doc "pfs2ls.guide")
; (update_doc "pfs2packets.guide")
; (update_doc "pfs2tools.guide")
; (update_doc "Proginfo")

(exit
)
