#!/bin/sh
#
# Manage samba shares
#	- after mounting: add the new samba share
#	- before umounting: disable and remove the samba share
#
# The following line _must_ be added to your /etc/samba/smb.conf:
#	include = /var/cache/cryptobox-server/settings/misc/samba-include.conf
# and you should create this file and chown it to the cryptobox user:
#	touch /var/cache/cryptobox-server/settings/misc/samba-include.conf
#
#
# Params:	$event $device $volume_name $volume_type $mount_dir
#
#	event:	premount | postmount | preumount | postumount
#	device:	name of the device
#	volume_name:	name of the volume
#	volume_type:	plain | luks
#	mount_dir:	mount directory
#

set -eu

# --------------=-=-=- some settings -=-=-=----------------
# adapt this part of the file to your needs

SAMBA_CONTROL=smbcontrol
SAMBA_CONF_DIR=/var/cache/cryptobox-server/settings/misc/samba.conf.d
MAIN_SAMBA_CONF_FILE=/var/cache/cryptobox-server/settings/misc/samba-include.conf

# this smb.conf snippet will get used for every published share
# _VOLUME_NAME and _SHARE_DIR_ are replaced by their actual values
SAMBA_SHARE_TEMPLATE=$(cat - <<-"EOF"
	[_VOLUME_NAME_]
		comment	= CryptoBox share
		path	= _SHARE_DIR_
		read only	= no
		guest ok	= yes
EOF
)

# -----=-=-=- check environment and parameters -=-=-=------

# check if samba is installed
if which "$SAMBA_CONTROL"
  then	true
  else	echo "samba not installed ('$SAMBA_CONTROL' not found)" >&2
	exit 1
 fi

# create include-file directory
mkdir -p "$SAMBA_CONF_DIR"

# create the main include file
touch "$MAIN_SAMBA_CONF_FILE"

# check event argument
if test "$#" -eq 0
  then	echo "Syntax: $(basename $0) EVENT [EVENT_INFORMATION]" >&2
	exit 1
 fi

event=$1

# ------------=-=-=- some functions -=-=-=-----------------

update_include_conf_file()
{
	(	echo "# this file was automatically generated by the CryptoBox"
		echo "# DO NOT EDIT - all changes will get lost!"
		find "$SAMBA_CONF_DIR" -type f -name "*.conf" | while read fname
		  do	mdir=$(grep "path.*=" "$fname" | cut -f 2 -d "=" | sed 's/^[ \t]*//')
				# check if the mount directory still exists
		  		if test -d "$mdir"
		  		  then	echo "include = $fname"
				  else	rm "$fname"
				 fi
		 done ) >"$MAIN_SAMBA_CONF_FILE"
}

send_reload_command()
{
	# reload config files
	"$SAMBA_CONTROL" smbd reload-config
}

empty_conf_dir()
{
	find "$SAMBA_CONF_DIR" -type f -name "*.conf" -print0 | xargs -0 rm
}

send_close_share_command()
{
	# close all connections and deny further requests
	"$SAMBA_CONTROL" smbd close-share "$1"
}

# -----------------=-=-=- main -=-=-=----------------------

case "$event" in
	premount|postumount )
		;;
	postmount )
		vol_name=$3
		mount_dir=$5
		echo "$SAMBA_SHARE_TEMPLATE" | sed "s#_SHARE_DIR_#$mount_dir#g; s#_VOLUME_NAME_#$vol_name#g" >"$SAMBA_CONF_DIR/${vol_name}.conf"
		update_include_conf_file
		send_reload_command
		;;
	preumount )
		vol_name=$3
		send_close_share_command "$vol_name"
		rm "$SAMBA_CONF_DIR/${vol_name}.conf" || true
		update_include_conf_file
		send_reload_command
		;;
	shutdown|bootup )
		empty_conf_dir
		;;
	* )
		# ignore all unsupported events
		exit 0
		;;
  esac

exit 0

