#!/bin/sh

# Flag file used by Oasis to determine which implementation to use.  Still used??
USE_DBUS_FLAG_FILE=/etc/.pmipc-dbus
IPC_LIB_LINK=libPmIpcLib.so
IPC_DBUS_LIB=libpmipc-dbus.so.0

enabled() {
    if [ $(basename $(readlink ${IPC_LIB_LINK})) == $(basename ${IPC_DBUS_LIB}) ]; then 
        echo "enabled"; 
    else 
        echo "disabled"; 
    fi
}

cd /usr/lib

if [ "$1" == "disable" ]; then
    if [ $(enabled) == "disabled" ]; then echo "already disabled" && exit 0; fi
    ENABLE=0
elif [ "$1" == "enable" ]; then
    if [ $(enabled) == "enabled" ]; then echo "already enabled" && exit 0; fi
    ENABLE=1
elif [ "$1" == "status" ]; then
    echo "current status:" $(enabled)
    return
else
    echo "usage: $0 enable | disable | status"
    return
fi

cat <<EOF
This script will switch which version of pmipc is being used at both
the Linux and Java layers on reboot.  Once the necessary changes are
made your device must be rebooted.  Enter "yes" to confirm that you
want to make these changes and reboot.  Any other answer will exit
this script without making any changes.
EOF

echo -n "What's it gonna be boy: yes ... or ... no? => "

read response
if [ ! "$response" == "yes" ]; then
    echo "Ok; outta here"
    return
fi

[ -h ${IPC_LIB_LINK} ] || (echo "error: ${IPC_LIB_LINK} not a symbolic link" && return)

if [ "$ENABLE" == 1 ]; then
    ln -sf libpmipc-dbus.so.0 ${IPC_LIB_LINK}
    date > ${USE_DBUS_FLAG_FILE}
else
    # There's not much point in stopping stuff if we're about to reboot....
    stop dbus
    rm -f ${USE_DBUS_FLAG_FILE}
    ln -sf libPmIpcLibOld.so ${IPC_LIB_LINK}
fi

shutdown -r now
