#!/bin/sh

# Copyright 2017 Attila Szollosi
#
# This file is part of postmarketos-android-recovery-installer.
#
# postmarketos-android-recovery-installer is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# postmarketos-android-recovery-installer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with postmarketos-android-recovery-installer.  If not, see <http://www.gnu.org/licenses/>.

export PATH="/bin"
export LD_LIBRARY_PATH="/lib"

# shellcheck source=/dev/null
. /install_options

# taken from https://github.com/Debuffer-XDA/Gov-Tuner/blob/master/META-INF/com/google/android/update-binary
# Copyright (c) 2016 - 2017 Debuffer
ui_print() {
	echo "ui_print $1" > /proc/self/fd/"$OUTFD"
	echo "ui_print" > /proc/self/fd/"$OUTFD"
}

# $1: fstab filename
# $2: partition
get_fstab_device() {
	src_column=$(awk '{for (i=1; i<=NF; ++i) { if ($i ~ /^\/dev/) {print i; exit;} }}' /"$1")
	[ -z "$src_column" ] && src_column=3
	# Warning: partition name is not escaped
	awk -v src="$src_column" \
		'!/^#/ && /(^|\s*)\/'"$2"'/ {print $src; exit;}' /"$1"
}

extract_partition_table() {
	fstab_recovery="recovery.fstab"
	# TWRP can use twrp.fstab instead of recovery.fstab (device specific)
	# This check exists to support both formats.
	if [ ! -e "/"$fstab_recovery ]; then
		fstab_recovery="twrp.fstab"
	fi

	if [ -n "$INSTALL_PARTITION" ]; then
		# We need to resolve symlinks, to make set_subpartitions() work.
		dev=$(findfs PARTLABEL="$INSTALL_PARTITION") || \
			dev=$(readlink -fn "$(get_fstab_device "$fstab_recovery" "$INSTALL_PARTITION")")
	fi

	if [ -n "$dev" ]; then
		echo "install device found at $dev"
		export INSTALL_DEVICE="$dev"
	else
		echo "Couldn't find $INSTALL_PARTITION partition."
		return 1
	fi
	if [ "$ISOREC" = "true" ]; then
		export KERNEL_PARTITION
		KERNEL_PARTITION=$(findfs PARTLABEL="$KERNEL_PARTLABEL")
		export INITFS_PARTITION
		INITFS_PARTITION=$(findfs PARTLABEL="$INITFS_PARTLABEL")
	else
		dev=$(findfs PARTLABEL="boot") || \
			dev=$(readlink -fn "$(get_fstab_device "$fstab_recovery" boot)")
		if [ -n "$dev" ]; then
			echo "boot partition found at $dev"
			export BOOT_PARTITION="$dev"
		else
			echo "Couldn't find boot partition."
			return 1
		fi
	fi
}

partition_install_device() {
	for command in "mktable msdos" \
		"mkpart primary ext2 2048s 256M" \
		"mkpart primary 256M 100%" \
		"set 1 boot on"
	do
		parted -s "$INSTALL_DEVICE" "$command"
	done
	partprobe
	if [ "$INSTALL_PARTITION" != "external_sd" ]; then
		kpartx -afs "$INSTALL_DEVICE"
	fi
	set_subpartitions
}

set_subpartitions() {
	export PMOS_BOOT
	PMOS_BOOT=/dev/mapper/"$(basename "$INSTALL_DEVICE")"p1
	export ROOT_PARTITION
	ROOT_PARTITION=/dev/mapper/"$(basename "$INSTALL_DEVICE")"p2
}
