#!/bin/sh -


_command="${0##*/}"
_copy='0'
_exit_status='0'
if which jbroot 2>>/dev/null 1>>/dev/null; then
	_root_path='/rootfs'
	_jb_root_path="$(jbroot)"
	_jb_root_path="${_jb_root_path%/*}"
else
	_root_path=''
	_jb_root_path="$(readlink -f '/var/jb')"
fi
_p_file="${_jb_root_path}/var/mobile/Library/Preferences/com.nan.auto-bindfs.plist"


_print_help(){
	echo "Usage: ${_command} [OPTIONS...] [PATH...]
	-h		Print this help 
	-l		print folder that will be mounted the next jailbreak
	-s		sutomatically mount the folder on the next jailbreak (empty when empty)
	-u		unmount folder that has been mounted as bindfs
	--copy		force a copy of all files within the original folder, even if they already exist
	--skip-copy	skip the copy check, even if there are no files in the destination

Example:
	${_command} \"/System/Library/Fonts\"
	${_command} \"/System/Library/Fonts\" \"/System/Library/ThermalMonitor\"
	${_command} -l
	${_command} -s \"/System/Library/Fonts\"
	${_command} -s \"/System/Library/Fonts\" \"/System/Library/ThermalMonitor\"
	${_command} -s
	${_command} -u \"/System/Library/Fonts\"
	${_command} -u \"/System/Library/Fonts\" \"/System/Library/ThermalMonitor\"
	${_command} --copy \"/System/Library/Fonts\"
	${_command} --skip-copy \"/System/Library/Fonts\"

Tips:
	\"-s\" will overwrite the configuration file set by the previous \"-s\" every time it is run
	\"--copy\" and \"--skip-copy\" only take effect when manually mounted and do not apply to automatic mountings"
}

_check_mount(){
	if mount | grep -q " on ${1} (bindfs"; then
		echo 1
	else
		echo 0
	fi
}

_copy(){
	if [ ! -d "${_jb_root_path}${1}" ]; then
		mkdir -p "${_jb_root_path}${1}"
		rm -rf "${_jb_root_path}${1}"
		cp -a "${_root_path}${1}" "${_jb_root_path}${1}"
	elif [ "$(ls "${_jb_root_path}${1}" | wc -l)" = '0' ]; then
		rm -rf "${_jb_root_path}${1}"
		cp -a "${_root_path}${1}" "${_jb_root_path}${1}"
	else
		sleep 0
	fi
}

_do(){
	_aaa="$("${_jb_root_path}/usr/libexec/mount_bindfs/jbctl-bindfs" "${1}" "${2}" "${3}" 2>&1)"
	if [ -z "${_aaa}" ]; then
		echo "status: Success"
	else
		echo "status: Failure"
		echo "Log: ${_aaa#*: }"
	fi
}

if [ "${#}" = '0' ]; then
	_print_help
	exit 1
fi
if ! id -u | grep -q '^0$'; then
	sudo "${0}" "$@"
	exit 0
fi
while [ "${#}" -gt '0' ]; do
	case "${1}" in
		-h)
			_print_help
			exit 0
		;;
		--copy)
			_copy='1'
			shift 1
		;;
		--skip-copy)
			_copy='2'
			shift 1
		;;
		/*)
			a='1'
			while [ "${#}" -gt '0' ]; do
				_path="$(echo "${1}" | sed 's#/$##' | sed 's#^/var#/private/var#' | sed 's#^/rootfs##')"
				echo "Will mount folder \"${_jb_root_path}${_path}\" in bindfs format to \"${_path}\""
				if [ ! -e "${_root_path}${_path}" ]; then
					echo "status: Failure"
					echo "Log: No such file or directory"
				else
					if _check_mount "${_path}" | grep -q '1'; then
						case "${_copy}" in
							1)
								echo "status: Failure"
								echo "Log: Directory \"${_path}\" has been mounted"
								"${0}" -u "${_path}"
								"${0}" --copy "${_path}"
								_exit_status="$((_exit_status+$?))"
								;;
							*)
								echo "status: Failure"
								echo "Log: Directory \"${_path}\" has been mounted"
								_exit_status="$((_exit_status+1))"
								;;
						esac
					else
						case "${_copy}" in
							0)
								_copy "${_path}"
								;;
							1)
								mkdir -p "${_jb_root_path}${_path}"
								rm -rf "${_jb_root_path}${_path}"
								cp -a "${_root_path}${_path}" "${_jb_root_path}${_path}"
								;;
							2)
								mkdir -p "${_jb_root_path}${_path}"
								;;
						esac
						_do 'bindfs' "${_path}" "${_jb_root_path}${_path}"
						_exit_status="$((_exit_status+$?))"
					fi
				fi
				unset _path
				shift 1
			done
			if [ "${_exit_status}" = '0' ]; then
				exit 0
			else
				exit 1
			fi
		;;
		-u)
			shift 1
			a='1'
			while [ "${#}" -gt '0' ]; do
				case "${1}" in
					/*)
						_path="$(echo "${1}" | sed 's#/$##')"
						echo "Will umount folder \"${_jb_root_path}${_path}\" on \"${_path}\""
						if _check_mount "${_path}" | grep -q '0'; then
							echo "status: Failure"
							echo "Log: Directory \"${_path}\" has not been mounted"
							_exit_status="$((_exit_status+1))"
						else
							_do 'umountbindfs' "${_path}"
							_exit_status="$((_exit_status+$?))"
						fi
						;;
					*)
						echo 'Error: invalid path' 1>&2
						_exit_status="$((_exit_status+$?))"
						;;
				esac
				unset _path
				shift 1
			done
			if [ "${_exit_status}" = '0' ]; then
				exit 0
			else
				exit 1
			fi
		;;
		-s)
			if [ -z "${2}" ]; then
				echo 'Will configured mount paths are about to be cleared'
				rm -rf "${_p_file}"
				echo "status: Success"
				exit 0
			fi
			shift 1
			rm -f "${_p_file}"
			plutil -create "${_p_file}" 1>>/dev/null
			plutil -key '0' -array "${_p_file}" 1>>/dev/null
			echo 'The following paths will be mounted on the next jailbreak'
			echo ''
			a='1'
			while [ "${#}" -gt '0' ]; do
				_path="$(echo "${1}" | sed 's#/$##' | sed 's#^/var#/private/var#' | sed 's#^/rootfs##')"
				if [ -e "${_root_path}${_path}" ]; then
					plutil -key '0' -arrayadd -string "${_path}" "${_p_file}" 1>>/dev/null
					echo "${_path}"
				fi
				unset _path
				shift 1
				a="$((a+1))"
			done
			plutil -convert xml1 "${_p_file}" 1>>/dev/null
			sed -i. '/^<dict>$/d' "${_p_file}"
			sed -i. '/<key>0<\/key>$/d' "${_p_file}"
			sed -i. '/^<\/dict>$/d' "${_p_file}"
			rm -f "${_p_file}."
			plutil -convert xml1 "${_p_file}" 1>>/dev/null
			exit 0
		;;
		-l)
			if [ ! -f "${_p_file}" ]; then
				echo 'You have not set any folder for automatic mount.'
				exit 0
			fi
			i='0'
			i_max="$(plutil "${_p_file}" | wc -l)"
			i_max="$((i_max-3))"
			while [ "${i}" -le "${i_max}" ]; do
				plutil -key "${i}" "${_p_file}"
				i="$((i+1))"
			done
			exit 0
		;;
		*)
			echo 'Error: invalid path' 1>&2
			exit 2
		;;
	esac
done
