#!/bin/sh -e

case $(auto-ostype) in
FreeBSD)
    auto-fusefs-install

    # Check for native file managers known to have a media eject feature
    # If none found, use pcmanfm
    for fm in caja thunar; do
	if [ -e /usr/local/bin/$fm ]; then
	    chosen_fm=$fm
	    break
	fi
    done
    if [ 0$chosen_fm = 0 ]; then
	pkg install -y pcmanfm
	chosen_fm=/usr/local/bin/pcmanfm
    fi
    pkg install -y automount
    cat << EOM > /usr/local/etc/automount.conf
FM=$chosen_fm
MNT_GROUP=operator
MNT_MODE=775
EOM
    service devd restart
    cat << EOM

Presently is is necessary to make /sbin/umount SUID root to allow ordinary
users to unmount.  This, however, allows any user to unmount any filesystem,
which is a security risk.  This should only be used on personal systems,
never on a multiuser system.

This approach is also fragile as freebsd-update could revert permissions
on /sbin/umount.  If this happens, you will need to run

    chmod 4555 /sbin/umount

A more secure and reliable solution is under development and will be
incorporated into this setup when available.

EOM
    printf "Allow all users to unmount? y/[n] "
    read suid
    if [ 0$suid = 0y ]; then
	chmod 4555 /sbin/umount
    fi
    ;;

*)
    auto-unsupported-os $0
    exit 1
    ;;

esac
