#!/bin/bash
command="${1}"

# test if gsettings tool is installed
if ! (which gsettings 1>/dev/null) ; then
  echo "need gsettings"
  exit 1
fi

#test for existing mate settings
if (gsettings get org.mate.media-handling automount 1>/dev/null 2>/dev/null) ; then
  mate=1
fi

#test for existing gnome settings
if (gsettings get org.gnome.desktop.media-handling automount 1>/dev/null 2>/dev/null) ; then
  gnome=1
fi

#test if at least one of the known settings where found
if [ -z $mate ] && [ -z $gnome ] ; then
  echo "No supported Settings found"
  echo "mate :  org.mate.media-handling automount"
  echo "gnome:  org.gnome.desktop.media-handling automount"
  exit 1
fi

case ${command} in
on)
  echo "turning on"
  if [[ $mate -eq "1" ]] ; then
    gsettings set org.mate.media-handling automount true
    gsettings set org.mate.media-handling automount-open true
  fi
  if [[ $gnome -eq "1" ]] ; then
    gsettings set org.gnome.desktop.media-handling automount true
    gsettings set org.gnome.desktop.media-handling automount-open true
  fi
  ;;
off)
  echo "turning off"
  if [[ $mate -eq "1" ]] ; then
    gsettings set org.mate.media-handling automount false
    gsettings set org.mate.media-handling automount-open false
  fi
  if [[ $gnome -eq "1" ]] ; then
    gsettings set org.gnome.desktop.media-handling automount false
    gsettings set org.gnome.desktop.media-handling automount-open false
  fi
  ;;
*)
  echo "Status:"
  if [[ $mate -eq "1" ]] ; then
    echo "#   mate-settings found:"
    echo "    - org.mate.media-handling automount :"
    echo -n "       "
    gsettings get org.mate.media-handling automount
    echo "    - org.mate.media-handling automount-open :"
    echo -n "       "
    gsettings get org.mate.media-handling automount-open
  fi

  if [[ $gnome -eq "1" ]] ; then
    echo "#   gnome-settings found:"
    echo "    - org.gnome.desktop.media-handling automount :"
    echo -n "       "
    gsettings get org.gnome.desktop.media-handling automount
    echo "    - org.gnome.desktop.media-handling automount-open :"
    echo -n "       "
    gsettings get org.gnome.desktop.media-handling automount-open
  fi
  echo "-------------------"
  echo "usage:"
  echo " $0 on  : turn on automount"
  echo " $0 off : turn off automount"
esac
