#!/bin/bash

version_code=22.7
version_date=20200829

build_date=$(LANG='en_US' date +"%Y%m%d")

VERSION=${version_code}.${build_date}
CWD=$(readlink -fn "${BASH_SOURCE[0]}")
CWD=$(dirname "${CWD}")

case $(uname -s) in
	*Darwin* | *BSD* )
		export sumsha256="shasum -a 256"
		export statx="stat -f %Z"
	;;
	*Linux* )
		export sumsha256="sha256sum"
		export statx="stat -c %Z"
	;;
esac

# DIRS
datafolder="${CWD}/data"
repofolder="$datafolder/repo"

# AppsFolder
appsfolder[0]="Full"
appsfolder[1]="${appsfolder[0]}/system"
appsfolder[2]="${CWD}/${appsfolder[1]}"
appsfolder[3]="${appsfolder[1]}/app"
appsfolder[4]="${appsfolder[1]}/priv-app"
appsfolder[5]="Google/gsync"
appsfolder[6]="${CWD}/${appsfolder[5]}"
appsfolder[7]="Google/swipe"
appsfolder[8]="${CWD}/${appsfolder[7]}"
appsfolder[9]="OsmAnd/system"
appsfolder[10]="${CWD}/${appsfolder[9]}"
appsfolder[11]="BromiteWebView/system"
appsfolder[12]="${CWD}/${appsfolder[11]}"


for funcfile in pkg update common download database report; do
	source "$datafolder/build-package.${funcfile}" || exit 1
done

[[ -z ${1} ]] && show_help

for opt in ${@}; do
	case ${opt} in
		full)
			check_nanodroid || error "run 'build-package pull' first!"
			mk_pkg_full
		;;

		microg )
			check_nanodroid || error "run 'build-package pull' first!"
			mk_pkg_microg
		;;

		fdroid )
			check_nanodroid || error "run 'build-package pull' first!"
			mk_pkg_fdroid
		;;

		patcher )
			mk_pkg_patcher
		;;

		uninstaller )
			mk_pkg_uninstaller
		;;

		setupwizard )
			mk_pkg_setupwizard
		;;

		bromitewebview )
			check_nanodroid || error "run 'build-package pull' first!"
			mk_pkg_bromite_webview
		;;

		osmand )
			check_nanodroid || error "run 'build-package pull' first!"
			mk_pkg_osmand
		;;

		systest )
			mk_pkg_systest
		;;

		google )
			check_nanodroid || error "run 'build-package pull' first!"
			mk_pkg_google
		;;

		all )
			check_nanodroid || error "run 'build-package pull' first!"
			mk_pkg_full
			mk_pkg_microg
			mk_pkg_fdroid
			mk_pkg_patcher
			mk_pkg_uninstaller
			mk_pkg_setupwizard
			mk_pkg_bromite_webview
			mk_pkg_osmand
			mk_pkg_systest
			mk_pkg_google
			create_report
		;;

		ver )
			increase_version_number "${2}" "${3}"
			exit $?
		;;

		bump )
			increase_module_version
			exit $?
		;;

		dalvik )
			dalvikize_jar "${2}" "${3}"
			exit $?
		;;

		pull )
			update_indices
			nanodroid_pull
			update_master
			check_nanodroid
		;;

		u-microg )
			update_indices
			update_microg
			update_master
			check_nanodroid
		;;

		u-fdroid )
			update_indices
			update_fdroid
			update_master
			check_nanodroid
		;;

		u-apps )
			update_indices
			update_apps
			update_master
			check_nanodroid
		;;

		u-swipe )
			update_swipe
			update_master
			check_nanodroid
		;;

		u-gsync )
			update_gsync
			update_master
			check_nanodroid
		;;

		u-bromite )
			update_indices
			update_bromite
			update_master
			check_nanodroid
		;;

		u-osmand )
			update_indices
			update_osmand
			update_master
			check_nanodroid
		;;

		u-patch )
			update_patches
		;;

		check )
			check_nanodroid
		;;

		clean )
			echo -ne "\nAre you sure to delete all untracked files?\n >> enter [y] or [j] to continue: "
			read -r user_input

			case "${user_input}" in
				y | Y | j | J )
					git clean -fdX
				;;

				* )
					echo "aborted"
				;;
			esac
		;;

		report )
			check_nanodroid || error "run 'build-package pull' first!"
			create_report
		;;

		* )
			show_help
		;;
	esac
done
