#!/bin/bash

if grep -v -q ibmasmfs /proc/filesystems; then
    modprobe ibmasm || exit 1
fi

echo -en "\nWaiting for ibmasm device to appear (10 seconds timeout)"
TIMEOUT=10
for ((i=0; i<$TIMEOUT; i++)); do
    DEVICE=`evnode -n "ibmasm RSA I remote mouse"`
    if [ ! -z $DEVICE ]; then 
        TIMEOUT="ok"
        break; 
    fi
    echo -n ". "
    sleep 1
done
# Check the timeout.
if [ $TIMEOUT == 10 ]; then
    echo -en "No device detected. Aborted."
    exit 1
fi

echo
DEVICE=`evnode -n "ibmasm RSA I remote mouse"`
XCONFIG="/etc/X11/xorg.conf"
if [ -e $XCONFIG ]; then
    TMPFILE=`mktemp ibmspupX.XXXXXXXXXX` || exit 1
    AWKFILE=`mktemp ibmspupAWK.XXXXXXXXXX` || exit 1
    cat > $AWKFILE <<-ENDAWK
    BEGIN{
            found_ibmasm=0
            started_input=0
            server_ok = 0
    }
    {
            lines=0
            do {
                    if (\$1 == "Section" || lines > 0) {
                            section[lines] = \$0
                            lines++
                            if (\$1 == "Section")
                                    section["name"] = \$2
                            if (\$1 == "EndSection")
                                    break
                            if (\$0 ~ /Identifier[ \t]*"ibmasm_remote"/) {
                                    found_ibmasm = 1
                            } else if (found_ibmasm == 1 && \$0 ~ /Option[\t ]*"Device"/) {
                                    found_ibmasm = 2
                                    sub(/Option[\t ]*"Device"[\t ]*"[^"]*"/,
                                        "Option       \"Device\" \"$DEVICE\"", \$0)
                                    section[lines-1] = \$0
                            }
                            if (\$0 ~ /InputDevice[ \t]*"ibmasm_remote"/)
                                    server_ok = 1
                    } else
                            print \$0
            } while (getline)
            if (lines == 0)
                       exit
    
            if (index(section["name"], "InputDevice") != 0 && found_ibmasm == 0) {
                    started_input = 1
            } else if (index(section["name"], "ServerLayout") != 0 && server_ok == 0) {
                    server_ok = 2
            } else if (started_input == 1 && found_ibmasm == 0) {
                    print "Section \"InputDevice\""
                    print "\tDriver       \"ibmasm\""
                    print "\tIdentifier   \"ibmasm_remote\""
                    print "\tOption       \"Device\" \"$DEVICE\""
                    print "\tOption       \"XScale\" \"1024\""
                    print "\tOption       \"YScale\" \"768\""
                    print "\tOption       \"SendCoreEvents\" \"True\""
                    print "EndSection\n"
                    found_ibmasm = 2
            }
            for (i=0; i<lines; i++) {
                    if (server_ok == 2 && i == lines-1) {
                            printf "\tInputDevice\t\"ibmasm_remote\"\n"
                            server_ok = 3
                    }
                    printf "%s\n", section[i]
            }
            delete section
    }
    ENDAWK
    awk -f $AWKFILE $XCONFIG > $TMPFILE 
    mv $XCONFIG ${XCONFIG}.ibmspupsave
    mv $TMPFILE $XCONFIG
    rm -f $AWKFILE
fi

mount -t ibmasmfs ibmasm /var/lib/ibmasm

/sbin/ibmsphalt > /dev/null 2>&1 &