;
; $VER: Install-MCC 1.5 (21.5.2012)
;
; $Id$
;
; History:
;
;   1.0   25.05.2010: initial release
;   1.1   26.05.2010: easier definition of supported languages
;   1.2   26.05.2010: added support for multiple catalog files per language
;                     and added british english as supported language
;   1.3   27.05.2010: the supported languages are now collected dynamically
;                     instead of being defined statically. Furthermore the
;                     .mcc name must be specified in the script's icon as
;                     application name. This makes it possible to use the
;                     script for arbitrary classes without modification.
;   1.4   11.06.2010: extended the final message that a reboot might be
;                     necessary to be able to use the just installed version.
;   1.5   21.05.2012: fixed the wrong selection of the builtin language.
;   1.6   20.12.2013: changed MorphOS desription to include 3.x
;   1.7   03.02.2017: added support for slovak language

;----------------------------------------------------------------------------
; /// language bit definition, do not change these!!
(set #lang_english             0)
(set #lang_german              1)
(set #lang_french              2)
(set #lang_spanish             3)
(set #lang_danish              4)
(set #lang_italian             5)
(set #lang_dutch               6)
(set #lang_norwegian           7)
(set #lang_swedish             8)
(set #lang_polish              9)
(set #lang_finnish             10)
(set #lang_hungarian           11)
(set #lang_greek               12)
(set #lang_turkish             13)
(set #lang_russian             14)
(set #lang_czech               15)
(set #lang_slovak              16)
(set #lang_english_british     17)
(set #lang_maximum             18)
; ///

;----------------------------------------------------------------------------
; /// default language detection
(set #i 0)
(while (<> @language (select #i "deutsch" @language))
    (set #i (+ #i 1))
)
(if (= #i 1) ; LANGUAGE
    (set #language "english")
    (set #language @language)
)

(set #i 0)
(set #defaultLanguages %10000000000000000)
(while (<> #language (select #i "czech" "russian" "türkçe" "greek" "magyar" "suomi" "polski" "svenska" "norsk" "nederlands" "italiano" "dansk" "español" "français" "deutsch" #language))
    (
        (set #i (+ #i 1))
        (set #defaultLanguages (shiftright #defaultLanguages 1))
    )
)
; always add english as default language
(set #defaultLanguages (bitor #defaultLanguages (shiftleft 1 #lang_english)))

; scan the Locale/Catalogs directory for supported languages
(set #supportedLanguages %00000000000000000)
(foreach "Locale/Catalogs" "#?"
    (set #i 0)
    (while (<> @each-name (select #i "english" "german" "french" "spanish" "danish" "italian" "dutch" "norwegian" "swedish" "polish" "finnish" "hungarian" "greek" "turkish" "russian" "czech" "slovak" @each-name))
        (set #i (+ #i 1))
    )
    (if (<> #lang_maximum #i)
        (set #supportedLanguages (bitor #supportedLanguages (shiftleft 1 #i)))
    )
)
; always add english as default supported language
(set #supportedLanguages (bitor #supportedLanguages (shiftleft 1 #lang_english)))

;----------------------------------------------------------------------------
; /// ***** English
(if (= #language "english")
    (
        (set #acknowledgeOS (cat "Please acknowledge the automatic\n"
                                 "detection of your operating system:"))
        (set #lang_catalog  (cat "Which catalog file do you want to install?\n"
                                 "(English language is built-in)"))
        (set #done          (cat "\n"
                                 @app-name " has been successfully installed.\n"
                                 "\n"
                                 "A reboot might be necessary to\n"
                                 "finish this installation!"))
    )
)

; ///
; /// ***** Deutsch
(if (= #language "deutsch")
    (
        (set #acknowledgeOS (cat "Bitte bestätigen Sie die automatische\n"
                                 "Ermittlung Ihres Betriebssystems:"))
        (set #lang_catalog  (cat "Welche catalog-Datei wollen Sie installieren?\n"
                                 "(Englisch ist eingebaut)"))
        (set #done          (cat "\n"
                                 @app-name " wurde erfolgreich installiert.\n"
                                 "\n"
                                 "Gegebenenfalls ist ein Neustart des Systems\n"
                                 "nötig, um die Installation abzuschließen!"))
    )
)
; ///

;----------------------------------------------------------------------------
; /// Checking environment
(set EXEC_ver (getversion "exec.library" (resident)))
(set NEWLIB_ver (getversion "newlib.library" (resident)))
(set AROS_ver (getversion "aros.library" (resident)))

; we use newlib.library to check whether we are running on AmigaOS 4.x
(if (>= NEWLIB_ver (* 4 65536))
    (set #detectedSystem 1)
    (
        ; we are not running on AmigaOS 4.x
        (if (>= AROS_ver (* 40 65536))
            (set #detectedSystem 3)
            (
                ; we are not running on AROS
                ; now check if we are running on MorphOS (exec.library V50+) or on AmigaOS 3.x
                (if (>= EXEC_ver (* 50 65536))
                    (set #detectedSystem 2)
                    (set #detectedSystem 0)
                )
            )
        )
    )
)

(set #detectedSystem
    (askchoice
        (prompt #acknowledgeOS)
        (help @askchoice-help)
        (choices "AmigaOS 3.x"
                 "AmigaOS 4.x"
                 "MorphOS 1.x/2.x/3.x"
                 "AROS i386"
                 "AROS PPC"
                 "AROS x86_64"
        )
        (default #detectedSystem)
    )
)
; finally convert the system number into a string for easier understanding
(set #detectedSystem
    (select #detectedSystem "os3" "os4" "mos" "aros-i386" "aros-ppc" "aros-x86_64")
)

(if (= #detectedSystem "os4")
    ; AmigaOS 4.x uses english locale names only
    (
        (set #cat_name_english               "english")
        (set #cat_name_german                "german")
        (set #cat_name_french                "french")
        (set #cat_name_spanish               "spanish")
        (set #cat_name_danish                "danish")
        (set #cat_name_italian               "italian")
        (set #cat_name_dutch                 "dutch")
        (set #cat_name_norwegian             "norwegian")
        (set #cat_name_swedish               "swedish")
        (set #cat_name_polish                "polish")
        (set #cat_name_finnish               "finnish")
        (set #cat_name_hungarian             "hungarian")
        (set #cat_name_greek                 "greek")
        (set #cat_name_turkish               "turkish")
        (set #cat_name_russian               "russian")
        (set #cat_name_czech                 "czech")
        (set #cat_name_slovak                "slovak")
        (set #cat_name_english_british       "english-british")
    )
    ; all other systems use localized locale names
    (
        (set #cat_name_english               "english")
        (set #cat_name_german                "deutsch")
        (set #cat_name_french                "français")
        (set #cat_name_spanish               "español")
        (set #cat_name_danish                "dansk")
        (set #cat_name_italian               "italiano")
        (set #cat_name_dutch                 "nederlands")
        (set #cat_name_norwegian             "norsk")
        (set #cat_name_swedish               "svenska")
        (set #cat_name_polish                "polski")
        (set #cat_name_finnish               "suomio")
        (set #cat_name_hungarian             "magyar")
        (set #cat_name_greek                 "greek")
        (set #cat_name_turkish               "türkçe")
        (set #cat_name_russian               "russian")
        (set #cat_name_czech                 "czech")
        (set #cat_name_slovak                "slovak")
        (set #cat_name_english_british       "english-british")
    )
)

; set up the source and destination path
(if (= #detectedSystem "os3")
    (
        (set #MCC_source "Libs/MUI/AmigaOS3")
        (set #MCC_dest "MUI:libs/mui")
    )
)
(if (= #detectedSystem "os4")
    (
        (set #MCC_source "Libs/MUI/AmigaOS4")
        (set #MCC_dest "MUI:libs/mui")
    )
)
(if (= #detectedSystem "mos")
    (
        (set #MCC_source "Libs/MUI/MorphOS")
        (set #MCC_dest "SYS:Classes/MUI")
    )
)
(if (= #detectedSystem "aros-i386")
    (
        (set #MCC_source "Libs/MUI/AROS-i386")
        (set #MCC_dest "SYS:Classes/Zune")
    )
)
(if (= #detectedSystem "aros-ppc")
    (
        (set #MCC_source "Libs/MUI/AROS-ppc")
        (set #MCC_dest "SYS:Classes/Zune")
    )
)
(if (= #detectedSystem "aros-x86_64")
    (
        (set #MCC_source "Libs/MUI/AROS-x86_64")
        (set #MCC_dest "SYS:Classes/Zune")
    )
)

; ///
; /// Let's ask user for anything
(complete 10)

(set catalog_lang
    (askoptions
        (prompt #lang_catalog)
        (help @askoptions-help)
        (choices
            (if (in #supportedLanguages #lang_english)
                (cat #prop "English")
                ("")
            )
            (if (in #supportedLanguages #lang_german)
                (cat #prop "Deutsch")
                ("")
            )
            (if (in #supportedLanguages #lang_french)
                (cat #prop "Français")
                ("")
            )
            (if (in #supportedLanguages #lang_spanish)
                (cat #prop "Español")
                ("")
            )
            (if (in #supportedLanguages #lang_danish)
                (cat #prop "Dansk")
                ("")
            )
            (if (in #supportedLanguages #lang_italian)
                (cat #prop "Italiano")
                ("")
            )
            (if (in #supportedLanguages #lang_dutch)
                (cat #prop "Nederlands")
                ("")
            )
            (if (in #supportedLanguages #lang_norwegian)
                (cat #prop "Norsk")
                ("")
            )
            (if (in #supportedLanguages #lang_swedish)
                (cat #prop "Svenska")
                ("")
            )
            (if (in #supportedLanguages #lang_polish)
                (cat #prop "Polski")
                ("")
            )
            (if (in #supportedLanguages #lang_finnish)
                (cat #prop "Suomi")
                ("")
            )
            (if (in #supportedLanguages #lang_hungarian)
                (cat #prop "Magyar")
                ("")
            )
            (if (in #supportedLanguages #lang_greek)
                (cat #prop "ĊëëçíéêÜ")
                ("")
            )
            (if (in #supportedLanguages #lang_turkish)
                (cat #prop "Türkçe")
                ("")
            )
            (if (in #supportedLanguages #lang_russian)
                (cat #prop "Russian")
                ("")
            )
            (if (in #supportedLanguages #lang_czech)
                (cat #prop "Czech")
                ("")
            )
            (if (in #supportedLanguages #lang_slovak)
                (cat #prop "Slovak")
                ("")
            )
            (if (and (in #supportedLanguages #lang_english_british) (= #detectedSystem "os4"))
                (cat #prop "English (british)")
                ("")
            )
        )
        (default #defaultLanguages)
    )
)

; ///
; /// Here we go!
(complete 33)

; copy the .mcc and .mcp files
(foreach #MCC_source "#?.mc?"
    (copylib
        (prompt @each-name)
        (source (tackon #MCC_source @each-name))
        (dest #MCC_dest)
        (confirm)
        (help @copylib-help)
    )
)

(complete 66)

; check if there were any catalogs selected at all
(if (<> 0 catalog_lang)
    (
        ; copy the .cd files for people wanting to translate
        (foreach "Locale" "#?_mcp.cd"
            (copyfiles
                (source (tackon "Locale" @each-name))
                (dest "LOCALE:Catalogs")
                (help @copyfiles-help)
            )
        )

        ; german catalog file
        (if (in catalog_lang #lang_german)
            (
                (makedir (tackon "LOCALE:Catalogs" #cat_name_german))

                (foreach "Locale/Catalogs/german" "#?_mcp.catalog"
                    (copyfiles
                        (source (tackon "Locale/Catalogs/german" @each-name))
                        (dest (tackon "LOCALE:Catalogs" #cat_name_german))
                        (help @copyfiles-help)
                    )
                )
            )
        )

        ; french catalog file
        (if (in catalog_lang #lang_french)
            (
                (makedir (tackon "LOCALE:Catalogs" #cat_name_french))

                (foreach "Locale/Catalogs/french" "#?_mcp.catalog"
                    (copyfiles
                        (source (tackon "Locale/Catalogs/french" @each-name))
                        (dest (tackon "LOCALE:Catalogs" #cat_name_french))
                        (help @copyfiles-help)
                    )
                )
            )
        )

        ; spanish catalog file
        (if (in catalog_lang #lang_spanish)
            (
                (makedir (tackon "LOCALE:Catalogs" #cat_name_spanish))

                (foreach "Locale/Catalogs/spanish" "#?_mcp.catalog"
                    (copyfiles
                        (source (tackon "Locale/Catalogs/spanish" @each-name))
                        (dest (tackon "LOCALE:Catalogs" #cat_name_spanish))
                        (help @copyfiles-help)
                    )
                )
            )
        )

        ; danish catalog file
        (if (in catalog_lang #lang_danish)
            (
                (makedir (tackon "LOCALE:Catalogs" #cat_name_danish))

                (foreach "Locale/Catalogs/danish" "#?_mcp.catalog"
                    (copyfiles
                        (source (tackon "Locale/Catalogs/danish" @each-name))
                        (dest (tackon "LOCALE:Catalogs" #cat_name_danish))
                        (help @copyfiles-help)
                    )
                )
            )
        )

        ; italian catalog file
        (if (in catalog_lang #lang_italian)
            (
                (makedir (tackon "LOCALE:Catalogs" #cat_name_italian))

                (foreach "Locale/Catalogs/italian" "#?_mcp.catalog"
                    (copyfiles
                        (source (tackon "Locale/Catalogs/italian" @each-name))
                        (dest (tackon "LOCALE:Catalogs" #cat_name_italian))
                        (help @copyfiles-help)
                    )
                )
            )
        )

        ; dutch catalog file
        (if (in catalog_lang #lang_dutch)
            (
                (makedir (tackon "LOCALE:Catalogs" #cat_name_dutch))

                (foreach "Locale/Catalogs/dutch" "#?_mcp.catalog"
                    (copyfiles
                        (source (tackon "Locale/Catalogs/dutch" @each-name))
                        (dest (tackon "LOCALE:Catalogs" #cat_name_dutch))
                        (help @copyfiles-help)
                    )
                )
            )
        )

        ; norwegian catalog file
        (if (in catalog_lang #lang_norwegian)
            (
                (makedir (tackon "LOCALE:Catalogs" #cat_name_norwegian))

                (foreach "Locale/Catalogs/norwegian" "#?_mcp.catalog"
                    (copyfiles
                        (source (tackon "Locale/Catalogs/norwegian" @each-name))
                        (dest (tackon "LOCALE:Catalogs" #cat_name_norwegian))
                        (help @copyfiles-help)
                    )
                )
            )
        )

        ; swedish catalog file
        (if (in catalog_lang #lang_swedish)
            (
                (makedir (tackon "LOCALE:Catalogs" #cat_name_swedish))

                (foreach "Locale/Catalogs/swedish" "#?_mcp.catalog"
                    (copyfiles
                        (source (tackon "Locale/Catalogs/swedish" @each-name))
                        (dest (tackon "LOCALE:Catalogs" #cat_name_swedish))
                        (help @copyfiles-help)
                    )
                )
            )
        )

        ; polish catalog file
        (if (in catalog_lang #lang_polish)
            (
                (makedir (tackon "LOCALE:Catalogs" #cat_name_polish))

                (foreach "Locale/Catalogs/polish" "#?_mcp.catalog"
                    (copyfiles
                        (source (tackon "Locale/Catalogs/polish" @each-name))
                        (dest (tackon "LOCALE:Catalogs" #cat_name_polish))
                        (help @copyfiles-help)
                    )
                )
            )
        )

        ; finnish catalog file
        (if (in catalog_lang #lang_finnish)
            (
                (makedir (tackon "LOCALE:Catalogs" #cat_name_finnish))

                (foreach "Locale/Catalogs/finnish" "#?_mcp.catalog"
                    (copyfiles
                        (source (tackon "Locale/Catalogs/finnish" @each-name))
                        (dest (tackon "LOCALE:Catalogs" #cat_name_finnish))
                        (help @copyfiles-help)
                    )
                )
            )
        )

        ; hungarian catalog file
        (if (in catalog_lang #lang_hungarian)
            (
                (makedir (tackon "LOCALE:Catalogs" #cat_name_hungarian))

                (foreach "Locale/Catalogs/hungarian" "#?_mcp.catalog"
                    (copyfiles
                        (source (tackon "Locale/Catalogs/hungarian" @each-name))
                        (dest (tackon "LOCALE:Catalogs" #cat_name_hungarian))
                        (help @copyfiles-help)
                    )
                )
            )
        )

        ; greek catalog file
        (if (in catalog_lang #lang_greek)
            (
                (makedir (tackon "LOCALE:Catalogs" #cat_name_greek))

                (foreach "Locale/Catalogs/greek" "#?_mcp.catalog"
                    (copyfiles
                        (source (tackon "Locale/Catalogs/greek" @each-name))
                        (dest (tackon "LOCALE:Catalogs" #cat_name_greek))
                        (help @copyfiles-help)
                    )
                )
            )
        )

        ; turkish catalog file
        (if (in catalog_lang #lang_turkish)
            (
                (makedir (tackon "LOCALE:Catalogs" #cat_name_turkish))

                (foreach "Locale/Catalogs/turkish" "#?_mcp.catalog"
                    (copyfiles
                        (source (tackon "Locale/Catalogs/turkish" @each-name))
                        (dest (tackon "LOCALE:Catalogs" #cat_name_turkish))
                        (help @copyfiles-help)
                    )
                )
            )
        )

        ; russian catalog file
        (if (in catalog_lang #lang_russian)
            (
                (makedir (tackon "LOCALE:Catalogs" #cat_name_russian))

                (foreach "Locale/Catalogs/russian" "#?_mcp.catalog"
                    (copyfiles
                        (source (tackon "Locale/Catalogs/russian" @each-name))
                        (dest (tackon "LOCALE:Catalogs" #cat_name_russian))
                        (help @copyfiles-help)
                    )
                )
            )
        )

        ; czech catalog file
        (if (in catalog_lang #lang_czech)
            (
                (makedir (tackon "LOCALE:Catalogs" #cat_name_czech))

                (foreach "Locale/Catalogs/czech" "#?_mcp.catalog"
                    (copyfiles
                        (source (tackon "Locale/Catalogs/czech" @each-name))
                        (dest (tackon "LOCALE:Catalogs" #cat_name_czech))
                        (help @copyfiles-help)
                    )
                )
            )
        )

        ; slovak catalog file
        (if (in catalog_lang #lang_slovak)
            (
                (makedir (tackon "LOCALE:Catalogs" #cat_name_slovak))

                (foreach "Locale/Catalogs/slovak" "#?_mcp.catalog"
                    (copyfiles
                        (source (tackon "Locale/Catalogs/slovak" @each-name))
                        (dest (tackon "LOCALE:Catalogs" #cat_name_slovak))
                        (help @copyfiles-help)
                    )
                )
            )
        )

        ; english-british catalog file, only AmigaOS4.x can handle this
        (if (and (in catalog_lang #lang_english_british) (= #detectedSystem "os4"))
            (
                (makedir (tackon "LOCALE:Catalogs" #cat_name_english_british))

                (foreach "Locale/Catalogs/english-british" "#?_mcp.catalog"
                    (copyfiles
                        (source (tackon "Locale/Catalogs/english-british" @each-name))
                        (dest (tackon "LOCALE:Catalogs" #cat_name_english_british))
                        (help @copyfiles-help)
                    )
                )
            )
        )
     )
)

(complete 100)

(message #done)

; /// ********************
;----------------------------------------------------------------------------
(exit (QUIET))
