#!/bin/sh
#
# $1 = device (sda1, sda2, sdb1 ...)

model=`cat /etc/model`

setcat=`grep config.plugins.automount /etc/enigma2/settings`
dev=`echo -e $setcat | grep config.plugins.automount.dev | cut -d"=" -f2`
usbdir=`echo -e $setcat | grep config.plugins.automount.usbdir | cut -d"=" -f2`
ntfsrw=`echo -e $setcat | grep config.plugins.automount.ntfsrw | cut -d"=" -f2`

if [ "$dev" = "" ]; then dev="label"; fi
if [ "$usbdir" = "" ]; then usbdir="/media/autofs"; fi
if [ "$ntfsrw" = "" ]; then ntfsrw="disable"; fi


if [ "$1" == "" ]; then
	devlist=`cat /proc/diskstats | cut -c 11- | cut -d " " -f1 | grep "sd[a-z][0-9]"`
else
	devlist=$1
fi

blkid=`blkid -w /dev/null -c /dev/null`

for device in $devlist; do
	if grep -q "^/dev/$device " /proc/mounts; then
		echo "[AUTOMOUNT] [MOUNT] /dev/$device allready mounted"
		continue
	fi
	label=`echo -e "$blkid" | grep "^/dev/$device:" | grep LABEL= | sed 's/^.*LABEL="//;s/".*$//' | tr -d " " | tr -d "-"`
	type=`echo -e "$blkid" | grep "^/dev/$device:" | grep TYPE= | sed 's/^.*TYPE="//;s/".*$//'`
	
	if [ "$label" == "SWAPPART" ]; then
		if [ ! -e /tmp/.swappart ]; then
			echo $swap_max_sectors > /sys/block/`echo $device | cut -c 1-3`/device/max_sectors > /dev/null 2>&1
			echo "[AUTOMOUNT] [MOUNT] mount /dev/$device as SWAPPART (label=$label, type=$type)"
			echo "$device#$type" > /tmp/.swappart
		fi
		continue
	fi
	if [ "$type" == "" ]; then echo "[AUTOMOUNT] [MOUNT] /dev/$device unknown fstype"; continue; fi
	
	if [ "$type" == "ext2" ] || [ "$type" == "ext3" ]; then opt="-o rw,errors=continue,noatime,nodiratime"; fi
	if [ "$type" == "fat" ] || [ "$type" == "vfat" ] || [ "$type" == "msdos" ]; then opt="-o rw"; fi
	if [ "$type" == "ntfs" ] && [ "$ntfsrw" == "ntfs-3g" ]; then opt="-o allow_other,use_ino,kernel_cache,nonempty,rw"; fi
	if [ "$type" == "ntfs" ] && [ "$ntfsrw" == "ntfsmount" ]; then opt="-o force,allow_other,use_ino,kernel_cache,nonempty,rw"; fi
			
	if [ "$label" == "SWAP" ] && [ ! -e /tmp/.swapdev ]; then
		echo $swap_max_sectors > /sys/block/`echo $device | cut -c 1-3`/device/max_sectors > /dev/null 2>&1
		if [ "$type" == "ntfs" ] && [ "$ntfsrw" == "ntfsmount" ]; then
			type="$ntfsrw"
			echo "[AUTOMOUNT] [MOUNT] mount /dev/$device on /var/swap (label=$label, type=$type)"
			ntfsmount "/dev/$device" /var/swap $opt
		elif [ "$type" == "ntfs" ] && [ "$ntfsrw" == "ntfs-3g" ]; then
			type="$ntfsrw"
			echo "[AUTOMOUNT] [MOUNT] mount /dev/$device on /var/swap (label=$label, type=$type)"
			ntfs-3g "/dev/$device" /var/swap $opt
		else
			echo "[AUTOMOUNT] [MOUNT] mount /dev/$device on /var/swap (label=$label, type=$type)"
			mount -t "$type" $opt "/dev/$device" /var/swap
		fi
		if [ $? -eq 0 ]; then 
			echo "$device" > /tmp/.swapdev
			if [ -e /var/swap/automount/automounts.xml ]; then
				cp -a /var/swap/automount/automounts.xml /var/etc/automount/automounts.xml
			else
				if [ -e /var/etc/automount/automounts.xml ]; then
					cp -a /var/etc/automount/automounts.xml /var/swap/automount/automounts.xml
				fi
			fi
			if [ -e /var/swap/saver ]; then
				mount --bind /var/swap/saver /usr/lib/enigma2/python/Plugins/Extensions/MediaCenter/saver
			fi
			if [ -e /var/swap/ba ]; then
				mount --bind /var/swap/ba /var/baroot
			fi
			if [ "$1" != "" ]; then
				swap "showInfoDirect"
			fi
		fi
	elif [ "$label" == "RECORD" ] && [ ! -e /tmp/.recorddev ] && [ ! -d /usr/lib/enigma2/python/AAF ]; then
		echo $record_max_sectors > /sys/block/`echo $device | cut -c 1-3`/device/max_sectors > /dev/null 2>&1
		if [ -L /media/hdd ] && [ -z "$1" ]; then
			rm -f /media/hdd
			mkdir -p /media/hdd
		fi
		if [ "$type" == "ntfs" ] && [ "$ntfsrw" == "ntfsmount" ]; then
			type="$ntfsrw"
			echo "[AUTOMOUNT] [MOUNT] mount /dev/$device on /media/hdd (label=$label, type=$type)"
			ntfsmount "/dev/$device" /media/hdd $opt
		elif [ "$type" == "ntfs" ] && [ "$ntfsrw" == "ntfs-3g" ]; then
			type="$ntfsrw"
			echo "[AUTOMOUNT] [MOUNT] mount /dev/$device on /media/hdd (label=$label, type=$type)"
			ntfs-3g "/dev/$device" /media/hdd $opt
		else
			echo "[AUTOMOUNT] [MOUNT] mount /dev/$device on /media/hdd (label=$label, type=$type)"
			mount -t "$type" $opt "/dev/$device" /media/hdd 
		fi
		if [ $? -eq 0 ]; then
			echo "$device" > /tmp/.recorddev
			retstr="OK";
			if [ ! -d /media/hdd/movie ]; then 
				rm -f /media/hdd/movie
				mkdir /media/hdd/movie
			fi
			if [ "$1" != "" ]; then 
				(sleep 20;vfdctl +hdd)&
			fi
		else
			retstr="ERR";
		fi
		if [ "$1" == "" ]; then
			echo "RECORDMOUNT: $retstr" >> /tmp/.infobox
		else
			infobox 3 Info "RECORDMOUNT: $retstr" &
		fi
		echo "RECORDMOUNT: $retstr" > /dev/vfd
		if [ "$model" = "ufs922" ]; then
			rm -f /var/badevice
			mkdir /var/badevice
			mount -t "$type" $opt "/dev/$device" /var/badevice
			if  [ ! -d /var/badevice/baroot ]; then
				rm -f /var/badevice/baroot
				mkdir /var/badevice/baroot
			fi
