#!/bin/sh

PATH=/bin:/sbin:/usr/bin:/usr/sbin
DATA=/tmp/data
SYSTEM=/tmp/system
UDISK=/tmp/udisk

echo Starting $0 `date`

mkdir -p ${DATA}
mkdir -p ${SYSTEM}
mkdir -p ${UDISK}

echo "Cleaning SETTINGS"
mount /dev/mmcblk2p7 ${UDISK}
rm -rf ${UDISK}/DCIM
cat /dev/null > ${UDISK}/.nomedia
umount ${UDISK}

mount /dev/mmcblk2p9 ${DATA}
mount /dev/mmcblk2p10 ${SYSTEM}

# cleaning up to free some space

if [ -f ${SYSTEM}/app/EbookDemo.apk ]
then
   echo "Free space. Removing EbookDemo"
   rm -f ${SYSTEM}/app/EbookDemo.*
fi

if [ -f ${SYSTEM}/app/EbookDemoService.apk ]
then
   echo "Free space. Removing EbookDemoService"
   rm -f ${SYSTEM}/app/EbookDemoService.*
fi

if [ -f ${SYSTEM}/app/LiveWallpapers.apk ]
then
   echo "Free space. Removing LiveWallpapers"
   rm -f ${SYSTEM}/app/LiveWallpapers.*
fi

if [ -f ${SYSTEM}/app/LiveWallpapersPicker.apk ]
then
   echo "Free space. Removing LiveWallpapersPicker"
   rm -f ${SYSTEM}/app/LiveWallpapersPicker.*
fi

if [ -f ${SYSTEM}/app/MagicSmokeWallpapers.apk ]
then
   echo "Free space. Removing MagicSmokeWallpapers"
   rm -f ${SYSTEM}/app/MagicSmokeWallpapers.*
fi

if [ -f ${SYSTEM}/app/VisualizationWallpapers.apk ]
then
   echo "Free space. Removing VisualizationWallpapers"
   rm -f ${SYSTEM}/app/VisualizationWallpapers.*
fi

if [ -f ${SYSTEM}/lib/libttfcore2.so ]
then
   echo "Free space. Removing BBEB leftovers"
   rm -f ${SYSTEM}/lib/libttfcore2.so
fi

# clean after old installations - potentially unnecessary
if [ -f ${DATA}/app/EbookLatinIME4Ebook.apk ]
then
   echo "Restoring Sony keyboard"
   rm -f ${DATA}/app/EbookLatinIME4Ebook.*

   echo "Reinstall EbookLatinIME4Ebook with custom signature"
   sed -i '/EbookLatinIME4Ebook/,/<\/package>/ { d }' ${DATA}/system/packages.xml
fi

# Root Explorer

if [ -d ${DATA}/data/com.speedsoftware.rootexplorer ]
then
   echo "Removing Root Explorer"
   rm -f ${DATA}/app/com.speedsoftware.rootexplorer*.apk
   rm -f ${DATA}/app/RootExplorer_*.apk
   rm -f ${DATA}/app/RootExplorer.apk
   rm -rf ${DATA}/data/com.speedsoftware.rootexplorer
   sed -i '/com\.speedsoftware\.rootexplorer/,/<\/package>/ { d }' ${DATA}/system/packages.xml
fi

# Total commander

if [ -d ${DATA}/data/com.ghisler.android.TotalCommander ]
then
   echo "Removing Total Commander"
   rm -f ${DATA}/app/tc_*.apk
   rm -f ${DATA}/app/tcandroid*.apk
   rm -f ${DATA}/app/com.ghisler.android.TotalCommander*.apk
fi

if [ -f ${SYSTEM}/app/EbookNetworkApplications.apk ]
then
   echo "Regenerating sysconf_db"
   rm -f ${DATA}/data/com.sony.drbd.ebook.netapp/databases/sysconf_db
fi

# proceeding with installation
if [ -d ${UPDATE_MP}/updates/data ]
then
   echo "Updating /data"
   cp -rfp ${UPDATE_MP}/updates/data/* ${DATA}

   cd ${UPDATE_MP}/updates/data
   find data -exec chmod a+rw ${DATA}/{} \;
   cd -
fi

if [ -d ${UPDATE_MP}/updates/system ]
then
   echo "Removing old system packages"
   cd ${UPDATE_MP}/updates/system
   find app -type f -name *.apk | sed -e 's/\(.*\.\)apk/\1odex/' -e 's:.*:'"${SYSTEM}"'\/&:' | xargs -t -r rm -f
   find framework -type f -name *.jar | sed -e 's/\(.*\.\)jar/\1odex/' -e 's:.*:'"${SYSTEM}"'\/&:' | xargs -t -r rm -f
   cd -

   echo "Updating /system"
   cp -rfp ${UPDATE_MP}/updates/system/* ${SYSTEM}

   echo "Changing system files modes"
   cd ${UPDATE_MP}/updates/system
   find app -type f -exec chmod a-x,go-w ${SYSTEM}/{} \;
   find lib -type f -exec chmod a-x,go-w ${SYSTEM}/{} \;
   find bin -type f -exec chmod a+x,go-w ${SYSTEM}/{} \;
   find bin -type f -exec chown 0.2000 ${SYSTEM}/{} \;
   find framework -type f -exec chmod a-x,go-w ${SYSTEM}/{} \;
   find usr -type f -exec chmod a-x,go-w ${SYSTEM}/{} \;
   cd -
fi

if [ -f ${DATA}/system/packages.xml ]
then
   echo "Getting rid of preferred activities in packages.xml"
   sed -i '/<preferred-activities>/,/<\/preferred-activities>/ { /preferred-activities/b; d }' ${DATA}/system/packages.xml

   echo "Cleaning system signatures"
   sed -i '/uid.system/,/perms/{/uid.system/b;/perms/b;d}' ${DATA}/system/packages.xml

   echo "Cleaning VM cache"
   rm -f ${DATA}/dalvik-cache/*
fi

if [ -f ${SYSTEM}/bin/su ]
then
   echo "Setting SuperUser"
   chmod 06755 ${SYSTEM}/bin/su
   mkdir -p ${SYSTEM}/xbin
   ln -fs /system/bin/su ${SYSTEM}/xbin/su
fi

umount ${DATA}
umount ${SYSTEM}

echo Done $0 `date`
