; $VER: Install ArcDir 1.1 (6.6.97)
; Script to install ArcDir.dopus5 1.1.
; Written by Edmund Vermeulen <edmundv@xs4all.nl>.

(set @default-dest "")

(set #proprender (if (>= @installer-version 42) "[2p" ""))


;=============================================================================
; check if Directory Opus 5 has been installed

(if
   (= (exists "DOpus5:DirectoryOpus" (noreq)) 0)
   (abort "Directory Opus 5 must be installed first.")
)


;=============================================================================
; uninstall

(if
   (=
      (getversion "ARexx/ArcDir.dopus5")
      (getversion "DOpus5:ARexx/ArcDir.dopus5")
   )
   (if
      (=
         (askchoice
            (prompt "ArcDir.dopus5 is already installed.")
            (help @askchoice-help)
            (choices (cat #proprender "Uninstall it.") "Install it again.")
         )
         0
      )
      (
         (delete "DOpus5:ARexx/ArcDir.dopus5")
         (delete "DOpus5:ARexx/NewArc.dopus5")
         (delete "DOpus5:C/ArcDirList")
         (delete "DOpus5:C/Move")
         (delete "DOpus5:C/LhAQuickList")
         (delete "Locale:Catalogs/nederlands/ArcDir.catalog")
         (delete "Locale:Catalogs/deutsch/ArcDir.catalog")
         (delete "Locale:Catalogs/français/ArcDir.catalog")
         (delete "Locale:Catalogs/norsk/ArcDir.catalog")
         (delete "DOpus5:Filetypes/LHA archive")
         (delete "DOpus5:Filetypes/LZX archive")

         (set #storedir
            (if
               (= (exists "DOpus5:Storage/Filetypes") 2)
               "DOpus5:Storage/Filetypes/"
               "DOpus5:Storage/"
            )
         )
         (set #n 0)
         (while
            (set #arctype (select #n "LHA" "LZX" ""))
            (
               (copyfiles
                  (source (cat #storedir #arctype " archive"))
                  (dest "DOpus5:Filetypes")
               )
               (set #n (+ #n 1))
            )
         )

         (exit "ArcDir has been uninstalled.")
      )
   )
)


;=============================================================================
; delete old stuff

(if
   (= (getversion "DOpus5:ARexx/LhADir.dopus5") 65536)
   (message "Version 1.0 of LhADir.dopus5 detected!\n\n"
            "Please note that ArcDir.dopus5 doesn't need any alterations "
            "to the toolbar or any other buttons in Opus whatsoever.\n\n"
            "You should remove all references to LhADir.dopus5. Copying "
            "the default toolbar over your old toolbar will usually "
            "accomplish this."
   )
)

(if
   (= (exists "DOpus5:ARexx/LhADir.dopus5") 1)
   (if
      (askbool
         (prompt "LhADir.dopus5 detected.\n\n"
                 "LhADir has been superseded by ArcDir and is not needed "
                 "any more. Is it okay to delete it?"
         )
         (help "Select 'Yes' to delete LhADir.dopus5 and related files "
               "from your system or 'No' to leave them alone."
         )
         (default 1)
      )
      (
         (delete "DOpus5:ARexx/LhADir.dopus5")
         (delete "DOpus5:ARexx/LhADir-handler")
         (delete "DOpus5:ARexx/NewLhA.dopus5")
         (delete "DOpus5:C/LhADirList")
         (delete "Locale:Catalogs/nederlands/LhADir.catalog")
         (delete "Locale:Catalogs/deutsch/LhADir.catalog")
         (delete "Locale:Catalogs/français/LhADir.catalog")
         (delete "Locale:Catalogs/norsk/LhADir.catalog")
         (delete "DOpus5:Buttons/LhADir Buttons")
         (delete "DOpus5:Buttons/toolbar_LhADir")
      )
   )
)


;=============================================================================
; ask questions first, copy files later

(set
   #storeold
   (if
      (= @user-level 2)
      (askbool
         (prompt "Your current filetypes for LHA and LZX will be overwritten. "
                 "Would you like to keep a safe copy of them in the "
                 "'DOpus5:Storage' drawer?\n\n"
                 "In case of an uninstallation these files will be reinstalled."
         )
         (help "Select 'Yes' to copy the files 'LHA archive' and 'LZX archive' "
               "from the 'DOpus5:Filetypes' drawer to the 'DOpus5:Storage' "
               "drawer. If you select 'No', the current (default) filetypes for "
               "LHA and LZX in the Storage drawer will be kept.\n\n"
               "When doing an uninstallation, these filetypes will be "
               "reinstalled from the Storage drawer to the Filetypes drawer. "
               "You can accomplish an uninstallation by running this "
               "installation script again."
        )
      )
   )
)

(set
   #how
   (+
      (askchoice
         (prompt "How would you like an archive to be listed when it "
                 "is double-clicked?"
         )
         (help @askchoice-help)
         (choices
            (cat #proprender "In a new lister (Browse).")
            "In the same lister (GetDir)."
         )
      )
      (* 2
         (askchoice
            (prompt "What do you want to happen when you drag and drop "
                    "an archive in another lister?"
            )
            (help @askchoice-help)
            (choices
               (cat #proprender "Extract it (with LhA/LZX).")
               "List it (with ArcDir). [needs Opus 5.5+]"
            )
         )
      )
   )     
)

(set
   #language
   (if
      (= (exists "ENV:Language") 1)
      (getenv "Language")
      ""
   )
)
(set #catalogs
   (askoptions
      (prompt "Which language catalog(s) do you want to install?")
      (help @askoptions-help)
      (choices (cat #proprender "Nederlands") "Deutsch" "Français" "Norsk")
      (default 
         (+
            (= #language "nederlands")
            (* 2 (= #language "deutsch"))
            (* 4 (= #language "français"))
            (* 8 (= #language "norsk"))
         )
      )
   )
)


;=============================================================================
; copy the files

(set @default-dest "DOpus5:")

(copyfiles
   (source "ARexx")
   (dest "DOpus5:ARexx")
   (pattern "#?")
   (files)
)
(copyfiles
   (source "C")
   (dest "DOpus5:C")
   (pattern "#?")
   (files)
)

(set #storedir
   (if
      (= (exists "DOpus5:Storage/Filetypes") 2)
      "DOpus5:Storage/Filetypes"
      "DOpus5:Storage"
   )
)
(set #n 0)
(while
   (set #arctype (select #n "LHA" "LZX" ""))
   (
      (if
         #storeold
         (copyfiles
            (source (cat "DOpus5:Filetypes/" #arctype " archive"))
            (dest #storedir)
         )
      )
      (copyfiles
         (source
            (select
               #how
               (cat "Filetypes/" #arctype " archive")
               (cat "Storage/Filetypes/" #arctype " archive_GetDir")
               (cat "Storage/Filetypes/" #arctype " archive_Browse&DnD")
               (cat "Storage/Filetypes/" #arctype " archive_GetDir&DnD")
            )
         )
         (dest "DOpus5:Filetypes")
         (newname (cat #arctype " archive"))
      )
      (set #n (+ #n 1))
   )
)

(set #n 0)
(while
   (set #thiscat (select #n "nederlands" "deutsch" "français" "norsk" ""))
   (
      (if
         (in #catalogs #n)
         (copyfiles
            (source (cat "Locale/Catalogs/" #thiscat "/ArcDir.catalog"))
            (dest (cat "LOCALE:Catalogs/" #thiscat))
         )
      )
      (set #n (+ #n 1))
   )
)


;=============================================================================
; exit message

(exit "To be able to use ArcDir.dopus5, ARexx needs to be activated "
      "by running the RexxMast program from the System drawer.\n\n"
      "You may uninstall ArcDir by running this installation script again."
)
