﻿#!/bin/sh

# write a log, in case sth goes wrong
FFP_LOG=/mnt/HD_a2/ffp.log
exec >>$FFP_LOG 2>&1

# Some Variables
USB_MOUNTP=/mnt/HD_a2/usbstorage
USB_DEV=sdc1
FFP_HD=/mnt/HD_a2/ffp
FFP_USB=$USB_MOUNTP/ffp
USBMODDIR=/mnt/HD_a2/.bootstrap
USBMOD=$USBMODDIR/usb-storage.ko

# Path to ffp on Disk
FFP_PATH=$FFP_HD

# prefix used to compile ffp packages
FFP_PREFIX=/ffp

# rc file path
FFP_RC=/ffp/etc/rc

echo "**** ffp 0.5 by fonz ****"
date

# Initialize variable
usbmod_loaded=no

# if the usb-module.ko does not exist, try to download it
if [ ! -e $USBMOD ]; then
	echo "Directory of usb-storage module not found. Will try to create directory for future actions."
	mkdir $USBMODDIR
	echo "usb-storage.ko module not found. Will try to to download it."
	wget -c http://wolf-u.li/u/10 -O $USBMOD
fi

# Load & Copy usb-storage module
if [ -e $USBMOD ]; then
	echo "Found usb-storage.ko module."
	cp $USBMOD /lib/modules/
	#load usb-storage module
	insmod /lib/modules/usb-storage.ko
	echo "insmod usb-storage.ko"
	sleep 15
	usbmod_loaded=yes
else
	echo "usb-storage.ko module still not found. fun_plug won't run. Please contact wolf-u.li with this error"
fi

# Find the USB Device
if test $usbmod_loaded = yes; then
	usb_found=no
	grep $USB_DEV /proc/partitions >/dev/null 2>/dev/null
	if [ $? -ne 0 ]; then
		# wait for disk...
		let timeout=30
		echo "Waiting for $USB_DEV (up to $timeout seconds) ..."
		while [ $timeout -gt 0 ]; do
			grep $USB_DEV /proc/partitions >/dev/null 2>/dev/null
			[ $? -eq 0 ] && break
			sleep 3
			let timeout=${timeout}-5
		done
		grep $USB_DEV /proc/partitions >/dev/null 2>/dev/null
		if [ $? -ne 0 ]; then
			echo "Failed. Did not find $USB_DEV in /proc/partitions."
			usb_found=no
		else
			echo "Found $USB_DEV in /proc/partitions"
			usb_found=yes
		fi
	else
		usb_found=yes
	fi

	# Mount the USB-Device
	if test $usb_found = yes; then
		mkdir -p $USB_MOUNTP
		echo -n "Mounting /dev/$USB_DEV on $USB_MOUNTP ..."
		mount /dev/$USB_DEV $USB_MOUNTP -o noatime
		if [ $? -ne 0 ]; then
			echo "Failed"
		else
			echo "Success"
		fi
	fi
else
	echo "usb-storage.ko module not loaded."
fi

# Check if Directory of ffp exists on the USB-Device and check if it is already installed (fun_plug.tgz doesn't exist, if it's installed).
if [ -d $FFP_USB ] || [ -r $USB_MOUNTP/fun_plug.tgz ]; then
	echo "* Found FFP on USB device"
	FFP_PATH=$FFP_USB
	FFP_TARBALL=$USB_MOUNTP/fun_plug.tgz
	# Move logs till now to USB-Stick, Relink logging
	mv $FFP_LOG $FFP_USB/
	FFP_LOG=$FFP_USB/ffp.log
	exec >>$FFP_LOG 2>&1
else
	echo "* FFP was not found on a USB device. Will try to download it."
	# Get the present Link for ffp 0.5
	# Remove a old fun_plug.tgz to ensure the newest version
	rm $USB_MOUNTP/fun_plug.tgz
	# Download the Link
	wget http://www.inreto.de/dns323/fun-plug/0.5/fun_plug -O $USB_MOUNTP/fun_plug.tgz
	# Check if it now exists
	if [ -r $USB_MOUNTP/fun_plug.tgz ]; then
		echo "* FFP was downloaded to the USB device."
		FFP_PATH=$FFP_USB
		FFP_TARBALL=$USB_MOUNTP/fun_plug.tgz
	else
		echo "Probably no Internet Connection or any other Problem, reverting back to HDD...."
		FFP_PATH=$FFP_HD
		FFP_TARBALL=$FFP_PATH/../fun_plug.tgz
	fi
fi

# create /ffp link
echo "ln -snf $FFP_PATH /ffp"
ln -snf $FFP_PATH /ffp

# install tarball
if [ -r $FFP_TARBALL ]; then
    echo "* Installing $FFP_TARBALL on $FFP_PATH"
    mkdir -p $FFP_PATH && tar xzf $FFP_TARBALL -C $FFP_PATH && /ffp/bin/tar xzf $FFP_TARBALL -C $FFP_PATH
    if [ $? -eq 0 ]; then
        echo "* OK"
    fi
    rm $FFP_TARBALL
fi

# suid busybox
if [ -x /ffp/bin/busybox ]; then
    chown root.root /ffp/bin/busybox
    chmod 0755 /ffp/bin/busybox
    chmod u+s /ffp/bin/busybox
fi

# run fun_plug.init, if present
if [ -x /ffp/etc/fun_plug.init ]; then
    echo "* Running /ffp/etc/fun_plug.init ..."
    /ffp/etc/fun_plug.init
fi

# run fun_plug.local, if present
if [ -x /ffp/etc/fun_plug.local ]; then
    echo "* Running /ffp/etc/fun_plug.local ..."
    /ffp/etc/fun_plug.local
fi

# run commands
if [ -x $FFP_RC ]; then
    echo "* Running $FFP_RC ..."
    $FFP_RC
    echo "*  OK"
else
    echo "$FFP_RC: Not found or not executable"
fi
