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

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

(set #pathprompt "Select a destination directory for the i2c.library binary:")
(set #pathhelp (cat "\nYou might want to change the given default "
        "directory (which is essentially LIBS:), if you have a strict policy "
        "of keeping system libraries and non-system libraries in separate "
        "places.") )
(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.\n\n") )
(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 indeed just 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 AA Amigas (i.e. A1200/A4000), "
        "1 for all the older ECS/OCS models.\n\n"
        "Hint: You don't have to run this script every time you want to "
        "try a new delay value. To test a value of 5, for example, you "
        "would execute the following from a shell prompt: \"I2Cscan -d5\", "
        "and if the result is okay: \"SetEnv I2CDELAY 5\" and \"Copy "
        "ENV:I2CDELAY ENVARC:\"." ) )
(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 would make sure that the current i2c.library is "
        "removed from memory, and might save you a lot of confusion.") )
(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 was poorly chosen and has been "
        "reset to the default value of " #safedelay ".") )

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

(set #hwtype 0)         ; c't-Hardware
(set #busdelay (getenv "I2CDELAY"))
(if (= "" #busdelay)
    (set #busdelay #safedelay)
)

;--------------------------- Interview, part 1 --------------------------

(if (= @user-level 2)       ; this question for experts only
    (set @default-dest
        (askdir
            (prompt #pathprompt)
            (help #pathhelp)
            (default @default-dest)
        )
    )
)

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

;-------------------------- Installation, part 1 ------------------------

; 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:")

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

;--------------------------- Interview, part 2 --------------------------

(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
(textfile
    (dest "ENV:I2CDELAY")
    (append #busdelay)
)
(textfile
    (dest "ENVARC:I2CDELAY")
    (append #busdelay)
)

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

(exit #farewell)
