#!/bin/sh
##############  ROX app.: Click image files to mount & unmount.	
# Originally by: Terry Becker	aka: SunBurnt
# Rewritten by jamesbond 2012, 2014, 2016 for Fatdog64 600
# Supported filetypes: iso, sfs, 2fs/ext2, 3fs/ext3, 4fs/ext4, img, initrd/initrd.gz
# parameter: $1 - filename (must be absolute for initrd)

### configuration
MKINITRD_NAME=repack-initrd

### helper - create a script to re-pack initrd
# create a simple script to rebuild initrd
# $1-location of original initrd, $2-compression type
create_mkinitrd() {
	local COMPRESSOR 
	touch $MKINITRD_NAME; chmod +x $MKINITRD_NAME
	case $2 in
		gzip) COMPRESSOR="gzip -9" ;;
		bzip2) COMPRESSOR="bzip2" ;;
		xz) COMPRESSOR="xz --check=crc32 --lzma2=dict=512KiB" ;;
		none) COMPRESSOR=cat;;
	esac
	cat >> $MKINITRD_NAME << EOF
#!/bin/sh
[ \$(id -u) -ne 0 ] && exec gtksu "Rebuild initrd" \$(readlink -f "\$0")
SOURCE="\$(dirname "\$0")"; cd "\$SOURCE"
exec Xdialog --no-buttons --title "$MKINITRD_NAME" --infobox "Working, please wait ..." 0 0 1000000 &
XPID=\$!
! [ -e kernel-modules.sfs ] && cp ./usr/share/blank.sfs kernel-modules.sfs
! [ -e kernel-modules.sfs ] && cp /usr/share/blank.sfs kernel-modules.sfs
find . | grep -v $MKINITRD_NAME | cpio -o -H newc | $COMPRESSOR > "$1"
kill \$XPID
su \$USER -c "rox -D \"\$SOURCE\""; rm -rf "\$SOURCE"
exec Xdialog --title "$MKINITRD_NAME" --infobox "Repacking done, $1 is now updated." 0 0 10000
EOF
}

# $1-mountpoint
remove_mountpoint() {
	# workaround a nasty aufs bug
	# only remove mountpoint if there is exactly one aufs root
	# otherwise aufs will *hard-lockup*
	if [ $(ls -d /sys/fs/aufs/* | wc -l) -eq 1 ]; then
		rmdir "$1"
	fi
	return 0 # always return success
}

############### main ##################
[ $(id -u) -ne 0 ] && exec gtksu "File Mounter" "$0" $(readlink -f "$1")
fullpath=$(readlink -f "$1")
filename=${1##*/}
extension=${filename##*.}
[ "$extension" = "$filename" ] && extension=
[ "$filename" = "initrd" -o "$filename" = "initrd.gz" ] && extension=$filename
extension=$(echo $extension | tr "A-Z" "a-z")
mountpoint=$(echo $fullpath | tr "'\\\\/ .;:?*[]{}()<>&|\"\`\t" +)
#echo $filename $extension $mountpoint

# mount only if it is not already mounted
if ! grep $mountpoint /proc/mounts; then  #&& ! busybox losetup -a | grep $fullpath; then
	case $extension in
		iso|sfs|2fs|3fs|4fs|ext2|ext3|ext4|img)
			mkdir -p /mnt/$mountpoint 2> /dev/null
			if mount -o loop "$1" /mnt/$mountpoint; then
				su $USER -c "rox /mnt/$mountpoint"
			else
				Xdialog --title "Error!" --infobox "Failed to mount $filename." 0 0 2000 
			fi
			;;
		initrd|initrd.gz)
			tmpdir=$(mktemp -dp /tmp initrd-XXXXXX)
			chgrp $USER $tmpdir
			chmod a+rwx $tmpdir
			cd $tmpdir
			if case $(file -b --mime-type "$1") in
				*gzip) TYPE="gzip" && zcat "$1" | cpio -i ;;
				*xz) TYPE="xz" && xzcat "$1" | cpio -i ;;
				*bzip2) TYPE="bzip2" && bzcat "$1" | cpio -i ;;
				*) TYPE="none" && cat "$1" | cpio -i ;;
			esac; then
				create_mkinitrd "$1" $TYPE
				su $USER -c "rox $tmpdir"
				Xdialog --left --title "Success" --infobox "$filename is an archive, and it has been extracted to $tmpdir.\n
Do what you want to do with it.\n
When done, you can rebuild $filename by running $MKINITRD_NAME.\n
If you decide otherwise, simply remove $tmpdir." 0 0 10000 
			else
				Xdialog --title "Error!" --infobox "Failed to extract $1." 0 0 2000 
			fi
			;;
		*)
			Xdialog --title "Error!" --infobox "Unsupported file type: $extension" 0 0 2000 
			;;
	esac
else
	#if ! grep $mountpoint /proc/mounts && busybox losetup -a | grep $fullpath; then
	#	load_sfs.sh	# in losetup but not in /proc/mounts, probably mounted by SFS loader
	#	exit
	#fi
	
	# already mounted, will try to unmount
	case $extension in
		iso|sfs|2fs|3fs|4fs|ext2|ext3|ext4|img)
			su $USER -c "rox -D /mnt/$mountpoint"
			if umount -d /mnt/$mountpoint && remove_mountpoint /mnt/$mountpoint; then
				Xdialog --title "Success!" --infobox "$filename has been un-mounted." 0 0 2000 
			else
				if Xdialog --title "Error!" --yesno "Failed to un-mount $filename, it is currently used by the following process:
$(fuser -m /mnt/$mountpoint | xargs ps -o cmd -p | sed 's/CMD//')\n\n
Do you want me to stop them and try again?" 0 0; then
					fuser -m -k /mnt/$mountpoint
					$0 $1
				fi
			fi
			;;
		*)
			Xdialog --title "Error!" --infobox "Unsupported file type: $extension" 0 0 2000
			;;
	esac
fi
