#!/bin/sh
# Update partition script

source /usr/local/lib/utils

root_part_num=2
root_part=/dev/mmcblk0p${root_part_num}
root_mount=/tmp/rootfs

do_preinst()
{
    notif "1/5 Extract update"
    exit 0
}

do_postinst()
{
    local version

    notif "2/5 Resize root filesystem"
    resize2fs ${root_part}
    if [ $? -ne 0 ]; then
	notif "Cannot resize root filesystem"
	exit 1
    fi
    notif "3/5 Mount root filesystem"
    mkdir -p ${root_mount}
    mount -t ext4 ${root_part} ${root_mount}
    if [ $? -ne 0 ]; then
	notif "Cannot mount root filesystem"
	exit 1
    fi
    notif "4/5 Unmount root filesystem"
    touch "${root_mount}/.first_boot"
    umount ${root_mount}
    if [ $? -ne 0 ]; then
	notif "Cannot unmount root filesystem"
	exit 1
    fi
    for file in $(ls /mnt/FunKey-rootfs-*.swu); do
	notif "5/5 Erase update file"
	rm -f "${file}"
    done
    exit 0
}

echo $0 $1

case "$1" in
preinst)
    notif "call do_preinst"
    do_preinst
    ;;
postinst)
    notif "call do_postinst"
    do_postinst
    ;;
*)
    notif "default"
    exit 1
    ;;
esac
