aptosaurus/aptosaurus.sh

179 lines
4.8 KiB
Bash
Executable File

#!/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}/gnupg";
################
### 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 " ${CACTION}update-cron${RS} - Like ${CACTION}update${RS}, but silent unless something goes wrong";
echo -e " ${CACTION}metafiles${RS} - Rebuild the repository metafiles only (useful if you've manually fiddled with the repo packages)";
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 $?;
task_begin "Creating directories";
mkdir "${dir_sources}" "${dir_repo}";
task_end $?;
if [ ! -d "${GNUPGHOME}" ]; then
mkdir "${GNUPGHOME}";
chown 0700 "${GNUPGHOME}";
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
}
# $1 - the .deb file to symlink
_symlink_deb() {
source="$1";
destination="$(basename "${source}")";
if [ -f "${destination}" ]; then return; fi
ln -s "${source}" "${destination}";
}
task_update() {
task_begin "Symlinking new packages";
package_count_before="$(find ${dir_repo} -name "*.deb" | wc -l)";
export -f _symlink_deb;
cd "${dir_repo}" || exit 2;
find "../${dir_sources}" -type f -name "*.deb" -print0 | xargs --null -n1 -I{} bash -c '_symlink_deb "{}"';
exit_code="$?";
cd - || exit 2;
package_count_after="$(find ${dir_repo} -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 $?;
tasks_run "metafiles";
}
task_metafiles() {
if [[ "$(basename "${PWD}")" != "$(basename "${dir_repo}")" ]]; then
cd "${dir_repo}";
fi
task_begin "Building packages file";
apt-ftparchive packages . >Packages;
execute bzip2 -kf Packages;
task_end $?;
task_begin "Generating release file";
apt-ftparchive release . >Release;
task_end $?;
task_begin "Signing release file";
execute gpg --yes -abs -u "${gpg_key_id}" -o Release.gpg Release
task_end $?;
}
task_update-cron() {
tmpfile="$(mktemp --suffix ".aptosaurus.log")";
set +e;
bash ./aptosaurus.sh update | ansi_strip >"${tmpfile}";
exit_code="${?}";
if [[ "${exit_code}" -ne 0 ]]; then
cat "${tmpfile}";
fi
rm "${tmpfile}";
}
###############################################################################
tasks_run $@;