From 65e286eced881c6602543e9855ca0cb5b14a3223 Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Tue, 6 Aug 2019 00:02:44 +0100
Subject: [PATCH] Create initial build script

---
 .gitmodules          |   3 +
 aptosaurus.sh        | 157 +++++++++++++++++++++++++++++++++++++++++++
 lantern-build-engine |   1 +
 3 files changed, 161 insertions(+)
 create mode 100644 .gitmodules
 create mode 100644 aptosaurus.sh
 create mode 160000 lantern-build-engine

diff --git a/.gitmodules b/.gitmodules
new file mode 100644
index 0000000..ecc03d9
--- /dev/null
+++ b/.gitmodules
@@ -0,0 +1,3 @@
+[submodule "lantern-build-engine"]
+	path = lantern-build-engine
+	url = https://gitlab.com/sbrl/lantern-build-engine.git
diff --git a/aptosaurus.sh b/aptosaurus.sh
new file mode 100644
index 0000000..174dff7
--- /dev/null
+++ b/aptosaurus.sh
@@ -0,0 +1,157 @@
+#!/usr/bin/env bash
+
+# Make sure the current directory is the location of this script to simplify matters
+cd "$(dirname "$(readlink -f "$0")")" || exit 2;
+export GNUPGHOME="${PWD}";
+
+################
+### Settings ###
+################
+
+# The name of this project
+project_name="aptosaurus";
+
+# The path to the lantern build engine git submodule
+lantern_path="./lantern-build-engine";
+
+###
+# Custom Settings
+###
+
+# The directory that contains the source deb packages
+dir_sources="sources";
+# The directory the repository is in
+dir_repo="repo";
+
+# The id of the GPG key to use for signing
+gpg_key_id="7A37B795C20E4651D9BBE1B2D48D801C6A66A5D8";
+
+###############################################################################
+
+# Check out the lantern git submodule if needed
+if [ ! -f "${lantern_path}/lantern.sh" ]; then git submodule update --init "${lantern_path}"; fi
+
+source "${lantern_path}/lantern.sh";
+
+if [[ "$#" -lt 1 ]]; then
+	echo -e "${FBLE}${project_name}${RS}";
+	echo -e "    by Starbeamrainbowlabs";
+	echo -e "${LC}Powered by the lantern build engine, v${version}${RS}";
+	echo -e "";
+	echo -e "Based on https://askubuntu.com/a/89698/139735";
+	echo -e "";
+	echo -e "${CSECTION}Usage${RS}";
+	echo -e "    ./build ${CTOKEN}{action}${RS} ${CTOKEN}{action}${RS} ${CTOKEN}{action}${RS} ...";
+	echo -e "";
+	echo -e "${CSECTION}Available actions${RS}";
+	echo -e "    ${CACTION}setup${RS}    - Perform initial setup";
+	echo -e "    ${CACTION}update${RS}   - Scan for new packages and add them to the repository";
+	echo -e "";
+	
+	exit 1;
+fi
+
+###############################################################################
+
+task_setup() {
+	task_begin "Checking environment";
+	check_command gpg true;
+	check_command cp true;
+	check_command rm true;
+	check_command mkdir true;
+	check_command find true;
+	check_command xargs true;
+	check_command ln true;
+	check_command apt-ftparchive true;
+	check_command dpkg-sig true;
+	task_end $?;
+	
+	task_begin "Checking keys";
+	if [ ! -f "./public.gpg" ]; then
+		echo "Error: Couldn't find the public key to use.";
+		echo "Make sure that ${HC}public.gpg${HC} exist on disk.";
+		exit 1;
+	fi
+	task_end $?;
+	
+	if [ ! -e "$GNUPGHOME/secring.gpg" ]; then
+		if [ ! -f "./secret.gpg" ]; then
+			echo "Couldn't find the secret key to use.";
+			echo "Make sure ${HC}secret.gpg${RS} exists on disk.";
+		fi
+		task_begin "Importing keys";
+		gpg --import -v -v ./secret.gpg;
+		gpg --import -v -v ./public.gpg;
+		gpg --list-keys;
+		rm secret.gpg;
+		task_end $?;
+	fi
+	
+	if [ ! -f "${repo_dir}/aptosaurus.asc" ]; then
+		task_begin "Hard linking public signing key";
+		cp -al "./public.gpg" "${dir_repo}/aptosaurus.asc";
+		task_end $?;
+	fi
+	
+	task_begin "Creating directories";
+	mkdir "${dir_sources}" "${dir_repo}";
+	task_end $?;
+}
+
+# $1 - the .deb file to symlink
+_symlink_deb() {
+	source="$1";
+	destination="${dir_repo}/$(basename "${source}")";
+	
+	if [ -f "${dir_repo}/$(basename "$1")" ]; then return; fi
+	
+	ln -s "../${dir_sources}/$(basename "${source}")" "${destination}";
+}
+
+task_update() {
+	task_begin "Symlinking new packages";
+	package_count_before="$(find ${dir_repo} -type f -name "*.deb" | wc -l)";
+	export -f _symlink_deb;
+	cd "${dir_repo}" || exit 2;
+	find "${dir_sources}" -type f -name "*.deb" -print0 | xargs -n1 -I{} bash -c '_symlink_deb "{}"';
+	exit_code="$?";
+	cd - || exit 2;
+	package_count_after="$(find ${dir_repo} -type f -name "*.deb" | wc -l)";
+	task_end "${exit_code}";
+	
+	if [[ "${package_count_before}" -eq "${package_count_after}" ]]; then
+		echo "No new packages to process.";
+		exit 0;
+	else
+		new_package_count=$((package_count_after-package_count_before));
+		echo -e "Found ${new_package_count} new packages";
+	fi
+	
+	cd "${dir_repo}" || exit 2;
+	
+	task_begin "Signing packages";
+	execute dpkg-sig -k "${gpg_key_id}" -s builder "*.deb";
+	task_end $?;
+	
+	task_begin "Building packages file";
+	execute apt-ftparchive packages . >Packages;
+	execute bzip2 -kf Packages;
+	task_end $?;
+	
+	task_begin "Generating release file";
+	
+	subtask_begin "Generating file";
+	execute apt-ftparchive release . >Release;
+	subtask_end $?;
+	
+	subtask_begin "Signing release file";
+	execute gpg --yes -abs -u "${gpg_key_id}" -o Release.gpg Release
+	subtask_end $?;
+	
+	task_end $?;
+}
+
+
+###############################################################################
+
+tasks_run $@;
diff --git a/lantern-build-engine b/lantern-build-engine
new file mode 160000
index 0000000..617fcdb
--- /dev/null
+++ b/lantern-build-engine
@@ -0,0 +1 @@
+Subproject commit 617fcdb5b9df7f57ccb6639a87be68238f99b9ed
