#!/bin/sh

THISDIR=/media/mmcblk0p1/Programs/_mackxpatch
INSTALLERDIR=/media/mmcblk0p1/Programs/Install
ZENITY=/media/mmcblk0p1/Programs/_base/bin/zenity
MTAG=/media/mmcblk0p1/Programs/_mackxpatch/bin/mtag

cd $THISDIR

install_schemas () {
    # Adapt gconf to enable the new views
    export GCONF_CONFIG_SOURCE=`gconftool-2 --get-default-source`
    # Install schemas for standard icons on Home-View
    /bin/cp -f ./schemas/standard.schemas /etc/gconf/schemas/standard.schemas
    gconftool-2 --makefile-install-rule /etc/gconf/schemas/standard.schemas

    # Application View
    /bin/cp -f ./schemas/applications.schemas /etc/gconf/schemas/applications.schemas
    gconftool-2 --makefile-install-rule /etc/gconf/schemas/applications.schemas

    # Installers View
    /bin/cp -f ./schemas/installers.schemas /etc/gconf/schemas/installers.schemas
    gconftool-2 --makefile-install-rule /etc/gconf/schemas/installers.schemas
    # copy the icons
    /bin/cp -f ./icons/icon-download-medium.png /usr/share/ctb/icon-download-medium.png
    /bin/cp -f ./icons/icon-download-small.png /usr/share/ctb/icon-download-small.png
    /bin/chmod a-x /usr/share/ctb/icon-download-*

    # Favourites View
    /bin/cp -f ./schemas/favourites.schemas /etc/gconf/schemas/favourites.schemas
    gconftool-2 --makefile-install-rule /etc/gconf/schemas/favourites.schemas

    # Recently Read View
    /bin/cp -f ./schemas/recently_read.schemas /etc/gconf/schemas/recently_read.schemas
    gconftool-2 --makefile-install-rule /etc/gconf/schemas/recently_read.schemas
    # copy the icons
    /bin/cp -f ./icons/icon-recentlyread-medium.png /usr/share/ctb/icon-recentlyread-medium.png
    /bin/cp -f ./icons/icon-recentlyread-small.png /usr/share/ctb/icon-recentlyread-small.png
    /bin/chmod a-x /usr/share/ctb/icon-recentlyread-*

    # Xournal View
    /bin/cp -f ./schemas/xournal.schemas /etc/gconf/schemas/xournal.schemas
    gconftool-2 --makefile-install-rule /etc/gconf/schemas/xournal.schemas

    # Views View
    /bin/cp -f ./schemas/views.schemas /etc/gconf/schemas/views.schemas
    gconftool-2 --makefile-install-rule /etc/gconf/schemas/views.schemas
    # copy the icons
    /bin/cp -f ./icons/icon-view-medium.png /usr/share/ctb/icon-view-medium.png
    /bin/cp -f ./icons/icon-view-small.png /usr/share/ctb/icon-view-small.png
    /bin/chmod a-x /usr/share/ctb/icon-view-*

    # Disable build-in views
    # gconftool-2 --type string --set /apps/er/patch/ctb/views/recent/mode "disabled"

    # Enable the appropriate views
    gconftool-2 --type string --set /apps/er/patch/ctb/views/application/mode "enabled"
    gconftool-2 --type string --set /apps/er/patch/ctb/views/installer/mode "enabled"
    gconftool-2 --type string --set /apps/er/patch/ctb/views/favourite/mode "enabled"
    gconftool-2 --type string --set /apps/er/patch/ctb/views/recentlyread/mode "enabled"
    gconftool-2 --type string --set /apps/er/patch/ctb/views/xournal/mode "enabled"
    gconftool-2 --type string --set /apps/er/patch/ctb/views/views/mode "enabled"

    # Create Backup of dr.ini
    /bin/cp -f /media/mmcblk0p1/System/dr.ini /media/mmcblk0p1/System/dr.patch.ini
    # and save the (just modified) settings to disk
    /usr/bin/erconftool -e /apps/er
    
    # These settings are only used after a reboot of the system, so when creating a script
    # to add extra Views, also add a reboot of the device (see below).
}

