#! /bin/bash
# vim: set tabstop=4 shiftwidth=4 noexpandtab filetype=sh:

# Set up A2SERVER to support Samba (Windows File Sharing)
# this script can also be used if new shares are introduced

autoAnswerYes=
if [[ -f /tmp/a2server-autoAnswerYes ]]; then
	autoAnswerYes=1
fi

if [[ ! $autoAnswerYes || -f /tmp/a2server-setupWindowsSharing ]]; then

	if [[ ! $autoAnswerYes ]]; then
		echo
		echo -n "Should Windows computers be able to connect to A2SERVER? "
		read
	fi
	if [[ $autoAnswerYes || ${REPLY:0:1} == "Y" || ${REPLY:0:1} == "y" ]]; then

		echo "A2SERVER: Setting up Windows file sharing..."
		sudo true

		sudo update-rc.d samba defaults &> /dev/null
		if [[ ! -f /etc/init.d/samba ]]; then
			installSamba=1
		fi

		if (( $installSamba )); then
			if [[ ! -f /tmp/a2server-packageReposUpdated ]]; then
				# prepare for installing packages
				sudo apt-get -y update
				touch /tmp/a2server-packageReposUpdated
			fi

			# Dependency: samba for Windows/modern Macs
			# FIXME: Figure out when smbpasswd moved and adjust this as necessary
			sudo apt-get -y install samba
			hash smbpasswd &> /dev/null || sudo apt-get -y install samba-common-bin
			sudo apt-get clean
		fi

		sudo /etc/init.d/samba start &> /dev/null

		workgroup=$(grep -o "^       workgroup = .*$" /etc/samba/smb.conf 2> /dev/null | cut -c 16-)
		if [[ -z "$workgroup" ]]; then
			workgroup="WORKGROUP"
		fi
		if [[ ! $autoAnswerYes ]]; then
			echo
			echo "Enter workgroup name (or press return for '${workgroup}'): "
			read
			# FIXME validation?
			if [[ $REPLY ]]; then
				workgroup=$REPLY
			fi
		fi
		sudo sed -i 's/^   workgroup = .*$/   workgroup = '$workgroup'/' /etc/samba/smb.conf 2> /dev/null
		sudo sed -i 's/^#   security = user/   security = user/' /etc/samba/smb.conf 2> /dev/null

		# remove GSFILES if no longer serving it
		if ! grep ^/srv/A2SERVER/GSFILES /usr/local/etc/netatalk/AppleVolumes.default; then
			sudo sed -i ':a;N;$!ba;s#\n\[GSFILES\].*\[#\n[#' /etc/samba/smb.conf
			sudo sed -i ':a;N;$!ba;s#\n\[GSFILES\].*$#\n#' /etc/samba/smb.conf
		fi

		grep ^/srv/A2SERVER /usr/local/etc/netatalk/AppleVolumes.default | cut -d" " -f2 \
			| while read sharename; do
			if [[ $(grep $sharename /etc/samba/smb.conf) ]]; then
				echo "A2SERVER: $sharename is already set up for Windows file sharing."
			else
				sudo tee -a /etc/samba/smb.conf >/dev/null <<EOF
[$sharename]
    path = /srv/A2SERVER/$sharename
    browsable = yes
    guest ok = yes
    read only = no
    create mask = 0666
    force user = $USER
EOF
				echo "A2SERVER: $sharename has been set up for Windows file sharing."
			fi
		done

		echo
		echo "A2SERVER: Setting Windows file sharing password to 'apple2'."
		echo -e 'apple2\napple2' | sudo smbpasswd -s -a $USER

		echo
		echo "A2SERVER: Windows file sharing is now running."
	else
		sudo /etc/init.d/samba stop &> /dev/null
		sudo update-rc.d -f samba remove &> /dev/null
		echo "A2SERVER: Windows file sharing has been turned off."
	fi

	echo
fi
