#!/bin/sh

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

PATCH_VERSION="2.5a1"

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

# these are for webOS 1.2 and 1.3
OLD_UA_1_2='Mozilla/5.0 (webOS/%s; U; %s) AppleWebKit/525.27.1 (KHTML, like Gecko) Version/1.0 Safari/525.27.1 %s'
NEW_UA_1_2='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 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
        grep -Fq "$1" $FILE || do_error "Could not find area to patch (unknown file version?)"
        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 - 2010 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 to add an \"iPhone\" tag"
echo "to the identifying string it sends to web sites (along with the normal webOS"
echo "and Palm device tags). This causes most web sites that don't know about webOS"
echo "devices to think that it is an iPhone and send the iPhone optimized version"
echo "of the web page rather than a generic unoptimized version. Since the browser"
echo "on webOS devices can display the iPhone version of web pages correctly, this"
echo "allows nearly all mobile web sites to display an optimized version on webOS"
echo "devices even without direct support for them."
echo

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

# if this is webOS 1.2 or 1.3
if echo "$WEBOS_VERSION" | grep -Eq "^1\.[23]($|[^0-9])"
then
    OLD_UA="$OLD_UA_1_2"
    NEW_UA="$NEW_UA_1_2"
# if this is webOS 1.1
elif 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
        ;;

    -c|--clear-cookies|-clear-cookies|clear-cookies)
        echo "Clearing browser cookies only. Phone will reboot when done."
        ;;

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

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

clear_browser_cookies
sync
sleep 0.1
killall -HUP novacomd
/sbin/reboot || do_error "Reboot failed"
