#!/bin/bash

#
# Script by Razuuu
# Works for Debian 12+
#

# Get apt packages from wiki build page (device: beyond1lte)
PACKAGES=$(curl -s https://wiki.lineageos.org/devices/beyond1lte/build | grep -o '<code class="language-plaintext highlighter-rouge">.*</code>' | awk -F'>|<' '{print $3}' | grep "bc" | grep "curl")

# SCRIPT CONFIG
ARG_CONFIGFILE=$1
CONFIGFILE="$(basename $0 .sh)_config.txt"

# SCRIPT FUNCTIONS
function user_shell() {
	su ${MACHINE_USERNAME} -c "$*"
}

# Check shell arguments
if [ -z ${ARG_CONFIGFILE} ]; then
	echo "Usage: ./$(basename $0) <${CONFIGFILE}>"
	exit 999
fi

# Check if configfile exist
if [ ! -f ${ARG_CONFIGFILE} ]; then
	# Create a template
	echo    "LINEAGE_VERSION=\"\" # e.g 18.1" >> ${CONFIGFILE}
	echo -e "MACHINE_USERNAME=\"\" # all lowercase\n" >> ${CONFIGFILE}
	echo    "SSHPUB_KEY=\"\"" >> ${CONFIGFILE}
	echo -e "SSHPRIV_KEY=\"\"\n" >> ${CONFIGFILE}
	echo    "GITUSER=\"\"" >> ${CONFIGFILE}
	echo    "GITEMAIL=\"\"" >> ${CONFIGFILE}
	echo    "Created configfile \"${CONFIGFILE}\" due to the file not existing, please edit!"
	exit 999
else
	# Source file
	source ${ARG_CONFIGFILE}

	# Check if all is set
	CONFIGVARS=("LINEAGE_VERSION" "MACHINE_USERNAME" "SSHPUB_KEY" "SSHPRIV_KEY" "GITUSER" "GITEMAIL")
	for CONFIGVAR in "${CONFIGVARS[@]}"; do
		CONFIGVARNOTSET="no"
		if [ -z "${!CONFIGVAR}" ]; then
			CONFIGVARNOTSET="yes"
			echo "${CONFIGVAR} is not set"
		fi
	done
	if [ ${CONFIGVARNOTSET} == "yes" ]; then
		echo "Please edit the configfile!"
		exit 999
	fi
fi

# Update machines apt cache
apt update

# Install curl (needed for install packages)
apt -y install curl

# Install packages
apt -y install ${PACKAGES} repo screen

# Fix libncurses5 by linking libcurses6
ln -s /lib/x86_64-linux-gnu/libncurses.so.6 /lib/x86_64-linux-gnu/libncurses.so.5
ln -s /lib/x86_64-linux-gnu/libtinfo.so.6 /lib/x86_64-linux-gnu/libtinfo.so.5

# Create user
adduser --disabled-password --gecos "" ${MACHINE_USERNAME}

# Setup SSH
SSHPATH="/home/${MACHINE_USERNAME}/.ssh"
SSHKEY_TYPE=$(echo ${SSHPUB_KEY} | grep -oE '^[^[:space:]]+' | sed 's/ssh-//')
mkdir -p ${SSHPATH}
echo "${SSHPUB_KEY}" > ${SSHPATH}/id_${SSHKEY_TYPE}.pub
echo "${SSHPRIV_KEY}" > ${SSHPATH}/id_${SSHKEY_TYPE}
chown ${MACHINE_USERNAME}:${MACHINE_USERNAME} -R ${SSHPATH}
chmod 700 ${SSHPATH}/
chmod 644 ${SSHPATH}/id_${SSHKEY_TYPE}.pub
chmod 600 ${SSHPATH}/id_${SSHKEY_TYPE}

# Setup Git
echo "[user]
	name = ${GITUSER}
	email = ${GITEMAIL}
[filter \"lfs\"]
	smudge = git-lfs smudge -- %f
	process = git-lfs filter-process
	required = true
	clean = git-lfs clean -- %f
[color]
	ui = auto" > /home/${MACHINE_USERNAME}/.gitconfig
chmod +x /home/${MACHINE_USERNAME}.gitconfig

# Sync LineageOS sources
USERHOME="/home/${MACHINE_USERNAME}"
user_shell "mkdir -p ${USERHOME}/android/lineage"
user_shell "cd ${USERHOME}/android/lineage && repo init -u https://github.com/LineageOS/android.git -b lineage-${LINEAGE_VERSION} --git-lfs"
user_shell "cd ${USERHOME}/android/lineage && screen -dmS lineage_reposync repo sync"
