#!/bin/bash
# nakedeb build script
# nak3dev @ 2020-2023 © WTFPL <https://arpinux.org>

# cfg ------------------------------------------------------------------
PROJECT="nakedeb project"
HOMEPAGE="https://nakedeb.arpinux.org"
CONTACT="nakedeb@arpinux.org"
VERSION="nakedeb-1.5.3"
DEBV="deb$(cat /etc/debian_version)"
DISTRO="bookworm"
NKDIR="$(realpath "$(dirname "$0")")"
LOG="${NKDIR}/${VERSION}.log"

# stop on error and try to trace it ------------------------------------
set -eE # same as: `set -o errexit -o errtrace`

# cd work directory ----------------------------------------------------
cd ${NKDIR}

# root -----------------------------------------------------------------
if [ $(whoami) != root ]; then
    echo -e "\nERROR: nakedbuild needs root\n"; exit 1
fi

# args & help ----------------------------------------------------------
if [ "$1" == "32" ]; then ARCH="i386"
elif [ "$1" == "64" ]; then ARCH="amd64"
elif [ "$1" == "clean" ]; then
    lb clean
    rm -Rf cache
    exit 0
else
    # no args or wrong args > help
    echo -e "\n wrong arg... need help ?\n"; cat ${NKDIR}/HELP; exit 1
fi

# open log -------------------------------------------------------------
# ouverture du fichier de log avec les informations sur le système hôte
echo -e "----\n${PROJECT} - ${HOMEPAGE} - ${CONTACT}" > ${LOG}
echo -e "$(date)" >> ${LOG}
echo -e "system: $(uname -a)" >> ${LOG}
echo -e "tools: live-build-$(lb -v) - Debian-$(cat /etc/debian_version)" >> ${LOG}
echo -e "> building ${VERSION} ${ARCH} ${DISTRO}" >> ${LOG}

# setup preseed --------------------------------------------------------
# mise en place du fichier preseed pour la fin de l'installation.
echo -e "----\nINFO: setting up naked preseed" >> ${LOG}
mkdir -p config/includes.chroot/usr/bin
install -m 755 ressources/nakedpreseed config/includes.chroot/usr/bin/nakedpreseed

# configure build ------------------------------------------------------
echo -e "----\nINFO: configuration de ${VERSION}-${ARCH}" >> ${LOG}
# common config
lb config -a "${ARCH}" \
    --iso-volume "${VERSION}" \
    --iso-application "${VERSION}" \
    --iso-publisher "${PROJECT}; ${HOMEPAGE}; ${CONTACT}" \
    --parent-distribution "${DISTRO}" \
    --distribution "${DISTRO}" | tee -a ${LOG}
# ordinosaures
if [ "${ARCH}" == "i386" ]; then
    lb config --linux-flavours "686" | tee -a ${LOG}
fi
# if apt-cacher-ng
if dpkg-query -W apt-cacher-ng &>"/dev/null"; then
    export http_proxy="http://127.0.0.1:3142/"
    lb config --apt-http-proxy "${http_proxy}"
fi

# build ISO ------------------------------------------------------------
echo -e "----\nINFO: construction de ${VERSION}-${ARCH}-${DISTRO}\n" >> ${LOG}
lb build

# rename ---------------------------------------------------------------
if test -f live-image-${ARCH}.hybrid.iso; then
    echo -e "----\nINFO: renommer"
    ISODIR="${NKDIR}/nakedeb-${ARCH}"
    NAME="${VERSION}-${DEBV}-${ARCH}"
    mkdir -p ${ISODIR}
    mv live-image-${ARCH}.hybrid.iso ${ISODIR}/${NAME}.iso
    mv chroot.packages.install ${ISODIR}/${NAME}.pkgs
    mv ${LOG} ${ISODIR}/${NAME}.log
    cd ${ISODIR}
    echo -e "----\nINFO: calculate md5sum"
    md5sum ${NAME}.iso > ${NAME}.md5
    echo -e "----\nINFO: make torrent"
    cp ${NKDIR}/ressources/mkp2p ${ISODIR}
    ./mkp2p ${NAME}.iso
    rm mkp2p
    cd ${NKDIR}
    echo -e "----\nINFO: permissions"
    chown -R ${SUDO_USER}:${SUDO_USER} ${ISODIR}
    echo -e "----\nINFO: nettoyage"
    lb clean
    echo -e "----\nbuild made in $(($SECONDS/60)) minutes\n"
    echo -e "MEMO: you should gpgsign md5 files\n"; exit 0
else
    chmod 666 ${LOG}
    echo -e "----\nERROR: ISO not builded ... check your ${LOG} file !\n"; exit 1
fi
# eof ------------------------------------------------------------------
