#! /bin/sh

. /etc/profile

DEFAULT_ROOTFS_NAME="rootfs-$MACHINE.tar.bz2"
DEFAULT_KERNEL_NAME="Image-$MACHINE.bin"
DEFAULT_INITRAMFS_NAME="recovery-$MACHINE.cpio.gz"
UPDATE_SRC_PATH="/home/diya/.update"
ROOTFS_DEV="/dev/mmcblk1p2"
SUPPORTED_COMMAND="rootfs kernel initramfs all"
TMP_MOUNT="/tmp/rootfs"


rootfs()
{
    filename=$1
    if [ -z "$filename" ]; then
        filename="$DEFAULT_ROOTFS_NAME"
    fi
    path="$UPDATE_SRC_PATH/$filename"
    backup_file="$UPDATE_SRC_PATH/rootfs-backup.img"
    echo "Checking rootfs at: $path"
    if [ ! -e "$path" ]; then
        echo "Error: rootfs file not found"
        return 1
    fi
    # backup the rootfs file
    echo "Backing up the current rootfs"
    if ! dd if=$ROOTFS_DEV of=$backup_file; then
        echo "Error: unable to backup current rootfs"
        return 1
    fi

    echo "Format rootfs partition"
    if ! mkfs.ext4 -F $ROOTFS_DEV; then
        echo "Error: Unable to format rootfs partition. Restore and quit"
        dd if=$backup_file of=$ROOTFS_DEV
        return 1
    fi
    mkdir -p $TMP_MOUNT
    echo "Mount rootfs partition to $TMP_MOUNT"
    if ! mount $ROOTFS_DEV $TMP_MOUNT; then
        echo "Error: Unable to mount rootfs partition. Restore and quit"
        dd if=$backup_file of=$ROOTFS_DEV
        return 1
    fi
    echo "Installing new rootfs"
    if ! tar -xpvf "$path" -C $TMP_MOUNT; then
        echo "Error: unable to install new rootfs. Restore and quit"
        umount $TMP_MOUNT
        dd if=$backup_file of=$ROOTFS_DEV
        return 1
    fi
    echo "Patch /etc/fstab"
    cat << EOF >> $TMP_MOUNT/etc/fstab

/dev/mmcblk1p1  /boot   vfat    defaults        0       0
/dev/mmcblk1p3  /var/etc        ext4    defaults        0       0
/dev/mmcblk1p4  /home   ext4    defaults        0       0

EOF
    sync
    echo "Unmount the rootfs partition"
    umount $TMP_MOUNT
    echo "Done"
    return 0
}

kernel()
{
    filename=$1
    if [ -z "$filename" ]; then
        filename="$DEFAULT_KERNEL_NAME"
    fi
    path="$UPDATE_SRC_PATH/$filename"
    echo "Checking kernel at: $path"
    if [ ! -e "$path" ]; then
        echo "Error: kernel file not found"
        return 1
    fi
    echo "Update kernel"
    cp -v "$path" /boot/kernel8.img
    cd /boot
    sync
    echo "Done"
    return 0
}

initramfs()
{
    filename=$1
    if [ -z "$filename" ]; then
        filename="$DEFAULT_INITRAMFS_NAME"
    fi
    path="$UPDATE_SRC_PATH/$filename"
    echo "Checking initramfs at: $path"
    if [ ! -e "$path" ]; then
        echo "Error: initramfs file not found"
        return 1
    fi
    echo "Update recovery intramfs"
    cp -v "$path" /boot/$DEFAULT_INITRAMFS_NAME
    cd /boot
    sync
    echo "Done"
    return 0
}

command_valid() {
    VALUE=$1
    echo $SUPPORTED_COMMAND | tr " " '\n' | grep -F -q -x "$VALUE"
}


name=$(basename $0)
cmd=${name#diya-update-}
file="$1"

if ! command_valid "$cmd"; then
    cmd="$1"
    file="$2"
fi

case "$cmd" in
    rootfs)
        rootfs $file
    ;;
    kernel)
        kernel $file
    ;;
    initramfs)
        initramfs $file
    ;;
    all)
        rootfs && kernel && initramfs
    ;;
    *)
cat << EOF
Usage: $name <command> [file]
    commands:
        - rootfs: update rootfs
        - kernel: update kernel image
        - initramfs: update recovery initramfs
EOF
    exit 1
    ;;
esac
