#!/bin/bash

################################################################################
# Title:        BldHelper-release.sh
# Description:  This script is meant to be run on the build server and expects 
#               to find and update itself from adjacent repos.
#               From PepDistroConfigs, these repos are ../PepProPixMaps &
#               ../PepProTools and are vital to having a working build.
# Author:       PeppermintOS Team <peppermintosteam@proton.me>
# Date:         May 10, 2023
# License:      GPL-3.0-or-later
################################################################################

# Set build working variables
PREFIX="Pepserver"          # Sets a unique final name of the ISO and checksum so <HouseKeeping> only removes 2 files.
SUFFIX="amd64"              # Also used by <HouseKeeping>. And to distinguish between amd64 and x86 or devuan and ubuntu.
BUILD="Dev"                 # Sets which pepbld.sh to use and the location in /var/www/html/[release|rc|testing|nightly|unstable]

# Make sure the script is running as root
if [[ $EUID -ne 0 ]]; then
   echo "This script must be run as root" 
   exit 1
fi

# Change to the working directory
cd /home/pepadmin/PepOSServer/daedalus

# Run the build script and redirect output to a log file
LogFile="/var/log/Live-Build/${PREFIX}-${SUFFIX}-${BUILD}.log"
./pepbld.sh &> $LogFile

# Set variables for file naming and location
TODAY=$(date -u +"%Y-%m-%d")
FileName="${PREFIX}-${SUFFIX}-${TODAY}"
LOCATION="/var/www/html/nightly/PepOSServer/${BUILD}"
OutFile="/tmp/${PREFIX}${SUFFIX}.out"
_cache="./cache"
_break=0 ; _wait=30

# Wait for the ISO to appear and move it to the correct location
until [ -e fusato/*.iso ]
do
    ((++_break))
    [ $_break -gt $_wait ] && break || sleep 1
done

if [ ${_break} -lt ${_wait} ]; then
    mv fusato/*.iso fusato/${FileName}.iso

    # Make the checksum file
    cd fusato
    echo "# ${FileName} ${TODAY}" > ${FileName}-sha512.checksum
    sha512sum ${FileName}.iso >> ${FileName}-sha512.checksum

    # Remove previous files and move the new files to the correct location
    rm -f ${LOCATION}/${PREFIX}-${SUFFIX}*.iso
    rm -f ${LOCATION}/${PREFIX}-${SUFFIX}*sha512.checksum
    mv ${FileName}.iso ${LOCATION}/${FileName}.iso
    mv ${FileName}-sha512.checksum ${LOCATION}/${FileName}-sha512.checksum
    touch ${LOCATION}/${FileName}*

    # Clean up old packages from the cache directory
    for i in $(grep "Del " ${LogFile} | sort -u | cut -f2,3 -d" " | tr " " "_" | tr ":" "*" | tr "+" "*" )
    do
        for j in $_cache/packages.*/${i}*.deb
        do  
            [ -e $j ] && rm $j
        done
    done

    # Clean up build artifacts
    lb clean &>/dev/null

    # Move the log file to the log directory
    [ ! -e /var/log/Live-Build ] && mkdir -p /var/log/Live-Build
    mv ${OutFile} ${LogFile}

    else 
	echo -e "\n\tAfter $_break seconds, ISO never appeared.\n" | tee --append ${OutFile}
	mv ${OutFile} ${LogFile}
    fi

    # Remove the fusato directory and its contents if it exists.
    if [ -d "fusato" ]; then
	rm -r fusato
    fi

