#!/bin/bash

MOUNTPOINTS=""
# Suche nach etx4/ntfs Removable devices
sync
if [ -d /mnt/xlinuxautomount ]; then
  while read f; do
    umount /mnt/xlinuxautomount/$f
  done < <(ls /mnt/xlinuxautomount/)
  rm -r /mnt/xlinuxautomount
fi
while read A1 A2 ASUBSYS SIZE DEV FSTYPE CMOUNT; do
  if [ "x$CMOUNT" == "x" -a "x$A1" == "x0" -a "x$A2" == "x0" ]; then
    mountopts=""
    if [[ "x$ASUBSYSx" != *":usb:"* ]]; then
      if [ "x$FSTYPE" == "xntfs" ]; then mountopts="-t ntfs -o umask=000,x-gvfs-show " ; fi
      if [ "x$FSTYPE" == "xext4" ]; then mountopts="-t ext4 -o users,defaults,x-gvfs-show,exec" ; fi
      if [ "x$FSTYPE" == "xvfat" ]; then mountopts="-t vfat -o umask=000,x-gvfs-show " ; fi
      if [ "x$FSTYPE" == "xexfat" ]; then mountopts="-t exfat -o umask=000,x-gvfs-show " ; fi
      if [ "x$mountopts" != "x" ]; then
        mkdir -p "/mnt/xlinuxautomount/$DEV"
        chmod 777 "/mnt/xlinuxautomount/$DEV"
        echo "mount $mountopts /dev/$DEV '/mnt/xlinuxautomount/$DEV'"
        mount $mountopts /dev/$DEV "/mnt/xlinuxautomount/$DEV"
      fi
    fi
  fi
done <<< $(lsblk -r --output RM,HOTPLUG,SUBSYSTEMS,SIZE,NAME,FSTYPE,MOUNTPOINT | grep ^0 )

