(set previnstall (getassign "FrexxEd" "a"))

(if previnstall
  (working)
  (abort "We need a previously installed FrexxEd and a FrexxEd: assign!")
)

(set txt_alreadyinstalled "%s is already installed!")

(set txt_askbackup "Backup FPL-programs that are replaced with new ones?")

(set txt_askbackuphelp (cat "Installer is about to copy a lot of FPL-programs "
    "to the FrexxEd:FPL/ directory. If you have done some changes to any "
    "of the programs in the distribution, and would like to keep those "
    "changes, press 'yes' now, and all FPL programs that are replaced with "
    "a different one will be copied into FrexxEd:oldFPL/.  NOTE: This "
    "doesn't include User.FPL. User.FPL is meant to get changed and won't "
    "be updated by installer!"))

(set txt_backupfilestext (cat "The following files were copied to "
                              "this directory when FrexxEd "
                              "was updated:\n"))

(set Origin
   (askfile
      (prompt
       "Enter the complete name and path of your FrexxEd 1.9"
       " library file!")
      (default "FrexxEd:libs/frexxed.library")
      (help
       "To be able to perform this patch, we must know which "
       "frexxed.library to update! Enter the complete path and file name "
       "and press OK!")
   )
)
(set Destination
   (askfile
      (prompt
       "Enter the complete name and path of your to become FrexxEd 1.10"
       " library!")
      (default "FrexxEd:libs/frexxed.library")
      (help
       "To be able to perform this patch, we must know where "
       "to put the updated version! Enter the complete path and file name "
       "and press OK!")
   )
)

(set @default-dest Destination)

(if (= 2 @user-level)
   ; Only experts have the advantage of changing LIBS:!
   (set libdir
      (askdir
         (prompt "Enter drawer/partition to install fpl.library in!")
         (help @askdir-help)
         (default "LIBS:")
      )
   )
   (set libdir "LIBS:")
)

(working)
(run ("spatch -pfred19.pch \"-o%s\" \"%s\"" Destination Origin))

; Copy fpl.library to LIBS:
(copylib
   (prompt "Install fpl.library?")
   (help @copylib-help)
   (source "libs/fpl.library")
   (dest libdir)
   (confirm)
)

; Make a backup-directory and copy all modified files (to preserve
; changes done by the user!) to that!

(set frexxdest previnstall)
(set backupdir (tackon frexxdest "oldFPL"))
(makedir backupdir)
(set destdir (tackon frexxdest "FPL/"))
(set origdir "FPL/")
(set backupchanged (askbool
                      (prompt txt_askbackup)
                      (help txt_askbackuphelp)
                   )
)
(foreach origdir "~(User.FPL)"
  (if (< @each-type 0)
     ; only do this stuff with FILES. Not DIRECTORIES.
     (
       (set destfile (tackon destdir @each-name))
       (set origfile (tackon "FPL" @each-name))
       (if (and backupchanged (exists destfile))
           ; Backuping is wanted and
           ; they both exist. Let's check if they're identical!
           (if (= (getsum destfile) (getsum origfile))
               ; Identical! Tell 'em it's already here!
               (working (txt_alreadyinstalled @each-name))
               ; Different, backup the previous file and copy the
               ; new one
               (
                  (copyfiles
                     (source destfile)
                     (dest backupdir)
                     (infos)
                  )
                  (copyfiles
                     (source origfile)
                     (dest destdir)
                     (infos)
                  )
                  (set allfiles (cat allfiles origfile "\n"))
               )
           )
           ; This is obviously a new file, it was not previously
           ; present! Just copy it.
           (copyfiles
              (source origfile)
              (dest destdir)
              (infos)
           )
       )
     )
   )
)
(if allfiles
   ; There has been some files backuped!
   (textfile
      (dest (tackon backupdir "README"))
      (append (cat txt_backupfilestext allfiles))
   )
)   

(message "done!")
