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

# vsd - a2cloud script to change ADTPro virtual disk image symlinks
#
# To the extent possible under law, T. Joseph Carter and Ivan Drucker have
# waived all copyright and related or neighboring rights to the a2cloud
# scripts themselves.  Software used or installed by these scripts is subject
# to other licenses.  This work is published from the United States.

skipWarning=
drive=
if [[ $1 == "-1" || $1 == "-d1" ]]; then
	shift
elif [[ $1 = "-2" || $1 == "-d2" ]]; then
	drive=2
	shift
fi
if [[ ! $1 ]]; then
	echo "virtual drive $(( drive ? 2 : 1 )): $(readlink /usr/local/adtpro/disks/Virtual${drive}.po)"
else
	if [[ $1 == "-f" ]]; then
		shift
		skipWarning=1
	fi
	if [[ ! -f $1 ]]; then
		echo "Image file '$1' was not found."
	elif [[ -f /usr/local/adtpro/disks/Virtual${drive}.po && ! -L /usr/local/adtpro/disks/Virtual${drive}.po ]]; then
		echo "/usr/local/adtpro/disks/Virtual${drive}.po is an actual disk image"
		echo "file, not a symbolic link. Please move or rename it, and try again."
	else
		rm /usr/local/adtpro/disks/Virtual${drive}.po &>/dev/null;
		[[ ${1:0:1} != "/" ]] && pwd="$PWD/";
		ln -s "$pwd$1" /usr/local/adtpro/disks/Virtual${drive}.po
		[[ $drive ]] && VSD2="$pwd$1" || VSD1="$pwd$1"
		if [[ $(ps aux | grep [A]DTPro) ]]; then
			if [[ ! $skipWarning ]]; then
				echo "Please make sure you're not writing to either virtual drive on your Apple II."
				echo -n "  Press return when ready, or control-C to cancel..."
				read
			fi
			sudo pkill -f [A]DTPro
			while [[ $(ps aux | grep [A]DTPro) ]]; do sleep 1; done
			/usr/local/bin/adtpro-start
		fi
	fi
fi

unset drive
unset pwd
