
(set @default-dest (expandpath "LIBS:"))
(set #safedelay 10)

;--------------------------- Englische Texte ----------------------------

(set #workmodeprompt (cat "This is a multi-purpose script. "
        "What do you want to do?") )
(set #workmode0 "Install i2c.library")
(set #workmode1 "Configure")
(set #workmode2 "Uninstall")
(set #workmodehelp ( cat "\n\"Install\" copies a library to LIBS: and creates "
        "an environment variable. Both can be removed again using the "
        "\"Uninstall\" option.\n\n"
        "\"Configure\" lets you adjust the environment variable which you "
        "created during installation.") )
(set #hwchoiceprompt "Pick your type of I²C interface:")
(set #hwchoice0 "SERIAL PORT, as proposed by \"c't\" magazine")
(set #hwchoice1 "serial port, Wiegand VTX 2000 hardware")
(set #hwchoice2 "serial port, HSL VideoTXT hardware")
(set #hwchoice3 "serial port, KDK Satcom PC-Videotext")
(set #hwchoice4 "PARALLEL PORT, as proposed by Jan Leuverink")
(set #hwchoice5 "parallel port, Printtechnik teletext")
(set #hwchoice6 "parallel port, Microtext hardware")
(set #hwchoice7 "autoconfig board, M&T TeleTxt")
(set #hwchoice8 "FLOPPY PORT")
(set #hwchoicehelp (cat "\nDepending on the kind of interface hardware you "
        "own, you can have one of several customized i2c.library versions "
        "installed. Those interfaces which are described in the accompanying "
        "schematics are CAPITALIZED.") )
(set #delayprompt "Enter a timing parameter for i2c.library:" )
(set #delayhelp (cat "\nSetting too small a delay parameter will keep "
        "i2c.library from working properly.\n\n"
        "While a value of " #safedelay " should be safe for all systems (and "
        "has apparently been confirmed to work for you, too), you might "
        "want to try smaller values, to optimize throughput: "
        "5 has often been reported to work for A1200, 1 for all other Amigas "
        "(including A4000)." ) )
(set #okmsg (cat "Communication with your I²C peripheral(s) was successfully "
        "established. Congratulations.") )
(set #advice (cat "\n\nHint: If you think that you chose the wrong type of "
        "library, you might want to reset your Amiga before running this "
        "script again. That's (in some cases) the only way to make sure that "
        "the current i2c.library is removed from memory.") )
(set #diagmsg5 (cat "I2CScan could not detect any listeners on your I²C bus. "
        "This might of course be just because there aren't any?" #advice ) )
(set #diagmsg10 (cat "I2CScan couldn't test your interface, because "
        "i2c.library either failed to detect it, or failed to access it "
        "(possibly due to resource conflicts)." #advice ) )
(set #diagmsg15 (cat "I²C protocol errors occured while running I2CScan. "
        "This might be because your hardware is not appropriate for the "
        "type of i2c.library that you installed." #advice ) )
(set #diagmsg20 "I2CScan failed to open i2c.library. This is weird." )
(set #timingmsg (cat "Your I2CDELAY parameter does not seem to work for "
        "i2c.library and has been reset to the default value of "
        #safedelay ".") )
(set #uifarewell (cat "Deinstallation complete!\n"
        "i2c.library and the I2CDELAY environment variable have been "
        "removed from your system.") )

;---------------------------- Default values ----------------------------

(set #hwtype 0)         ; c't hardware
(set #workmode 0)       ; normal installation
(set #result 0)         ; will be overwritten once we run I2Cscan
(set #farewell #okmsg)  ; this too, in case of an error
(if (= 0 (exists "ENV:I2CDELAY"))
  (run (cat "SetEnv I2CDELAY " #safedelay) )
)
(set #busdelay (getenv "I2CDELAY"))

;-------------------- Experts may choose what to do. --------------------

(if (= @user-level 2)
    (set #workmode
        (askchoice
            (prompt #workmodeprompt)
            (help #workmodehelp)
            (choices #workmode0 #workmode1 #workmode2)
            (default #workmode)
        )
    )
)

;------------------------- Are we uninstalling? -------------------------

(if (= #workmode 2)
  (
    (delete "ENVARC:I2CDELAY")
    (delete (tackon @default-dest "i2c.library") )
    (run "Avail FLUSH >NIL:")
    (message #uifarewell)
    (exit (quiet))
  )
)

;------ For normal installation, pick, install and test a library. ------

(if (= #workmode 0) (

(set #hwtype
    (askchoice
        (prompt #hwchoiceprompt)
        (help #hwchoicehelp)
        (choices #hwchoice0 #hwchoice1 #hwchoice2 #hwchoice3 #hwchoice4
                 #hwchoice5 #hwchoice6 #hwchoice7 #hwchoice8 )
        (default #hwtype)
    )
)

; Copy the library binary.
(set #sourcefile
    (select #hwtype
      "libs/i2c.library.ser"
      "libs/i2c.library.ser2"
      "libs/i2c.library.ser3"
      "libs/i2c.library.ser4"
      "libs/i2c.library.par"
      "libs/i2c.library.par2"
      "libs/i2c.library.par3"
      "libs/i2c.library.card"
      "libs/i2c.library.disk"
    )
)
(copyfiles
    (source #sourcefile)
    (dest @default-dest)
    (newname "i2c.library")
)

; Try to purge a currently loaded image.
(run "Avail FLUSH >NIL:")

; Before asking for an optimized delay value, does this library work at all?
(set #result
    (run (cat "bin/I2CScan -d" #safedelay " >NIL:") )
)
(if (= #result 20)
    (set #farewell #diagmsg20)
)
(if (= #result 15)
    (set #farewell #diagmsg15)
)
(if (= #result 10)
    (set #farewell #diagmsg10)
)
(if (= #result 5)
    (set #farewell #diagmsg5)
)

) ) ; #workmode 1

;--------------------------- Interview, part 3 --------------------------

(if (= #result 0)           ; Alright, it works with the safe value. Optimize?
(

(if (= @user-level 2)       ; this question for experts only
    (set #busdelay
        (asknumber
            (prompt #delayprompt)
            (help #delayhelp)
            (range 0 100)
            (default (+ #busdelay))
        )
    )
)

; Test the current setting of #busdelay
(set #result
    (run (cat "bin/I2CScan -d" #busdelay " >NIL:") )
)
(if (<> #result 0)          ; poorly chosen delay parameter
    (
    (set #farewell #timingmsg)
    (set #busdelay #safedelay)
    )
)

)
)   ; #result 0

;-------------------------- Installation, part 2 ------------------------

; create the I2CDELAY environment variable
(run (cat "SetEnv I2CDELAY " #busdelay) )
(copyfiles
    (source "ENV:I2CDELAY")
    (dest "ENVARC:")
)

;------------------------------- Farewell -------------------------------

(message #farewell)

(if (= #workmode 1)
  (exit (quiet))
)