# for ba use other methode dont use a tmp mount change to var or other
			mount --bind /var/badevice/baroot /var/baroot
			mkdir -p /media/hdd/stick
			mount --bind /media/hdd/stick /var/swap
		fi
	elif [ "$label" == "XE2BA" ] && [ `cut -d" " -f2 /proc/mounts | grep -x "/var/baroot" | wc -l` -eq 0 ]  && [ ! -d /usr/lib/enigma2/python/AAF ]; then
		echo "[AUTOMOUNT] [MOUNT] mount /dev/$device on /var/baroot (label=$label, type=$type)"
		mount -t "$type" $opt "/dev/$device" /var/baroot
# for ba use other methode dont use a tmp mount change to var or other
		if [ "$model" = "ufs922" ]; then
			rm -f /var/badevice
			mkdir /var/badevice
			mount -t "$type" $opt "/dev/$device" /var/badevice
			if  [ ! -d /var/badevice/baroot ]; then
				rm -f /var/badevice/baroot
				mkdir /var/badevice/baroot
			fi
			mount --bind /var/badevice/baroot /var/baroot
		fi
	elif [ "$label" == "XBA" ] && [ `cut -d" " -f2 /proc/mounts | grep -x "/var/baroot" | wc -l` -eq 0 ]  && [ ! -d /usr/lib/enigma2/python/AAF ]; then
		echo "[AUTOMOUNT] [MOUNT] mount /dev/$device on /var/baroot (label=$label, type=$type)"
		mount -t "$type" $opt "/dev/$device" /var/baroot
	elif [ ! -d /usr/lib/enigma2/python/AAF ]; then
		if [ "$dev" == "label" ]; then
			if [ "$label" == "" ]; then label="NONLABEL"; fi
			labelcount=`cut -d" " -f2 /proc/mounts | grep -w "$usbdir/$label" | wc -l`
			if [ $labelcount -gt 0 ]; then label="$label-$labelcount"; fi
			if [ ! -d "$usbdir/$label" ]; then
				rm -f "$usbdir/$label"
				mkdir -p "$usbdir/$label"
			fi
			if [ "$type" == "ntfs" ] && [ "$ntfsrw" == "ntfsmount" ]; then
				type="$ntfsrw"
				echo "[AUTOMOUNT] [MOUNT] mount /dev/$device on $usbdir/$label (label=$label, type=$type)"
				ntfsmount "/dev/$device" "$usbdir/$label" $opt &
			elif [ "$type" == "ntfs" ] && [ "$ntfsrw" == "ntfs-3g" ]; then
				type="$ntfsrw"
				echo "[AUTOMOUNT] [MOUNT] mount /dev/$device on "$usbdir/$label" (label=$label, type=$type)"
				ntfs-3g "/dev/$device" "$usbdir/$label" $opt &
			else
				echo "[AUTOMOUNT] [MOUNT] mount /dev/$device on $usbdir/$label (label=$label, type=$type)"
				mount -t "$type" $opt "/dev/$device" "$usbdir/$label" &
			fi
		else
			if [ ! -d "$usbdir/$device" ]; then
				rm -f "$usbdir/$device"
				mkdir -p "$usbdir/$device"
			fi
			if [ "$type" == "ntfs" ] && [ "$ntfsrw" == "ntfsmount" ]; then
				type="$ntfsrw"
				echo "[AUTOMOUNT] [MOUNT] mount /dev/$device on $usbdir/$device (label=$label, type=$type)"
				ntfsmount "/dev/$device" "$usbdir/$device" $opt &
			elif [ "$type" == "ntfs" ] && [ "$ntfsrw" == "ntfs-3g" ]; then
				type="$ntfsrw"
				echo "[AUTOMOUNT] [MOUNT] mount /dev/$device on "$usbdir/$device" (label=$label, type=$type)"
				ntfs-3g "/dev/$device" "$usbdir/$device" $opt &
			else
				echo "[AUTOMOUNT] [MOUNT] mount /dev/$device on $usbdir/$device (label=$label, type=$type)"
				mount -t "$type" $opt "/dev/$device" "$usbdir/$device" &
			fi
		fi
# for ba use other methode dont use a tmp mount change to var or other
		if [ "$model" = "ufs922" ] && [ "$device" = "sda5" ]; then
			rm -f /var/badevice
			mkdir /var/badevice
			mount -t "$type" $opt "/dev/$device" /var/badevice
			if  [ ! -d /var/badevice/baroot ]; then
				rm -f /var/badevice/baroot
				mkdir /var/badevice/baroot
			fi
			mount --bind /var/badevice/baroot /var/baroot
		fi
	fi
done
