;
; MIMEhandlerII.install
;

(working "Installing MIMEhandlerII")

(message "\n\nMIMEhandlerII is freeware.\nDo consult the guide for further information")

(set @default-dest "RAM:")
(set #error-mess "This script requires Installer V42.7 which you do not seem to have.")
(set #welcome "Welcome to the installation of MIMEhandlerII\n\nAn excellent MIME handler for YAM with powerful and easy configuration")
(set #put-in-yam-rexx-prompt "The installer script is about to put the script \"MIMEhandlerII.rexx\" in the YAM:rexx/ directory. Is this were you want it?")
(set #unable-to-find-yam-prompt "The installer script was unable to find the YAM: assign. Please choose where you wish to install the script \"MIMEhandlerII.rexx\".")
(set #unable-to-find-yam-help (cat #unable-to-find-yam-prompt " This is usually the directory in which you installed YAM, in its subdirectory \"rexx\"."))
(set #install-docs-prompt "Would you like to install the documentation in Amigaguide format?")
(set #install-docs-where-prompt "Where would you like to install the documentation?")

(set #install-dir-script "YAM:rexx/")
(set #install-dir-docs "YAM:")

;----------------------------------------
; Ask the user where to put the docs
(procedure P_install-docs
    (set #install-dir-docs
         (askdir
              (prompt #install-docs-prompt)
              (disk)
              (default "")
              (help @askdir-help)
         )
    )
)

;----------------------------------------
; Check versions for files
(procedure P_check-version #dest_path #file #installing
    (if (and (= (exists (#dest_path)) 1) (= #installing 1))
         (
              (set #vernum (getversion (#dest_path)))
              (set #old_v (/ #vernum 65536))
              (set #old_r (- #vernum (* #old_v 65536) ))

              (set #vernum (getversion (#file)))
              (set #new_v (/ #vernum 65536))
              (set #new_r (- #vernum (* #new_v 65536) ))

              (set #version-install-prompt (cat "About to install: \"" #file))
              (set #temp-str ("%ld.%ld" #old_v #old_r))
              (set #version-install-prompt (cat #version-install-prompt "\" " #temp-str))
              (set #temp-str ("%ld.%ld" #new_v #new_r))
              (set #version-install-prompt (cat #version-install-prompt "\nAlready installed file: \"" #file))
              (set #version-install-prompt (cat #version-install-prompt "\" " #temp-str))

              (askbool
                   (prompt #version-install-prompt)
                   (choices "Proceed" "Skip")
                   (help "The former file is the one the script is about to install, the latter the one already existing.")
              )
         )
         (
              (+ #installing 0)
         )
    )
)

;--------------------------------------------------------

; We cannot handle this script without Installer 42.7 or better
(if (<= @installer-version 2752519)
    (
         (message #wrongversion)
         (exit (quiet))
    )
)

(welcome #welcome)
(complete 0)

(if (not (exists "YAM:" (noreq)))
    (
         (set #install-dir-script
              (askdir
                   (prompt #unable-to-find-yam-prompt)
                   (help #unable-to-find-yam-help)
                   (disk)
                   (default "")
              )
         )

         (complete 20)

         (set #install-docs
              (askbool
                   (prompt #install-docs-prompt)
                   (choices "Yes" "No")
                   (help "Should documentation be installed or not.")
              )
         )

         (complete 30)

         (if #install-docs
              (P_install-docs)
         )

         (complete 40)

         (set #install-script 1)

         (set #install-script
              (P_check-version
                   (tackon #install-dir-script "MIMEhandlerII.rexx")
                   ("MIMEhandlerII.rexx")
                   #install-script
              )
         )

         (complete 50)

         (set #install-docs
              (P_check-version
                   (tackon #install-dir-docs "MIMEhandlerII.guide")
                   ("MIMEhandlerII.guide")
                   #install-docs
              )
         )

         (complete 60)
    )
    (
         (set #proceed-default-install
              (askbool
                   (prompt #put-in-yam-rexx-prompt)
                   (choices "Proceed" "Change")
                   (help "The YAM: assign was found and thus the default directory to put the script in is \"YAM:rexx/\".\nIf this is something undesirable you are free to choose your own.\n")
              )
         )

         (complete 10)

         (if (not #proceed-default-install)
              (
                   (set #install-dir-script
                        (askdir
                             (prompt "Where should the script be put?")
                             (help "Usually the best alternative is in \"YAM:rexx/\"")
                             (default "YAM:rexx/")
                        )
                   )
              )
         )

         (complete 20)

         (set #install-docs
              (askbool
                   (prompt #install-docs-prompt)
                   (choices "Yes" "No")
                   (help "Should documentation be installed or not.")
              )
         )

         (complete 30)

         (if #install-docs
              (P_install-docs)
         )

         (complete 40)

         (set #install-script 1)

         (set #install-script
              (P_check-version
                   (tackon #install-dir-script "MIMEhandlerII.rexx")
                   ("MIMEhandlerII.rexx")
                   #install-script
              )
         )

         (complete 50)

         (set #install-docs
              (P_check-version
                   (tackon #install-dir-docs "MIMEhandlerII.guide")
                   ("MIMEhandlerII.guide")
                   #install-docs
              )
         )

         (complete 60)
    )
)

(if #install-docs
    (copyfiles
         (source "MIMEhandlerII.guide")
         (dest #install-dir-docs)
         (files)
    )
)

(complete 80)

(if #install-script
    (copyfiles
         (source "MIMEhandlerII.rexx")
         (dest #install-dir-script)
         (files)
    )
)

(complete 100)
(exit "Done installing MIMEhandlerII.\n\nStudy the guide for documentation.")