install_mandatory_schemas () {
    # Adapt gconf to enable the new views
    export GCONF_CONFIG_SOURCE=`gconftool-2 --get-default-source`

    # Entries for last apps
    /bin/cp -f ./schemas/last_apps.schemas /etc/gconf/schemas/last_apps.schemas
    gconftool-2 --makefile-install-rule /etc/gconf/schemas/last_apps.schemas

    # Entries for startup view
    /bin/cp -f ./schemas/ctb_view.schemas /etc/gconf/schemas/ctb_view.schemas
    gconftool-2 --makefile-install-rule /etc/gconf/schemas/ctb_view.schemas

    # Entries for mdbindex threshold
    /bin/cp -f ./schemas/ctb_jitview.schemas /etc/gconf/schemas/ctb_jitview.schemas
    gconftool-2 --makefile-install-rule /etc/gconf/schemas/ctb_jitview.schemas

    # Entries for mdbindex logging
    /bin/cp -f ./schemas/ctb_indexing.schemas /etc/gconf/schemas/ctb_indexing.schemas
    gconftool-2 --makefile-install-rule /etc/gconf/schemas/ctb_indexing.schemas

    # user-views can have different numbers when new user views are installed, so set
    # startup view to 0 after install
    #gconftool-2 --type int --set /apps/er/patch/ctb/startupmode 0
    # Make sure that after install no document is started automatically
    gconftool-2 --type int --set /apps/er/lastapps/active 0
    
    # Extra entries for sensor after reboot
    /bin/cp -f ./schemas/ctb_sensor.schemas /etc/gconf/schemas/ctb_sensor.schemas
    gconftool-2 --makefile-install-rule /etc/gconf/schemas/ctb_sensor.schemas

    # Extra entries for clock in menu bar
    /bin/cp -f ./schemas/popup_clock.schemas /etc/gconf/schemas/popup_clock.schemas
    gconftool-2 --makefile-install-rule /etc/gconf/schemas/popup_clock.schemas

    # Default Tags
    /bin/cp -f ./schemas/ctb_tags.schemas /etc/gconf/schemas/ctb_tags.schemas
    gconftool-2 --makefile-install-rule /etc/gconf/schemas/ctb_tags.schemas

    # Extra entries for idle time and rgbled
    /bin/cp -f ./schemas/sys_extra.schemas /etc/gconf/schemas/sys_extra.schemas
    gconftool-2 --makefile-install-rule /etc/gconf/schemas/sys_extra.schemas

}

backup_firmware () {
    # Save originals ONLY if they do not exists
    if [ ! -e $THISDIR/ctb_original ]
    then
        /bin/cp -f /usr/bin/ctb $THISDIR/ctb_original
    fi

    if [ ! -e $THISDIR/mdbindex_original ]
    then
        /bin/cp -f /usr/bin/mdbindex $THISDIR/mdbindex_original
    fi

    if [ ! -e $THISDIR/popupmenu_original ]
    then
        /bin/cp -f /usr/bin/popupmenu $THISDIR/popupmenu_original
    fi

    if [ ! -e $THISDIR/settings_original ]
    then
        /bin/cp -f /usr/bin/settings $THISDIR/settings_original
    fi

    if [ ! -e $THISDIR/sysd_original ]
    then
        /bin/cp -f /usr/bin/sysd $THISDIR/sysd_original
    fi

    if [ ! -e $THISDIR/libermetadb_original ]
    then
        /bin/cp -f /usr/lib/libermetadb.so.0.0.0 $THISDIR/libermetadb_original
    fi

    if [ ! -e $THISDIR/libergtk_original ]
    then
        /bin/cp -f /usr/lib/libergtk.so.0.0.1 $THISDIR/libergtk_original
    fi

    if [ ! -e $THISDIR/globaldb_original ]
    then
        /bin/cp -f /usr/share/ctb/settings/global.db $THISDIR/globaldb_original
    fi

    if [ ! -e $THISDIR/ctbmoru_original ]
    then
        /bin/cp -f /usr/share/locale/ru/LC_MESSAGES/ctb.mo $THISDIR/ctbmoru_original
    fi
}

