#!/bin/sh

# User Agent Spoofer for webOS devices (universal)
# Copyright 2009 Carl E. Thompson (devel-webos [at] carlthompson.net)
# Much help from hofs1 and wtgreen at PreCentral.net . Thank you.

PATCH_VERSION="2.2"

# these must be the same length
OLD_UA='Mozilla/5.0 (webOS/%s; U; %s) AppleWebKit/525.27.1 (KHTML, like Gecko) Version/1.0 Safari/525.27.1 %s'
NEW_UA='Mozilla/5.0 (iPhone; U; en)(webOS/%s; U; %s) AppleWebKit/525.27.1 Version/1.0 Safari/525.27.1 %s -CET'

# these are for the older webOS 1.1
OLD_UA_1_1='Mozilla/5.0 (webOS/1.1; U; %s) AppleWebKit/525.27.1 (KHTML, like Gecko) Version/1.0 Safari/525.27.1 Pre/1.0'
NEW_UA_1_1='Mozilla/5.0 (iPhone; U; en)(webOS/1.1; U; %s) AppleWebKit/525.27.1 Version/1.0 Safari/525.27.1 Pre/1.0 -CET'

WEBOS_VERSION=$(cat /etc/palm-build-info | sed -nre "s/^PRODUCT_VERSION_STRING\s*=\s*Palm webOS\s*(.*)/\1/p")

FILE="/usr/lib/libWebKitLuna.so"
BACKUP_FILE="/usr/lib/libWebKitLuna.so-iphone_user_agent-${WEBOS_VERSION}.bak"
TEMP_FILE="/tmp/libWebKitLuna.so-iphone_user_agent.patched"

do_error() { while [ -n "$1" ]; do echo "ERROR: $1" >&2; shift; done; exit 1; }

options() { echo "Available command line options: install, uninstall, restore-backup, help"; }

clear_browser_cookies()
{
    cp -f /var/palm/data/browser-cookies.db . || do_error "Could not copy browser cookies database"
    sqlite3 browser-cookies.db "delete from Cookies;" || do_error "Could not clear browser cookies"
    mv -f browser-cookies.db /var/palm/data/browser-cookies.db || do_error "Could not update browser cookies database"
}

remove_all_backups() { rm -f /usr/lib/libWebKitLuna.so-iphone_user_agent*; }

sedify() { echo "$1" | sed -e 's/\//\\\//g' -e 's/(/\\(/g' -e 's/)/\\)/g'; }

do_patch()
{
        if grep -Fq "$2" $FILE
        then
            echo "User agent already in desired state so no action appears necessary. Exiting."
            exit 0
        fi
        OLD=$(sedify "$1")
        NEW=$(sedify "$2")
        sed -re "s/$OLD/$NEW/" $FILE > $TEMP_FILE || do_error "Could not patch file"
        mv -f $TEMP_FILE $FILE || do_error "Could not rename patched file to original name"
}

remount() { mount / -oremount,rw || do_error "Could not remount root filesystem read/write"; }

echo "User Agent Spoofer version $PATCH_VERSION for all Palm webOS devices"
echo "Copyright 2009 Carl E. Thompson (devel-webos [at] carlthompson.net)"
echo "Much help from hofs1 and wtgreen at PreCentral.net . Thank you."
echo
echo "This program patches the web browser on a webOS device so that it appears to"
echo "web servers as an iPhone. This allows nearly all mobile web sites to render"
echo "properly."
echo

[ -n "$WEBOS_VERSION" ] || do_error "This patch only runs on Palm webOS devices"

# if this is webOS 1.1
if echo "$WEBOS_VERSION" | grep -Eq "^1\.1($|[^0-9])"
then
    OLD_UA="$OLD_UA_1_1"
    NEW_UA="$NEW_UA_1_1"
fi

# Make sure user agent strings are the same length
[ "${#OLD_UA}" = "${#NEW_UA}" ] || do_error "Old and new user agent strings are not the same length"

[ -n "$1" ] && OPT="$1" || OPT="install"

case "$OPT" in
    -u|--uninstall|-uninstall|uninstall)
        echo "Removing user agent spoof patch. Phone will reboot when done."
        remount
        do_patch "$NEW_UA" "$OLD_UA"
        remove_all_backups
        ;;

    -i|--install|-install|install)
        echo "Applying user agent spoof patch. Phone will reboot when done."
        remount
        remove_all_backups
        cp -f $FILE $BACKUP_FILE || do_error "Could not copy original file to backup"
        do_patch "$OLD_UA" "$NEW_UA"
        ;;

    -r|--restore-backup|-restore-backup|restore-backup)
        echo "Restoring library file from backup. Phone will reboot when done."
        [ -r $BACKUP_FILE ] || do_error "Backup file for this version of webOS not found (not installed?)"
        remount
        mv -f $BACKUP_FILE $FILE || do_error "Restoring from backup failed"
        remove_all_backups
        ;;

    -h|--help|-help|help)
        options
        exit 0
        ;;

    *)
        do_error "Unknown option: $OPT" "$(options)"
        ;;
esac

clear_browser_cookies
sync
reboot || do_error "Reboot failed"
