(delopts "oknodelete" "force" "askuser")

(onerror

    (exit (quiet))
)

; ---------------------------------- constants ---------------------------------

(set @basic_parts_manual    1)
(set @basic_parts_examples  2)
(set @basic_parts_developer 4)

; ---------------------------------- variables  --------------------------------

(set basic_found            0)
(set basic_components       7)
(set basic_installmode      1)
(set basic_path            "")
(set basic_installdir      "")

(if (exists "golded:" (noreq))

    (set basic_editor "golded:ed -sticky %s")

    (set basic_editor "ed")
)

; ----------------------------------- strings ----------------------------------

(if (= @installer-version 0)

    (set #basic_esc "")

    (set #basic_esc "[2p")
)

(if (= @language "deutsch")

    (
        (set #basic_welcome           "Willkommen bei Distributed BASIC !\n\nMit diesem Programm wird Distributed BASIC auf Ihrem Computer installiert.\n\nWARNUNG: Diese Software ist durch das nationale und internationale Urheberrecht geschützt. Unerlaubter Vertrieb oder Verleih der Software oder einer ihrer Komponenten wird verfolgt.")

        (set #basic_welcome_install   "INSTALLIEREN")

        (set #basic_welcome_uninstall "Entfernen")

        (set #basic_components        "Wählen Sie die optionalen Komponenten, die installiert werden sollen:")

        (set #basic_manual            "Dokumentation")

        (set #basic_examples          "Beispiele")

        (set #basic_developer         "Entwicklermaterial")

        (set #basic_found             "Das Programm wurde schon einmal installiert:\n\nWählen Sie AKTUALISIEREN, um Komponenten hinzuzufügen oder zu entfernen. Wählen Sie ANDERES VERZEICHNIS, um die Installation an anderer Stelle zu wiederholen.")

        (set #basic_found_help        "AKTUALISIEREN\n\nÄndert die vorhandene Installation. Dort vorhandne Komponenten werden im weiteren Verlauf dieser Installation aktualisiert.\n\nANDERES VERZEICHNIS\n\nDie Installation erfolgt in ein anderes Verzeichnis. Eine vorhandene Installation an anderer Stelle bleibt erhalten. Sie sollten von dieser Möglichkeit nur Gebrauch machen, wenn mit der vorhandenen Installation Probleme auftauchen, die eine Aktualisierung nicht ratsam erscheinen lassen.")

        (set #basic_found_confirm     "AKTUALISIEREN")

        (set #basic_found_newinstall  "Anderes Verzeichnis")

        (set #basic_path_choose       "Wählen Sie einen Pfad für die Installation. In dem gewählten Pfad wird ein Verzeichnis für Distributed BASIC erzeugt:")

        (set #basic_overwrite         "An dieser Stelle existiert schon ein Verzeichnis mit dem Namen BASIC ! Soll mit der Installation in dieses Verzeichnis fortgefahren werden ?")

        (set #basic_overwrite_help    "Möglicherweise gehört dieses Verzeichnis zu einem anderen Programm oder einer älteren unvollständigen Installation.")

        (set #basic_overwrite_install "Installieren")

        (set #basic_overwrite_cancel  "Anderes Verzeichnis wählen")

        (set #basic_flavour           "Wählen Sie die mathematische Genauigkeit des Interpreters:")

        (set #basic_flavour_help      "Der vorgesehene Anwendungsbereich von Distributed BASIC erfordert keine Fliesskommazahlen, wir empfehlen deshalb die Installation der schnelleren Version mit Ganzzahlarithmetik.")

        (set #basic_flavour_integer   "Ganzzahlarithmetik")

        (set #basic_flavour_float     "Fliesskommazahlen")

        (set #basic_uninstalling      "Distributed BASIC wird entfernt...")

        (set #basic_uninstallerror    "Deinstallation abgeschlossen. Es konnten nicht alle Komponenten entfernt werden. Beenden Sie alle Programme, starten Sie den Rechner neu und löschen Sie die verbleibenden Dateien.")

        (set #basic_nostartup         "Die Datei s:user-startup fehlt oder ist beschädigt !")

        (set #basic_startup           "Sollen die notwendigen Einträge in der Startup-Datei des Rechners jetzt erzeugt werden ?")

        (set #basic_editor            "Wählen Sie den Editor, der vom BASIC-Interpreter aufgerufen wird, wenn der EDIT-Befehl benutzt wird:")

        (set #basic_editor_help       "Prinzipiell kann jeder beliebige Editor verwendent werden. Der Platzhalter %s steht für den Dateinamen.")

        (set #basic_nocdrom           "Der Vertrieb dieser Software auf CD-ROM ist untersagt.")
    )

    ; english strings

    (
        (set #basic_welcome           "Welcome to the Distributed BASIC setup !\n\nThis program installs Distributed BASIC on your computer.\n\nWARNING: This software is protected by international and national laws. Unauthorized distribution or rental of this software or one of its components are prosecuted to the full extent possible.")

        (set #basic_welcome_install   "INSTALL")

        (set #basic_welcome_uninstall "Uninstall")

        (set #basic_components        "Select optional components to install:")

        (set #basic_manual            "Documentation")

        (set #basic_examples          "Examples")

        (set #basic_developer         "Developer material")

        (set #basic_found             "This software has been installed before:\n\nChoose UPDATE to update, add or remove components of the existing installation. Choose OTHER DIRECTORY to install again to another directory.")

        (set #basic_found_help        "UPDATE\n\nUpdates the existing installation: update, add or remove its components.\n\nOTHER DIRECTORY\n\nRepeat installation to another directory. Your old installation remains unchanged. You shouldn't use this installation type unless you have problems with your existing installation.")

        (set #basic_found_confirm     "UPDATE")

        (set #basic_found_newinstall  "Other directory")

        (set #basic_path_choose       "Choose a path for installation. The setup program will create a directory for Distributed BASIC in the path you specify:")

        (set #basic_overwrite         "A BASIC drawer already exists in this path ! Continue with installation into this drawer ?")

        (set #basic_overwrite_help    "This drawer could belong to another program or to a previous incomplete installation.")

        (set #basic_overwrite_install "Install")

        (set #basic_overwrite_cancel  "Choose other path")

        (set #basic_flavour           "Choose the mathematical precision of the interpreter:")

        (set #basic_flavour_help      "We recommend to install the faster integer version because the intended usage of Distributed BASIC as scripting language does not require floating point math.")

        (set #basic_flavour_integer   "Integer math")

        (set #basic_flavour_float     "Floating point math")

        (set #basic_uninstalling      "Removing Distributed BASIC ...")

        (set #basic_uninstallerror    "Uninstallation completed. Some files could not be removed. Terminate all programs, reboot your computer and delete the remaining files manually.")

        (set #basic_nostartup         "The file s:user-startup is missing or corrupt !")

        (set #basic_startup           "Should the required changes to s:user-startup be made now ?")

        (set #basic_editor            "Choose the editor that gets started by the BASIC interpreter if the EDIT command is used:")

        (set #basic_editor_help       "You can use any editor. %s is a placeholder for the file name.")

        (set #basic_nocdrom           "Distribution of this software on CD-ROM is not permitted.")
    )
)

(set app-name "Distributed BASIC")

; ------------------------------- media check ----------------------------------

(run "echo >delete.me \"This file can be deleted.\"")

(if (exists "delete.me")

    (delete "delete.me")

    ; else

    (abort #basic_nocdrom)
)

; ----------------------------- choose installmode -----------------------------

(if (exists "basic:" (noreq))

    (set basic_installmode

        (askbool

            (prompt #basic_welcome)

            (choices

                #basic_welcome_install

                #basic_welcome_uninstall
            )

            (default 0)

            (help @askbool-help)
        )
    )

    ; else

    (message #basic_welcome)
)

; uninstall

(if (= basic_installmode 0)

    (
        (working #basic_uninstalling)

        (set basic_path (expandpath "basic:"))

        ; clean up startup sequence

        (run "installdata/stripstartup >NIL: LINES=6 SECTIONNAME=BASIC")

        ; get rid of active assigns pointing to BASIC directory

        (run "assign libs: basic:libs remove")

        (run "assign c: basic:c remove")

        (run "path remove basic:c")

        (run "assign basic: remove")

        ; delete root directory and its icon

        (run ("installdata/remdir >NIL: BASEDIR=\"%s\"" basic_path))

        (if (exists basic_path (noreq))

            (message #basic_uninstallerror)
        )

        (exit (quiet))
    )
)

; install

(if (= basic_installmode 1)

    (
        ; some users may have moved the library to "sys:libs" (where it is not supposed to be)

        (if (exists "sys:libs/BASIC.library" (noreq))

            (delete "sys:libs/BASIC.library")
        )

        ; find old installation

        (if (exists "basic:" (noreq))

            (
                (set basic_installdir (pathonly (expandpath "basic:")))

                (set basic_found

                    (askbool

                        (prompt #basic_found)

                        (choices

                            #basic_found_confirm

                            #basic_found_newinstall
                        )

                        (default 0)

                        (help #basic_found_help)
                    )
                )
            )

            ; else

            (
                (if (exists "work:" (noreq))

                    (set basic_installdir "work:")

                    (set basic_installdir "sys:")
                )
            )
        )

        ; create installation directory and temporary assign

        (while (= basic_path "")

            (if (= basic_found 1)

                (set basic_path (expandpath "basic:"))

                ; else

                (
                    ; keep requesting path until valid choice is made

                    (set basic_repeat 1)

                    (while (= basic_repeat 1)

                        (set basic_installdir

                            (askdir

                                (prompt #basic_path_choose)

                                (default basic_installdir)

                                (help @askdir-help)
                            )
                        )

                        (if (exists basic_installdir)

                            (set basic_repeat 0)

                            (set basic_repeat 1)
                        )
                    )

                    (set basic_path (tackon basic_installdir "BASIC"))

                    (if (exists basic_path)

                        (
                            (set basic_overwrite

                                (askbool

                                    (prompt #basic_overwrite)

                                    (choices

                                        #basic_overwrite_install

                                        #basic_overwrite_cancel
                                    )

                                    (help #basic_overwrite_help)
                                )
                            )

                            (if (= basic_overwrite 0)

                                (set basic_path "")
                            )
                        )
                    )
                )
            )

            (if (<> basic_path "")

                (
                    (set @default-dest basic_path)

                    ; check existing installation

                    (if (exists basic_path (noreq))

                        (
                            (makeassign "basic" basic_path)

                            (set basic_components 0)

                            (if (exists "basic:manual")

                                (set basic_components (+ basic_components @basic_parts_manual))
                            )

                            (if (exists "basic:examples")

                                (set basic_components (+ basic_components @basic_parts_examples))
                            )

                            (if (exists "basic:developer")

                                (set basic_components (+ basic_components @basic_parts_developer))
                            )

                            ; read files via getenv command (ie. read file into env variable)

                            (if (exists "basic:env/editor")

                                (
                                    (run "installdata/setenv NAME=TEMP FROM=basic:env/editor")

                                    (set basic_editor (getenv "TEMP"))
                                )
                            )
                        )

                        ; else

                        (
                            (makedir basic_path)

                            (makeassign "basic" basic_path)
                        )
                    )

                    ; verify assign

                    (if (not (exists "basic:" (noreq)))

                        (
                            (set basic_path "")

                            (set basic_found 0)
                        )
                    )
                )
            )
        )

        ; query options

        (set basic_flavour

            (askchoice

                (prompt #basic_flavour)

                (help #basic_flavour_help)

                (choices

                    (cat #basic_esc #basic_flavour_integer)

                    (cat #basic_esc #basic_flavour_float)
                )
            )
        )

        (set basic_options

            (askoptions

                (prompt #basic_components)

                (help @askoptions-help)

                (default basic_components)

                (choices

                    (cat #basic_esc #basic_manual)

                    (cat #basic_esc #basic_examples)

                    (cat #basic_esc #basic_developer)
                )
            )
        )

        (set basic_editor

            (askstring

                (prompt #basic_editor)

                (help #basic_editor_help)

                (default basic_editor)
            )
        )

        ; create directories

        (makedir "basic:env")

        (makedir "basic:libs")

        (makedir "basic:c")

        ; install drawer icon

        (set basic_drawer (cat basic_path ".info"))

        (if (not (exists basic_drawer))

            (
                (copyfiles

                    (source "installdata/drawer.info")

                    (dest basic_installdir)

                    (newname (fileonly basic_drawer))
                )

                (tooltype

                    (dest basic_drawer)

                    (noposition)
                )
            )
        )

        (makedir "basic:libs")

        ; install license

        (copyfiles

            (source "")

            (choices "License" "License.info")

            (dest "basic:")

            (optional "nofail" "force")
        )

        ; install library

        (run "avail flush")

        (if (= basic_flavour 0)

             (copyfiles

                 (source "libs/integer/BASIC.library")

                 (dest "basic:libs")

                 (optional "nofail" "force")
             )

             ; else

             (copyfiles

                 (source "libs/fp/BASIC.library")

                 (dest "basic:libs")

                 (optional "nofail" "force")
             )
        )

        ; install binaries

        (copyfiles

            (source "developer/examples/bx/sync/bx")

            (dest "basic:c")

            (optional "nofail" "force")
        )

        (copyfiles

            (source "developer/examples/basic/basic")

            (dest "basic:c")

            (optional "nofail" "force")
        )

        ; install preferences

        (if (not (exists "env:" (noreq)))

            (
                (makedir "ram:env")

                (makeassign "env" "ram:")
            )
        )

        ; install manual

        (if (IN basic_options 0)

            (copyfiles

                (source "")

                (choices "Manual" "Manual.info")

                (dest "basic:")

                (optional "nofail" "force")
            )

            ; else

            (run "delete >nil: basic:manual basic:manual.info quiet")
        )

        ; install examples

        (if (IN basic_options 1)

            (copyfiles

                (source "examples")

                (all)

                (dest "basic:examples")

                (optional "nofail" "force")
            )

            ; else

            (run "delete >nil: basic:examples all quiet")
        )

        ; install developer material

        (if (IN basic_options 2)

            (copyfiles

                (source "developer")

                (all)

                (dest "basic:developer")

                (optional "nofail" "force")
            )

            ; else

            (run "delete >nil: basic:developer all quiet")
        )

        ; move icons to the correct position

        (run "installdata/moveicons >NIL: grid dir=basic: dx=75 dy=75 columns=4 files Manual License")

        ; remove write protection for material copied from CD-ROM

        (run "installdata/unprotect basedir=basic:")

        ; update user-startup

        (startup "BASIC"

            (prompt #basic_startup)

            (help @startup-help)

            (command

                (cat

                    ("assign basic: \"%s\"\n" basic_path)

                    ("assign add LIBS: basic:libs\n")

                    ("path add basic:c\n")
                )
            )
        )

        (run "assign add libs: basic:libs")

        (run "assign add c: basic:c")

        ; save prefs

        (
            (delete "basic:env/editor")

            (textfile

                (dest "basic:env/editor")

                (append basic_editor)
            )

            (if (not (exists "basic:env/console"))

                (textfile

                    (dest "basic:env/console")

                    (append "con:////BASIC/AUTO/WAIT/CLOSE")
                )
            )
        )
    )
)

(exit)

(welcome)
