#!/bin/bash

# import functions library
source rescue_common

tmpdir="/tmp/flash-rescue-root"
rootdir="$tmpdir/pen"

media_dir="/tmp/media"
upgrade_dir="$media_dir/upgrade"


config_files_network_scripts="etc/sysconfig/network-scripts/ifcfg-* \
                              etc/sysconfig/network-scripts/cellular.d \
                              etc/sysconfig/network-scripts/vpn.d \
                              etc/sysconfig/network-scripts/wireless.d"

config_files_users="etc/passwd etc/shadow etc/group etc/gshadow"
config_files_time="etc/localtime etc/ntp etc/ntp.conf"
config_files="etc/sysconfig/* $config_files_users $config_files_time \
              etc/wpa_supplicant.conf etc/shorewall etc/kde \
              etc/udev/rules.d/61-*_config.rules"

config_files_to_remove="etc/sysconfig/harddrake2"

function prepare() {

    echo "Mounting Mandriva Flash key..."
    echo

    modprobe unionfs > /dev/null 2>&1
    modprobe squashfs > /dev/null 2>&1
    modprobe loop > /dev/null 2>&1

    mkdir -p $rootdir
    mkdir -p $tmpdir/squash
    mkdir -p $tmpdir/user
    mkdir -p $tmpdir/union

    if ! mount_usbroot $rootdir; then
        return 1
    fi

    mount -t ext2 -o loop $sys_loop $tmpdir/user > /dev/null 2>&1
    if [ $? -ne 0 ]; then
        echo "Error mounting system.loop"
        return 1
    fi

    return 0
}

function get_existing_rpms() {

    echo "Getting list of installed packages..."
    echo

    mount -t squashfs -o loop $sfs_loop $tmpdir/squash > /dev/null 2>&1
    if [ $? -ne 0 ]; then
        echo "Error mounting distrib.sqfs"
        return 1
    fi

    mount -t unionfs -o dirs=$tmpdir/user=rw:$tmpdir/squash=ro \
        none $tmpdir/union
    if [ $? -ne 0 ]; then
        echo "Error creating union of distrib.sqfs and system.loop"
        return 1
    fi

    chroot $tmpdir/squash rpm -qa | sort > /tmp/previous_rpms.list
    if [ $? -ne 0 ]; then
        echo "Error getting list of vanilla rpms installed on previous key"
    fi

    chroot $tmpdir/union rpm -qa | sort > /tmp/existing_rpms.list
    if [ $? -ne 0 ]; then
        echo "Error getting list of rpms installed prior to upgrade"
    fi

    umount $tmpdir/union > /dev/null 2>&1
    lumount $tmpdir/squash > /dev/null 2>&1

    mkdir -p $tmpdir/user/root
    diff /tmp/previous_rpms.list /tmp/existing_rpms.list | sed -n 's/^> //p' > $tmpdir/user/root/new_existing_rpms.list

    return 0
}

function move_files() {
    for i in $*; do
	if [ -e $i ]; then
	    dir=`dirname $i`
	    [ "$dir" = "." ] || mkdir -p ..keep/$dir
	    mv $i ..keep/$dir || { echo "Error moving $i"; return 1; }
	fi
    done

    return 0
}

function remove_non_user_nor_config_files() {

    echo "Filtering system configuration files and user accounts..."
    echo

    cd $tmpdir/user

    rm -rf $config_files_to_remove

    if [ -e ..keep ]; then
	mv ..keep ..keep.bak
    fi
    if [ -e ..keep ]; then
	echo "remove_non_user_nor_config_files can't work with existing temp dir"
	return 1
    fi

    mkdir ..keep

    move_files $config_files_network_scripts || return 1
    rm -rf etc/sysconfig/network-scripts

    move_files home root $config_files || return 1
    rm -rf *
    rm -rf .[^.]*

    mv ..keep/* .
    rmdir ..keep

    cd - > /dev/null

    return 0
}

function copy_new_version() {

    echo "Copying new Mandriva Flash system..."

    rm -rf $rootdir$boot_rel
    rm -f $rootdir$loopbacks_rel/.*sqf
    rm -f $rootdir$loopbacks_rel/.*sqfs
    [ -d "$modules_dir" ] && rm -rf $modules_dir

    tar c -C $upgrade_dir . | pv -s `du -sb $upgrade_dir | cut -f 1` | tar x -C $rootdir
    echo

    chmod -R u+w $rootdir
    chmod -R u-w $rootdir$boot_rel $rootdir$loopbacks_rel

    new_label=$(source_version_variables "2.0"; echo $label)
    MTOOLS_SKIP_CHECK=1 mlabel -i $usbroot_device ::$new_label

    return 0
}

function merge_config_files() {

    echo "Merging user and group accounts in new system..."
    echo

    mount -t squashfs -o loop $sfs_loop $tmpdir/squash > /dev/null 2>&1
    if [ $? -ne 0 ]; then
        echo "Error mounting distrib.sqfs"
        return 1
    fi

    upgrade.merge-users $tmpdir/user/etc $tmpdir/squash/etc

    mount -t unionfs -o dirs=$tmpdir/user=rw:$tmpdir/squash=ro \
        none $tmpdir/union
    if [ $? -ne 0 ]; then
        echo "Error creating union of distrib.sqfs and system.loop"
        return 1
    fi

    #- reapply lang configuration, mostly to rewrite kdeglobals
    chroot $tmpdir/union perl -I/usr/lib/libDrakX -Mlang -e "lang::write(lang::read())" &>/dev/null

    #- remove KDE sessions file so that previous compiz usage does not break session
    rm -f $tmpdir/union/home/*/.kde/share/config/ksmserverrc &>/dev/null

    #- remove KDE autostart files that should have been migrated by startkde...
    rm -f $tmpdir/union/home/*/.kde/Autostart/{alignment-icons,openkmenu}.desktop &>/dev/null

    return 0
}

function doit() {

    if [ -d $tmpdir/user/var/lib/rpm/Packages ]; then
        get_existing_rpms || return 1
    fi

    remove_non_user_nor_config_files || return 1

    copy_new_version || return 1

    merge_config_files || return 1

    return 0
}

function cleanup() {
    cd /

    umount $tmpdir/union > /dev/null 2>&1
    lumount $tmpdir/user > /dev/null 2>&1
    lumount $tmpdir/squash > /dev/null 2>&1
    umount $rootdir > /dev/null 2>&1

    rmdir $tmpdir/union $tmpdir/user $tmpdir/squash 2> /dev/null
    rmdir $rootdir $tmpdir 2> /dev/null
}

clear
trap cleanup SIGINT

version=""
if ! insert_pendrive; then
    exit 1
fi

#- works only when upgrading versions with the same layout
sys_loop="$rootdir$loopbacks_rel/system.loop"
sfs_loop="$rootdir$loopbacks_rel/distrib.sqfs"

modules_dir="$rootdir$loopbacks_rel/modules"

echo

if ! prepare; then
    cleanup
    exit 1
fi

if ! doit; then
    cleanup
    exit 1
fi

echo "Please wait, finishing copy to key..."
echo

cleanup

echo "Congratulations! Your Mandriva Flash system is now upgraded."
echo

exit 0