#!/bin/sh -


if [ -f '/etc/profile' ]; then
	. /etc/profile
	_apt_cache='/var/cache'
	_givemedeb_daemon='/Library/LaunchDaemons/com.nan.givemedeb.plist'
elif [ -f '/var/jb/etc/profile' ]; then
	. /var/jb/etc/profile
	_apt_cache='/var/jb/var/cache'
	_givemedeb_daemon='/var/jb/Library/LaunchDaemons/com.nan.givemedeb.plist'
else
	echo 'Where the fuck "profile"?' 1>&2
	exit 1
fi
_save_path='/var/mobile/Downloads/dpkg_triggered'
_tmp_folder="${_apt_cache}/givemedeb_caches"

if [ -e "${_tmp_folder}" ]; then
	mkdir -p "${_save_path}"
	if [ ! "$(ls "${_tmp_folder}" | wc -l)" = '0' ]; then
		i='1'
		i_max="$(ls "${_tmp_folder}" | wc -l)"
		while [ "${i}" -le "${i_max}" ]; do
			_deb_name="$(ls "${_tmp_folder}" | sed -n '1p')"
			rm -f "${_save_path}/${_deb_name}"
			mv "${_tmp_folder}/${_deb_name}" "${_save_path}/${_deb_name}"
			unset _deb_name
			i="$((i+1))"
		done
	fi
	rm -rf "${_tmp_folder}"
fi
launchctl unload "${_givemedeb_daemon}"
exit 0
