#!/bin/sh -


if [ -f '/etc/profile' ]; then
	. /etc/profile
elif [ -f '/var/jb/etc/profile' ]; then
	. /var/jb/etc/profile
else
	echo 'Where the fuck "profile"?' 1>&2
	exit 1
fi

_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 "${_jb_root_path}/basebin/.safe_mode" ]; then
	_exit
fi
if [ -f '/var/mobile/.not_auto_mount' ]; then
	_exit
fi

if [ ! -f "${_p_file}" ]; then
	_exit
fi
i='0'
i_max="$(plutil "${_p_file}" | wc -l)"
i_max="$((i_max-3))"
while [ "${i}" -le "${i_max}" ]; do
	if mount_bindfs "$(plutil -key "${i}" "${_p_file}")" 1>>'/dev/null' 2>>'/dev/null'; then
		_reboot_userspace='1'
	fi
	i="$((i+1))"
done
if [ -n "${_reboot_userspace}" ]; then
	sleep 1
	launchctl reboot userspace
fi
_exit
