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

# fixup - correct any mistakes from previous versions
#
# To the extent possible under law, T. Joseph Carter and Ivan Drucker have
# waived all copyright and related or neighboring rights to the a2server
# scripts themselves.  Software used or installed by these scripts is subject
# to other licenses.  This work is published from the United States.

# The idea behind this script is to prevent other scripts from becoming a
# minefield of ancient and deprecated code designed to compensate for other
# even more ancient and replaced code that is no longer needed, no longer
# useful, and honestly complicates maintenance of that code.  Everything here
# will explain what it does, when it was put here, and it should remain clear
# when the legacy code may be removed.
#
# The use of functions which get immediately called is to try and prevent the
# long spidery conditional blocks a2cloud was originally known for.

# # Added 2000-00-00
# fix_some_stupid_mistake()
# {
# 	# Needed until: Upgrades from 0.0.0 no longer supported
# 	:
# }
# fix_some_stupid_mistake


# Added 2018-09-07
move_media_a2shared()
{
# 	# Needed until: Upgrades from versions < 1.3.0 are not supported
	local netatalk_restart
	if [[ -d /media/A2SHARED ]]; then
		cat <<-EOF
			a2server: As of version 1.3.0, the standard location for
			          netatalk to store Apple II files is /srv/A2SERVER.
			          We will move /media/A2SHARED to the new location.
			          If you need to do that yourself (you'll know if
			          you do), press ctrl-c now.  Otherwise press Return.
		EOF
		read

		# netatalk doesn't much like its sharepoint just disappearing
		if service netatalk status >/dev/null; then
			netatalk_restart=1
			sudo service netatalk stop
		fi

		# Debated: Create /media/A2SHARED symlink? Nah, bad idea.
		sudo mv /media/A2SHARED /srv/A2SERVER
		sudo sed -i 's|/media/A2SHARED|/srv/A2SERVER|g' /usr/local/etc/netatalk/AppleVolumes.default
		if [[ $netatalk_restart ]]; then
			sudo service netatalk start
		fi

		# Samba can just be reloaded after modifying the config
		if [[ -f /etc/samba/smbd.conf ]]; then
			sudo sed -i 's|/media/A2SHARED|/srv/A2SERVER|g' /etc/samba/smbd.conf
			if service smbd status >/dev/null; then
				sudo service smbd reload
			elif service samba status >/dev/null; then
				sudo service samba reload
			fi
		fi
	fi
}
move_media_a2shared
