#!/bin/sh
# Mount file  ("Ext2", "Ext3", "Ext4", "SquashFS", "ISO")
#VERSION 4.4

if [ -f $(dirname $0)/pfs ] ;  then
    . $(dirname $0)/pfs
else 
    . $(which pfs) || exit 13 
fi    

#help
HLP(){
echo "Usage: $0 <file>"
echo OR
echo "Usage: $0 <file> <mount point>"
exit 1
}

case "$1" in
    "" | -h | --help)  HLP ;;
    *) if ! [ -f $1 ] ; then 
    echo "$1 - file nofound "
    HLP  ; fi ;;
esac

allow_only_root
mntfile="$(realpath "$1")"

if [ "$2" ]; then
  mntpoint="$2"
else
  mntpoint=/mnt/$(echo "${mntfile}" | tr ' ' '_' | sed "s#^\.##g" | sed "s#/#+#g")
  [ -d "${mntpoint}" ] && rmdir "${mntpoint}" 2>/dev/null
  if [ -d "${mntpoint}" ]; then
    echo "$(basename "$0"): File \"${mntfile}\" is mounted!" >&2; exit 1
  else
    mkdir -p "${mntpoint}"
  fi
fi

fstype=$(fs_type ${mntfile})
if [ "${fstype}" = "" ]; then
  echo "Filesystem not supported!" >&2; exit 1
fi
sync

`which busybox` mount -t ${fstype} -o loop "${mntfile}" "${mntpoint}"
exit $?
