#!/bin/sh

# Uncomment the following line to get debug info
#set -x

# This is to avoid expanding '*' in fdisk results
set -f

source /usr/local/lib/utils

SELF=$(basename $0)

# Find out the root partition number from the kernel command line
root_part=$(cat /proc/cmdline | sed -n 's|^.*root=\([^ ]*\).*|\1|p')
root_part_num=${root_part#/dev/mmcblk0p}
if [ "${root_part_num}" -eq 1 ]; then
    die 0 "recovery mode"
elif [ "${root_part_num}" = "{$root_part}" ]; then
    die 1 "${root_part} is not an SD card partition, aborting"
elif [ "${root_part_num}" -ne 2 ]; then
    die 2 "unknown partition layout, aborting"
fi
let swap_part_num=${root_part_num}+1
swap_part=/dev/mmcblk0p${swap_part_num}
let share_part_num=${swap_part_num}+1
share_part=/dev/mmcblk0p${share_part_num}

check_root_id () {
    [ $(id -u) -ne 0 ] && die 3 "this script must be run as root, aborting"
    return 0
}

resize_rootfs_partition () {

    # Check that the last partition is the rootfs partition
    local last_part_line=$(fdisk -l /dev/mmcblk0 2>/dev/null | tail -n 1)
    set ${last_part_line}
    local last_part_num=${1#/dev/mmcblk0p}
    local part_start=${3}
    if [ "${last_part_num}" != "${root_part_num}" ]; then
	die 4 "rootfs is not the last partition. Don't know how to expand, aborting"
    fi

    # Remove (temporarily) the rootfs partition
    # Re-create the rootfs partition with a 1GB size
    fdisk /dev/mmcblk0 >/dev/null 2>&1 <<EOF
d
${root_part_num}
n
p
${root_part_num}
${part_start}
+1G
w
EOF

    # Mark the rootfs partition as bootable
    sfdisk -A /dev/mmcblk0 ${root_part_num} >/dev/null 2>&1 || die 7 "cannot make the rootfs partition bootable, aborting"

    return 0
}

reload_partition_table () {
    partprobe /dev/mmcblk0 >/dev/null 2>&1 || die 9 "cannot reload the partition table, aborting"
    return 0
}

resize_rootfs_filesystem () {
    rw
    resize2fs ${root_part} >/dev/null 2>&1 || die 10 "cannot resize the root filesystem, aborting"
    ro
    return 0
}

create_swap () {
    mount | grep -q ${share_part}
    if [ $? -ne 0 ]; then

	# Check that the last partition is the rootfs partition
	local last_part_line=$(fdisk -l /dev/mmcblk0 2>/dev/null | tail -n 1)
	set ${last_part_line}
	local last_part_num=${1#/dev/mmcblk0p}
	if [ "$last_part_num" != "$root_part_num" ]; then
	    die 11 "rootfs is not the last partition. Don't know how to create the backing store partition"
	fi

	# Create an additional linux swap partition
	let swap_part_num=${last_part_num}+1
	swap_part=/dev/mmcblk0p${swap_part_num}
	fdisk /dev/mmcblk0 >/dev/null 2>&1 <<EOF
n
p
${swap_part_num}

+128M
t
${wap_part_num}
82
w
EOF
	mkswap ${swap_part} >/dev/null 2>&1
	if [ $? -ne 0 ]; then
	    die 14 "cannot create swap file, aborting"
	fi
    fi
    return 0
}

enable_swap () {
    swapon -a >/dev/null 2>&1 || die 15 "cannot enable swap file, aborting"
    return 0
}

create_backing_store_partition () {
    mount | grep -q ${share_part}
    if [ $? -ne 0 ]; then

	# Check that the last partition is the swap partition
	local last_part_line=$(fdisk -l /dev/mmcblk0 2>/dev/null | tail -n 1)
	set ${last_part_line}
	local last_part_num=${1#/dev/mmcblk0p}
	if [ "${last_part_num}" != "${swap_part_num}" ]; then
	    die 15 "rootfs is not the last partition. Don't know how to create the backing store partition"
	fi

	# Create an additional FAT32 share partition that fills the disk
	let share_part_num=${last_part_num}+1
	share_part=/dev/mmcblk0p${share_part_num}
	fdisk /dev/mmcblk0 >/dev/null 2>&1 <<EOF
n
p
${share_part_num}


t
${share_part_num}
c
w
EOF
	sync
    fi
    return 0
}

format_backing_store_partition () {

    # Format the backing store as FAT32
    mkfs.vfat ${share_part} >/dev/null 2>&1 || die 17 "cannot format the backing store partition"
    return 0
}

copy_files_to_store_partition () {
    # Add file to force assembly tests
    
    
    mount /mnt/ || die 18 "Cannot mount /mnt"
    unzip -q -o /usr/local/share/mnt_freware_games.zip -d /mnt/
    mkdir -p /mnt/apps
    set +f
    cp -f /usr/games/opk/*.opk /mnt/apps/
    set -f
    umount /mnt/ || die 20 "Cannot unmount /mnt"
    return 0
}

check_root_id
notif " FIRST BOOT DETECTED"

notif " 1/9 RESIZE ROOT PARTITION"
resize_rootfs_partition

notif " 2/9 RELOAD ROOT PARTITION"
reload_partition_table

notif " 3/9 RESIZE ROOT FILESYSTEM"
resize_rootfs_filesystem

notif " 4/9 CREATE SWAP"
create_swap

notif " 5/9 ENABLE SWAP"
enable_swap

notif " 6/9 CREATE USB PARTITION"
create_backing_store_partition

notif " 7/9 RELOAD PARTITION TABLE"
reload_partition_table

notif " 8/9 FORMAT USB PARTITION"
format_backing_store_partition

notif " 9/9 COPY FILES TO ^    USB PARTITION"
copy_files_to_store_partition

notif " FIRST BOOT SETUP FINISHED!"

sleep 1
clear_notif
