#!/var/jb/bin/sh


. /var/jb/etc/profile

_jb_root_path="${0%/usr/libexec/mount_bindfs*}"
_p_file="${_jb_root_path}/var/mobile/Library/Preferences/com.nan.auto-bindfs.plist"

_exit(){
	launchctl unload '/var/jb/Library/LaunchDaemons/com.nan.auto-bindfs.plist'
	exit 0
}

if [ -f '/var/jb/basebin/.safe_mode' ]; then
	_exit
fi
if [ -f '/var/mobile/.not_auto_mount' ]; then
	_exit
fi
if [ -e '/tmp/com.nan.auto-bindfs' ]; then
	_exit
else
	mkdir '/tmp/com.nan.auto-bindfs'
fi

if [ ! -f "${_p_file}" ]; then
	_exit
fi
_content="$(plutil "${_p_file}" | sed '1d' | sed '$d')"
i='1'
i_max="$(echo "${_content}" | wc -l)"
while [ "${i}" -le "${i_max}" ]; do
	_path="$(echo "${_content}" | sed -n "${i}p")"
	_path="${_path#*\"}"
	_path="${_path%\"*}"
	if mount_bindfs "${_path}" 1>>'/tmp/com.nan.auto-bindfs/auto-bindfs.log' 2>>'/tmp/com.nan.auto-bindfs/auto-bindfs.log'; then
		_reboot_userspace='1'
	fi
	i="$((i+1))"
done
if [ -n "${_reboot_userspace}" ]; then
	launchctl reboot userspace
fi
_exit
