2024-10-27 10:36:30 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
source ./common.d/variables.sh
|
|
|
|
|
|
|
|
log()
|
|
|
|
{
|
|
|
|
case "$2" in
|
|
|
|
error)
|
2024-10-27 11:12:26 +00:00
|
|
|
printf "\e[1m\e[31mERROR:\e[0m \e[1m%s\e[0m\n" "$1">&2;;
|
2024-10-27 10:36:30 +00:00
|
|
|
internal)
|
2024-10-27 13:34:14 +00:00
|
|
|
printf "\e[1m\e[96m >>\e[0m \e[1m%s\e[0m\n" "$1">&2;;
|
2024-10-27 10:36:30 +00:00
|
|
|
ierror)
|
2024-10-27 13:34:14 +00:00
|
|
|
printf "\e[1m\e[31m >>\e[0m \e[1m%s\e[0m\n" "$1">&2;;
|
2024-10-27 10:36:30 +00:00
|
|
|
*)
|
2024-10-27 11:12:26 +00:00
|
|
|
printf "\e[1m\e[92m>>>\e[0m \e[1m%s\e[0m\n" "$1">&2;;
|
2024-10-27 10:36:30 +00:00
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
|
|
|
mkdir_if_not_exists ()
|
|
|
|
{
|
|
|
|
[ -d "$1" ] && return 0
|
|
|
|
[ -f "$1" ] && rm -f "$1"
|
|
|
|
mkdir "$1"
|
|
|
|
}
|
|
|
|
|
2024-11-02 16:18:26 +00:00
|
|
|
umount_if_mouted()
|
|
|
|
{
|
|
|
|
[ ! -d "$1" ] && return 0
|
|
|
|
if grep -qs "$(realpath "$1")" /proc/mounts; then
|
|
|
|
umount "$1"
|
|
|
|
return $?
|
|
|
|
else
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
umount_force()
|
|
|
|
{
|
|
|
|
[ ! -d "$1" ] && return 0
|
|
|
|
while true; do
|
|
|
|
umount_if_mouted "$1" && break
|
|
|
|
log "Failed to umount $1. Trying again after 3 seconds" ierror
|
|
|
|
sleep 3
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2024-10-27 10:36:30 +00:00
|
|
|
prepare_env()
|
|
|
|
{
|
|
|
|
mkdir_if_not_exists "./cache"
|
|
|
|
mkdir_if_not_exists "./out"
|
|
|
|
mkdir_if_not_exists "./raw"
|
|
|
|
mkdir_if_not_exists "./tmp"
|
2024-11-02 17:33:43 +00:00
|
|
|
[ ! -d "./packages" ] && {
|
2024-11-03 06:15:51 +00:00
|
|
|
log "Packages not found. Cannot continue" error
|
2024-11-02 17:33:43 +00:00
|
|
|
exit 4
|
|
|
|
}
|
2024-10-27 10:36:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
require ()
|
|
|
|
{
|
|
|
|
which "$1" > /dev/null 2>&1 ||
|
|
|
|
{
|
|
|
|
log "$1 not found" error
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# shellcheck disable=SC2161
|
|
|
|
# shellcheck disable=SC2086
|
|
|
|
arguments() {
|
|
|
|
while [[ $# -gt 0 ]]; do
|
|
|
|
opt="$1"
|
|
|
|
|
|
|
|
shift
|
|
|
|
|
|
|
|
case "$(echo ${opt} | tr '[:upper:]' '[:lower:]')" in
|
|
|
|
-c | --cleanup)
|
2024-11-02 17:36:29 +00:00
|
|
|
cleanup
|
|
|
|
exit $? ;;
|
2024-11-02 17:31:46 +00:00
|
|
|
|
|
|
|
-r | --cleanraw)
|
|
|
|
cleanraw
|
2024-11-02 17:36:29 +00:00
|
|
|
exit $? ;;
|
2024-10-27 10:36:30 +00:00
|
|
|
|
|
|
|
-h | -help | --help)
|
2024-11-02 17:36:29 +00:00
|
|
|
usage
|
|
|
|
exit $? ;;
|
2024-10-27 10:36:30 +00:00
|
|
|
|
|
|
|
*)
|
2024-10-27 11:12:26 +00:00
|
|
|
export LNIBUILD="$opt"
|
2024-10-27 10:36:30 +00:00
|
|
|
break 2 ;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
usage() {
|
|
|
|
log "Usage commands:"
|
|
|
|
|
|
|
|
cat <<EOF
|
|
|
|
# Help screen (this)
|
|
|
|
$0 --help or $0 -h
|
|
|
|
|
|
|
|
# Cleanup
|
|
|
|
$0 --cleanup or $0 -c
|
|
|
|
|
2024-11-02 17:31:46 +00:00
|
|
|
# Clean raw images
|
|
|
|
$0 --cleanraw or -r
|
|
|
|
|
2024-10-27 10:36:30 +00:00
|
|
|
# Build LNIBUILD
|
|
|
|
$0 <path/to/LNIBUILD>
|
|
|
|
EOF
|
2024-11-02 17:31:46 +00:00
|
|
|
return 0
|
2024-10-27 10:36:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# shellcheck disable=SC2162
|
|
|
|
cleanup()
|
|
|
|
{
|
|
|
|
source ./common.d/build_functions.sh
|
|
|
|
find ./tmp/ -mindepth 1 -maxdepth 1 | read || {
|
|
|
|
[ -z ${quiet+x} ] && log "Nothing to clean" error
|
2024-11-02 17:31:46 +00:00
|
|
|
return 0
|
2024-10-27 10:36:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for d in ./tmp/*/; do
|
|
|
|
log "Unmounting $d"
|
|
|
|
detach_chroot "$d"
|
2024-11-02 16:14:29 +00:00
|
|
|
umount "$d/boot/simpleinit"
|
|
|
|
umount "$d/boot/efi"
|
|
|
|
umount "$d"
|
2024-10-27 10:36:30 +00:00
|
|
|
rm -d "$d"
|
|
|
|
done
|
2024-11-02 17:31:46 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanraw()
|
|
|
|
{
|
2024-11-02 17:37:53 +00:00
|
|
|
find ./raw/ -mindepth 1 -maxdepth 1 | read || {
|
|
|
|
[ -z ${quiet+x} ] && log "Nothing to clean" error
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
rm ./raw/* -f && log "Done!"
|
2024-10-27 10:36:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# shellcheck disable=SC2317
|
|
|
|
_shutdown()
|
|
|
|
{
|
|
|
|
quiet='' cleanup
|
|
|
|
if [ -z ${1+x} ]; then
|
|
|
|
exit 0
|
|
|
|
else
|
|
|
|
exit "$1"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
sigterm_handler()
|
|
|
|
{
|
|
|
|
sig=$?
|
|
|
|
printf "\e[1m\e[31m>>\e[0m \e[1m%s\e[0m\n" "Shutdown signal received."
|
|
|
|
_shutdown $sig
|
|
|
|
}
|
|
|
|
|
|
|
|
trap 'trap " " SIGINT SIGTERM SIGHUP; kill 0; wait; sigterm_handler' SIGINT SIGTERM SIGHUP
|