install_patched_files () {
    # overwrite existing files
    /bin/cp -f $THISDIR/bin/ctb /usr/bin/
    /bin/cp -f $THISDIR/bin/mdbindex /usr/bin/
    /bin/cp -f $THISDIR/bin/popupmenu /usr/bin/
    /bin/cp -f $THISDIR/bin/settings /usr/bin/
    /bin/cp -f $THISDIR/bin/sysd /usr/bin/
    /bin/cp -f $THISDIR/lib/libermetadb.so.0.0.0 /usr/lib/
    /bin/cp -f $THISDIR/lib/libergtk.so.0.0.1 /usr/lib/

    /bin/cp -f $THISDIR/share/ctb/settings/global.db /usr/share/ctb/settings/
    # Copy extra files
    /bin/cp -f $THISDIR/share/ctb/settings/mackx_patch.desktop /usr/share/ctb/settings/
    /bin/cp -f $THISDIR/share/ctb/settings/lastapps.desktop /usr/share/ctb/settings/
    /bin/cp -f $THISDIR/share/ctb/settings/topled.desktop /usr/share/ctb/settings/
    /bin/cp -f $THISDIR/share/ctb/settings/waitdialog.desktop /usr/share/ctb/settings/
    /bin/cp -f $THISDIR/share/ctb/settings/indexing.desktop /usr/share/ctb/settings/
    /bin/cp -f $THISDIR/share/ctb/settings/tags.desktop /usr/share/ctb/settings/
    
    # icons for properties
    /bin/cp -f $THISDIR/share/ctb/*.png /usr/share/ctb
	
	# locales only russian is available for now
	/bin/cp -f $THISDIR/share/locale/ru/LC_MESSAGES/ctb.mo /usr/share/locale/ru/LC_MESSAGES/
    
    #/bin/cp -f $THISDIR/mxp_version /etc/
}

remove_unused_files () {
    # These files are not needed anymore, so remove them
    /bin/rm -f $INSTALLERDIR/AddAnyFolderView.desktop
    /bin/rm -f $INSTALLERDIR/RestoreMackxPatch.desktop
    /bin/rm -f $THISDIR/_add_anyfolder_view.sh
    /bin/rm -f $THISDIR/_restore.sh
}

restore_original_files () {
    # Restore the original program(s)
    if [ -e $THISDIR/ctb_original ]
    then
        /bin/cp $THISDIR/ctb_original /usr/bin/ctb
        /bin/rm -f $THISDIR/ctb_original
    fi

    if [ -e $THISDIR/mdbindex_original ]
    then
        /bin/cp $THISDIR/mdbindex_original /usr/bin/mdbindex
        /bin/rm -f $THISDIR/mdbindex_original
    fi

    if [ -e $THISDIR/popupmenu_original ]
    then
        /bin/cp $THISDIR/popupmenu_original /usr/bin/popupmenu
        /bin/rm -f $THISDIR/popupmenu_original
    fi

    if [ -e $THISDIR/settings_original ]
    then
        /bin/cp $THISDIR/settings_original /usr/bin/settings
        /bin/rm -f $THISDIR/settings_original
    fi

    if [ -e $THISDIR/sysd_original ]
    then
        /bin/cp $THISDIR/sysd_original /usr/bin/sysd
        /bin/rm -f $THISDIR/sysd_original
    fi

    if [ -e $THISDIR/libermetadb_original ]
    then
        /bin/cp $THISDIR/libermetadb_original /usr/lib/libermetadb.so.0.0.0
        /bin/rm -f $THISDIR/libermetadb_original
    fi

    if [ -e $THISDIR/libergtk_original ]
    then
        /bin/cp $THISDIR/libergtk_original /usr/lib/libergtk.so.0.0.1
        /bin/rm -f $THISDIR/libergtk_original
    fi

    if [ -e $THISDIR/globaldb_original ]
    then
        /bin/cp $THISDIR/globaldb_original /usr/share/ctb/settings/global.db
        /bin/rm -f $THISDIR/globaldb_original
    fi

    if [ -e $THISDIR/ctbmoru_original ]
    then
        /bin/cp $THISDIR/ctbmoru_original /usr/share/locale/ru/LC_MESSAGES/ctb.mo
        /bin/rm -f $THISDIR/ctbmoru_original
    fi

    # /bin/rm -f /etc/mxp_version
    
    # TODO: should gconf settings also be removed? On the next install the available settings
    #       will be used automatically
}

add_tags_to_files () {
    # Mark the desktop file as Installer so it will show up in the installer view
    # When using this tool the syntax is: 
    # mtag     [file]                            [title]           [author] [tag]
    $MTAG $INSTALLERDIR/ApplyMackxPatch.desktop 'Install Mackx Hack' Mackx install
    # Valid tags, used by this hack are: application, fav1, install and xournal.
    # see also _mackxpatch/schemas
}


if [ -f $THISDIR/ctb_original ]; then
    $ZENITY --question --title "MackxPatch" --text "<b>De-install</b>\n\nDo you want to restore original IREX programs?"
    if [ $? -eq 0 ]; then
        # migrate thumbs
        $THISDIR/installfiles/migrate-thumbs --fs2db -q
        if [ $? -ne 0 ] ; then
            echo
            echo "Can't migrate thumbnails from file system to global.db"
			$ZENITY --info --title "MackxPatch" --text "Can't migrate thumbnails from \nfile system to global.db\n\nPress ok to reboot"
        else
            echo "Restoring original files"
            restore_original_files
            #remove_schema
            rm $THISDIR/dr1000hackx.installed >/dev/null 2>&1
        fi
        echo "Restoring original files"
        restore_original_files
        
        $ZENITY --info --title "MackxPatch" --text "<b>Patch Removed</b>\n\nPress ok to reboot"
        /sbin/reboot
        sleep 31
    fi
fi

$ZENITY --question --title "MackxPatch" --text "<b>Upgrade</b>\n\nDo you want to install MackxPatch?"
if [ $? -eq 0 ]; then
    # We will only come here if de-install was not choosen!!

	# first backup original programs, only if they don't exist
	if [ ! -f $THISDIR/ctb_original ]; then
		echo "Backingup original firmware files"
		backup_firmware
	fi
	# migrate thumbs
	$THISDIR/installfiles/migrate-thumbs --db2fs -q
	# The tools return an 'error' if files are already converted
	#if [ $? -ne 0 ] ; then
	#	echo
	#	echo "Can't migrate thumbnails from global.db to file system"
	#else
		# install schemas
		install_mandatory_schemas
		# Ask if views need to be installed
		$ZENITY --question --title "MackxPatch" --text "<b>Document Views</b>\n\nDo you want to install the schemas for the new Views?"
		if [ $? -eq 0 ]; then
			echo "Installing schemas for new views"
			install_schemas
		else
			echo "Not installing views"
		fi

		# tag installation file
		add_tags_to_files
		# install patches files
		install_patched_files
		# store "package is installed"
		touch $THISDIR/dr1000hackx.installed
	#fi

    # For the new version to be used, a reset is needed
	# Do not show a dialog, the new libraries will crash the DR when they are used ...
    # $ZENITY --info --title "MackxPatch" --text "<b>Patch Applied</b>\n\nPress ok to reboot"
    /sbin/reboot
    sleep 31
fi
    