;======================================================================
;
; Some basic strings

(set #disksalv-install 
     (cat "\nInstall DiskSalv\n"
          "\nYou may install the DiskSalv program and related "
          "files in the directory of your choice.  Proceed?"))

(set #disksalv-install-help
     (cat "\nThis section lets you choose in which drawer or on "
          "which disk the DiskSalv program will be installed."
          "\n\nIf you choose to proceed, you will be asked where you "
          "want the DiskSalv files placed.  If you choose "
          "to skip this part, you will go to the portion of the "
          "installation that processes language installations".))

(set #which-disk
     "In which disk or drawer should DiskSalv be installed?")

(set #any-locale
     (cat "\nInstall DiskSalv Localizations\n"
          "\nYou may install the DiskSalv locale files for various "
          "languages in the LOCALE: directory.  Proceed?"))

(set #any-locale-help
     (cat "\nDiskSalv can be operated in different languages."
          "\n\nIf you choose to proceed, you will be asked to "
          "select the translations you want available on your"
	  "system."))

(set #which-language "\nWhich catalogs should be installed?")

(set #which-language-help
     (cat "\nDiskSalv can be operated in many different "
         "languages. One file will be copied to your "
         "hard drive for each language supported.\n\n"
         "To reduce the amount of space consumed by the "
         "language files, you can select only the "
         "files of specific languages to be copied.\n\n"
         "Check the boxes of the languages you wish "
         "to have available on your system.\n\n"
         @askoptions-help))

(set #docs-install
     (cat "\nInstall DiskSalv Documentation\n"
          "\nA small manual for DiskSalv can be installed in the"
          "\ndirectory of your choice.  Proceed?"))

(set #which-docs "\nWhich manuals should be installed?")

(set #docs-install-help
     (cat "\nThis section lets you choose in which drawer or on "
          "which disk the DiskSalv documentation will be installed."
          "\n\nIf you choose to proceed, you will be asked where you "
          "want the DiskSalv documents placed.  If you choose "
          "to skip this part, installation is complete."))

(set #docs-where
     (cat "\nIn which disk or drawer should DiskSalv documentation "
          "be installed?"))

(set #system-catalog "locale:catalogs"
     #subdir-catalog "locale/catalogs"
     #subdir-docs    "locale/docs"
     #dscatname      "DiskSalv.catalog"
     #dsdocname      "DiskSalv.doc"
     #tmp-dir        "T:DiskSalv")

(set #get-dearchiver
     (cat "\nPlease specify the file name of a de-archiver "
          "program for \"LHA\"-type files."))

;======================================================================
;
; This function asks about the language, and sets the global "lang" to
; the language bit mask returned by the request.
;

(procedure ASKLANGUAGE
   (if (= askmode 0)
       ((set lang (askoptions (prompt #which-language)
                              (help #which-language-help)
                              (choices "dansk"
                                       "deutsch"
                                       "english"
                                       "fran�ais"
                                       "italiano"
                                       "norsk"
                                       "suomi"
                                       "svenska")))
         (if (IN lang 2) (set doclang 1))
         (if (IN lang 7) (set doclang (+ doclang 2))))
     (if (= doclang 0)
         ((set doclang (askoptions (prompt #which-docs)
			           (help #which-language-help)
                                   (choices "english" "svenska")))))))

;======================================================================
;
; This function finds a de-archiver in the user's system, resulting to
; a panic "ask-the-user" if none can be found.
;

(procedure FINDARCPROG
   (set n 0)
   (while (set test (select n "lharc"
                              "lha"
                              "lz"
                              ""))
      (if (not (run (cat "which " test)))
          (set arcprog test))
      (set n (+ n 1)))

   (if (= arcprog "")
       (set arcprog (askfile (prompt #get-dearchiver)
                             (help @askfile-help)
                             (default "C:"))))
)

;======================================================================
;
; This function gets the locale file information.  We mark the occasion
; with bit 0 set for the availability of the subdirectory, bit 1 set 
; for the availability of the archive file. 
;

(procedure ANYLOCALE
   (set localecode 0)
   (if (exists locale-catalogs)
       (set localecode 1))
   (if (exists locale-archive)
       (set localecode (+ localecode 2)))
   localecode)

;======================================================================
;
; This function transfers a locale file from the appropriate archive
; entry to the proper LOCALE:Catalogs subdirectory.
;

(procedure DOCATARCS
   (if (= arcprog "") (FINDARCPROG))
   (if (<> arcprog "")
       ((working "\nDe-archiving selected catalogs\n")
	(makedir #tmp-dir)
        (set arcsfiles (cat #subdir-catalog "/(" arclst ")/#?"))
        (set dearc-com (cat arcprog " >NIL: <NIL: e " locale-archive " "
                            arcsfiles))
        (set script (tackon #tmp-dir "de-archive-script"))
        (textfile (dest script)
                  (append (cat "cd " #tmp-dir "\n" dearc-com "\n")))
        (execute script)
        (copyfiles (source (tackon #tmp-dir #subdir-catalog))
                   (dest   #system-catalog)
                   (pattern "#?"))
        (run (cat "Delete >NIL: <NIL: " #tmp-dir " all")))))

;======================================================================
;
; This function transfers the locale files from the appropriate 
; existing subdirectories (in "sublst")  to the corresponding
; LOCALE:Catalogs subdirectories.
;

(procedure DOCATSUBS
   (copyfiles (source locale-catalogs)
              (dest   #system-catalog)
              (pattern (cat "(" sublst ")"))))

;======================================================================
;
; This function selects the language to be installed.
;

(procedure DOLOCALE
   (if (not (exists #system-catalog))
       (makedir #system-catalog))
   (set n      0
        sublst ""
        arclst "")
   (while (set language (select n "dansk"
                                  "deutsch"
                                  "english"
                                  "fran�ais"
                                  "italiano"
                                  "norsk"
                                  "suomi"
                                  "svenska"
                                  ""))
     (if (AND (IN lang n) (<> n 2))
        (if (exists (tackon locale-catalogs (tackon language #dscatname)))
            (if (= sublst "") (set sublst language) (set sublst (cat sublst "|" language)))
            (if (= arclst "") (set arclst language) (set arclst (cat arclst "|" language)))))
     (set n (+ n 1)))
   (if (<> sublst "") (DOCATSUBS))
   (if (<> arclst "") (DOCATARCS)))

;======================================================================
;
; This function transfers a doc file from the appropriate archive
; entry to the selected documentation subdirectory.
;

(procedure DODOCARCS
   (if (= arcprog "") (FINDARCPROG))
   (if (<> arcprog "")
       ((working "\nDe-archiving selected docs files\n")
	(makedir #tmp-dir)
        (set arcsfiles (cat #subdir-docs "/(" arclst ")/#?"))
        (set dearc-com (cat arcprog " >NIL: <NIL: e " locale-archive " "
                            arcsfiles))
        (set script (tackon #tmp-dir "de-archive-script"))
        (textfile (dest script)
                  (append (cat "cd " #tmp-dir "\n" dearc-com "\n")))
        (execute script)
        (copyfiles (source (tackon #tmp-dir #subdir-docs))
                   (dest   docoutputdir)
                   (pattern "#?"))
        (run (cat "Delete >NIL: <NIL: " #tmp-dir " all")))))

;======================================================================
;
; This function transfers the docs files from the appropriate 
; existing subdirectories (in "sublst")  to the selected
; directory.
;

(procedure DODOCSUBS
   (copyfiles (source locale-docs)
              (dest   docoutputdir)
              (pattern (cat "(" sublst ")"))))

;======================================================================
;
; This function manages document installation.  It should automatically
; install any docs based on the requested localizations.  If none match,
; the user should be given a new choice of languages.
;

(procedure DODOCS
   (set docoutputdir 
        (tackon (askdir (prompt  #docs-where)
		        (help    @askdir-help)
		        (default @default-dest))
                "DiskSalv_Docs"))
   (set n      0
        sublst ""
        arclst "")
   (while (set language (select n "english"
                                  "svenska"
                                  ""))
     (if (IN doclang n)
        ((set testfile (tackon locale-docs (tackon language #dsdocname)))
         (if (exists testfile)
             (if (= sublst "") (set sublst language) (set sublst (cat sublst "|" language "|"
                                                                      language ".info")))
             (if (= arclst "") (set arclst language) (set arclst (cat arclst "|" language "|"
                                                                      language ".info"))))))
     (set n (+ n 1)))
   (if (<> sublst "") (DODOCSUBS))
   (if (<> arclst "") (DODOCARCS)))

;======================================================================
;
; The main program

(set inputdir          (pathonly @icon))
(set locale-archive    (tackon inputdir "DSLocale.lha")
     locale-catalogs   (tackon inputdir #subdir-catalog)
     locale-docs       (tackon inputdir #subdir-docs)
     arcprog           ""
     doclang           0
     @default-dest     "SYS:Tools")

; First, install the DiskSalv program proper

(if (askbool (prompt #disksalv-install)
             (help #disksalv-install-help)
             (choices "Yes" "Skip This Part")
             (default 1))
    ((set outputdir 
          (askdir (prompt  #which-disk)
		  (help    @askdir-help)
		  (default @default-dest)))
     (set @default-dest outputdir)
     (copyfiles (source inputdir)
                (dest outputdir)
                (pattern "DiskSalv")
                (files)
                (infos))
     (tooltype  (dest (tackon outputdir "DiskSalv"))
                (noposition)))
    (set @default-dest ""))

; Next, consider the locales.  Ask the user for a selection
; of catalogs, then call the catalog installer function. 


(if (ANYLOCALE)
    (if (askbool (prompt #any-locale)
                 (help #any-locale-help)
                 (choices "Yes" "Skip This Part")
                 (default 1))
                  
        ((set askmode 0)
         (ASKLANGUAGE)
         (DOLOCALE))))

(if (askbool (prompt #docs-install)
	     (help #docs-install-help)
             (choices "Yes" "Skip This Part")
             (default 1))
    ((set askmode 1)
     (ASKLANGUAGE)
     (DODOCS)))