#!/bin/sh -


if ! id -u | grep -q '^0$'; then
	exec sudo "${0}" "$@"
fi
readonly _command="${0##*/}"
export LANG=en_US.UTF-8


_randnum(){
	if [ -z "${2}" ]; then
		tr -dc 'A-Za-z0-9' < /dev/urandom | head -c "${1}"
	else
		tr -dc "${1}" < /dev/urandom | head -c "${2}"
	fi
}

case "${1}" in
	''|-h|--help)
		echo "Usage: ${_command} [--help/--install] <file>"
		exit 0
		;;
	--install)
		_ext='1'
		shift
		;;
	--filza)
		_ext='2'
		shift
		;;
	*)
		sleep 0
		;;
esac
case "$(dpkg-deb -f "${1}" 'Architecture')" in
	iphoneos-arm64)
		sleep 0
		;;
	*)
		echo "[x] unsupport this deb arch($(dpkg-deb -f "${1}" 'Architecture'))"
		echo '[i] irld only support "iphoneos-arm64"'
		exit 1
		;;
esac

_path(){
	sed -i.bak 's#^Architecture: .*#Architecture: iphoneos-arm#' "${1}/DEBIAN/control"
	sed -i.bak '/^Installed-Size/d' "${1}/DEBIAN/control"
	if ! cat "${1}/DEBIAN/control" | grep -q '^Depends: ' ; then
		echo 'Depends: com.nan.irld' >>"${1}/DEBIAN/control"
	else
		if ! cat "${1}/DEBIAN/control" | grep -q ', com.nan.irld' ; then
			sed -i.bak '/^Depends: .*/s/$/, com.nan.irld/' "${1}/DEBIAN/control"
			sed -i.bak 's# , com.nan.irld#, com.nan.irld#' "${1}/DEBIAN/control"
		fi
	fi
    sed -i.bak 's#firmware[^,]*,##' "${1}/DEBIAN/control"
    sed -i.bak 's#Firmware[^,]*,##' "${1}/DEBIAN/control"
    sed -i.bak "$(grep -n '^Depends: ' "${1}/DEBIAN/control" | awk -F ':' '{print $1}')s#  # #g" "${1}/DEBIAN/control"
	if ! cat "${1}/DEBIAN/control" | grep -q '^Description: ' ; then
		echo 'Description: An awesome MobileSubstrate tweak!' >>"${1}/DEBIAN/control"
	fi
	if ! cat "${1}/DEBIAN/control" | grep -q '^Maintainer: ' ; then
		echo 'Maintainer: someone' >>"${1}/DEBIAN/control"
	fi
	rm -f "${1}/DEBIAN/control.bak"
	rm -f "${1}/DEBIAN/md5sums"
	chmod -R 0755 "${1}/DEBIAN"/*
	chmod -R 0644 "${1}/DEBIAN/control"
	if [ -f "${1}/DEBIAN/triggers" ]; then
		chmod -R 0644 "${1}/DEBIAN/triggers"
	fi
}

while [ '1' -le '2' ]; do
	_tmp="/tmp/$(_randnum 'A-Z0-9' '8')-$(_randnum 'A-Z0-9' '4')-$(_randnum 'A-Z0-9' '4')-$(_randnum 'A-Z0-9' '4')-$(_randnum 'A-Z0-9' '12')"
	if [ ! -e "${_tmp}" ]; then
		mkdir -p "${_tmp}"
		break
	fi
done
echo 'Cache Directory:'
echo "${_tmp}"
_info="$(dpkg-deb -f "${1}" 'Package')_$(dpkg-deb -f "${1}" 'Version')_iphoneos-arm"
echo '[*] unzip deb...'
dpkg-deb -x "${1}" "${_tmp}/${_info}" &
mkdir -p "${_tmp}/${_info}/DEBIAN/"
dpkg-deb -e "${1}" "${_tmp}/${_info}/DEBIAN/" &
wait
echo '[*] Applying Patches...'
_path "${_tmp}/${_info}"
echo '[*] Repackage as deb...'
dpkg-deb -b "${_tmp}/${_info}" "${_tmp}/${_info}.deb" 1>>/dev/null
echo "new deb file:"
echo "${_tmp}/${_info}.deb"
case "${_ext}" in
	0)
		sleep 0
		;;
	1)
		echo '[*] Hand it over to apt for further installation...'
		printf '%*s\n' "$(($(tput cols)-1))" | tr ' ' '='
		exec apt install --allow-change-held-packages "${_tmp}/${_info}.deb"
		;;
	2)
		if [ -e "${PWD}/${_info}.deb" ]; then
			mv "${_tmp}/${_info}.deb" "${PWD}/${_info}_$(_randnum 'A-Z0-9' '20').deb"
		else
			mv "${_tmp}/${_info}.deb" "${PWD}/${_info}.deb"
		fi
		;;
esac
