#!/bin/bash

# Mount the sys, proc, tmpfs filesystems
mount -t sysfs /sys /sys
mount -t tmpfs /tmp /tmp
mount -t proc /proc /proc
mkdir -p /dev/pts
mount -t devpts devpts /dev/pts

# Touch /tmp/resolv.conf, to let dhclient-script work correctly
touch /tmp/resolv.conf

# Set the hostname
/bin/hostname -F /etc/hostname

# Raise network interface
/sbin/ifconfig eth0 up

# Provide a fixed link-local address to eth0:1 virtual interface (give 5 seconds timeout
/sbin/ifconfig eth0:1 169.254.120.120/16 

# Run dhclient in daemon mode
/sbin/dhclient -lf /tmp/dhclient.leases -pf /tmp/dhclient.pid -nw eth0

# Start ntp client daemon
ntpd -g -G

# Launch dropbear in daemon mode
/usr/sbin/dropbear -B -b /etc/dropbear/banner

# Spawn a shell on ttyS2 serial port, always available
#setsid getty --autologin root ttyS2 &
getty --autologin root ttyS2 &

# Check if the multitool "MT" partition requires a resize
DEVICE_MT_PARTITION=$(blkid --label "MULTITOOL" -l) # eg: /dev/mmcblk0p2
BOOT_DEVICE="/dev/$(lsblk -n -o PKNAME $DEVICE_MT_PARTITION)"

CLASS_MT_PARTITION="/sys/class/block/$(lsblk -n -o KNAME $DEVICE_MT_PARTITION)" # eg: /sys/class/block/mmcblk0p2
CLASS_BOOT_DEVICE="/sys/class/block/$(lsblk -n -o PKNAME $DEVICE_MT_PARTITION)" # eg: /sys/class/block/mmcblk0

if [ -b "${DEVICE_MT_PARTITION}" ]; then

	MAX_DIFF_SPACE=$((1024 * 1024)) # a difference in spare space greater than this amount
					# triggers a resize (1 GB)

	THRES_DIFF_SPACE=$((128 * 1024)) # fill the MULTITOOL partition up to this space (max - 128 MB)
	
	MT_PARTITION_NUMBER=$(cat "$CLASS_MT_PARTITION/partition") # partition number
	MT_PARTITION_START=$(cat "$CLASS_MT_PARTITION/start") # sectors
	MT_PARTITION_SIZE=$(cat "$CLASS_MT_PARTITION/size") # sectors

	DEVICE_SIZE=$(cat "$CLASS_BOOT_DEVICE/size")
	SPARE_SPACE=$(($DEVICE_SIZE - $MT_PARTITION_START))

	SPARE_SPACE=$(($DEVICE_SIZE / 2)) # sectors / 2 = kbytes
	MT_PARTITION_SIZE=$(($MT_PARTITION_SIZE / 2)) # sectors / 2 = kbytes

	DIFF_SPACE=$(($SPARE_SPACE - $MT_PARTITION_SIZE))

	if [ $DIFF_SPACE -gt $MAX_DIFF_SPACE ]; then
	
		# While true; used for convenient break syntax
		while true; do

			dialog --infobox "Please wait some seconds, resizing MULTITOOL partition..." 5 60 <> /dev/tty1 >&0 2>&1
			
			# Resize the partition up to the end of the device size
			parted "$BOOT_DEVICE" resizepart $MT_PARTITION_NUMBER 100%
			
			ERR=$?

			if [ $ERR -ne 0 ]; then
				dialog --msgbox "Could not resize MULTITOOL partition, parted error $ERR" 7 60 <> /dev/tty1 >&0 2>&1
				break
			fi

			# Resize the NTFS filesystem inside the partition
			echo "y" | ntfsresize -f -P -s $(($SPARE_SPACE - $THRES_DIFF_SPACE))k -P "${DEVICE_MT_PARTITION}" >/dev/null

			ERR=$?

			if [ $ERR -ne 0 ]; then
				dialog --msgbox "Could not resize MULTITOOL partition, ntfsresize error $ERR" 7 60 <> /dev/tty1 >&0 2>&1
				break
			fi
			
			# Fix the dirty NTFS bits after resize operation
			ntfsfix -d "${DEVICE_MT_PARTITION}" >/dev/null
			
			ERR=$?

			if [ $ERR -ne 0 ]; then
				dialog --msgbox "Could not fix ntfs partition after ntfsfix, error $ERR" 7 60 <> /dev/tty1 >&0 2>&1
				break
			fi
			
			break
			
		done

	fi

fi

# Mount the MULTITOOL partition to write dmesg log on it,
# then immediately unmounts it
mount "$DEVICE_MT_PARTITION" "/mnt"
if [[ $? -eq 0 ]]; then
	dmesg > /mnt/dmesg.multitool.log
	umount "/mnt"
fi

if [[ -f /usr/local/bin/multitool.sh ]]; then
	while true; do
		setsid /bin/bash /usr/local/bin/multitool.sh <> /dev/tty1 >&0 2>&1	
	done
fi

# Use tty1 as default console 
TTY_SERIAL="tty1"

# If kernel uses ttyFIQ0 as console, tell getty to use the same device
grep -q 'console=ttyFIQ0' /proc/cmdline && TTY_SERIAL="ttyFIQ0"

while true; do
	setsid getty --autologin root $TTY_SERIAL
done