#!/bin/bash
#
# env vars:
# - ELECBUILD_NOCACHE: if set, forces rebuild of docker image
# - ELECBUILD_COMMIT: if set, do a fresh clone and git checkout
# - ARCH: target architecture (x86_64 or aarch64, default: x86_64)

set -e

PROJECT_ROOT="$(dirname "$(readlink -e "$0")")/../../.."
PROJECT_ROOT_OR_FRESHCLONE_ROOT="$PROJECT_ROOT"
CONTRIB="$PROJECT_ROOT/contrib"
CONTRIB_APPIMAGE="$CONTRIB/build-linux/appimage"
DISTDIR="$PROJECT_ROOT/dist"
BUILD_UID=$(/usr/bin/stat -c %u "$PROJECT_ROOT")

. "$CONTRIB"/build_tools_util.sh

# Determine architecture (default to x86_64 if not set)
export ARCH="${ARCH:-x86_64}"
if [ "$ARCH" != "x86_64" ] && [ "$ARCH" != "aarch64" ]; then
    fail "Unsupported ARCH: $ARCH. Must be x86_64 or aarch64"
fi
info "Building AppImage for architecture: $ARCH"


DOCKER_BUILD_FLAGS=""
if [ ! -z "$ELECBUILD_NOCACHE" ] ; then
    info "ELECBUILD_NOCACHE is set. forcing rebuild of docker image."
    DOCKER_BUILD_FLAGS="--pull --no-cache"
fi

if [ -z "$ELECBUILD_COMMIT" ] ; then  # local dev build
    DOCKER_BUILD_FLAGS="$DOCKER_BUILD_FLAGS --build-arg UID=$BUILD_UID"
fi

info "building docker image."
docker build \
    $DOCKER_BUILD_FLAGS \
    --build-arg TARGETARCH="$ARCH" \
    -t pallectrum-appimage-builder-img \
    "$CONTRIB_APPIMAGE"

# maybe do fresh clone
if [ ! -z "$ELECBUILD_COMMIT" ] ; then
    info "ELECBUILD_COMMIT=$ELECBUILD_COMMIT. doing fresh clone and git checkout."
    FRESH_CLONE="/tmp/pallectrum_build/appimage/fresh_clone/pallectrum"
    rm -rf "$FRESH_CLONE" 2>/dev/null || ( info "we need sudo to rm prev FRESH_CLONE." && sudo rm -rf "$FRESH_CLONE" )
    umask 0022
    git clone "$PROJECT_ROOT" "$FRESH_CLONE"
    cd "$FRESH_CLONE"
    git checkout "$ELECBUILD_COMMIT"
    PROJECT_ROOT_OR_FRESHCLONE_ROOT="$FRESH_CLONE"
else
    info "not doing fresh clone."
fi

# build the type2-runtime binary, this build step uses a separate docker container
# defined in the type2-runtime repo (patched with type2-runtime-reproducible-build.patch)
"$CONTRIB_APPIMAGE/make_type2_runtime.sh" || fail "Error building type2-runtime."

# if doing fresh clone, copy the runtime from original project to fresh clone
if [ ! -z "$ELECBUILD_COMMIT" ] ; then
    info "Copying type2-runtime to fresh clone..."
    RUNTIME_SRC="$PROJECT_ROOT/contrib/build-linux/appimage/.cache/appimage/type2-runtime"
    RUNTIME_DST="$FRESH_CLONE/contrib/build-linux/appimage/.cache/appimage/type2-runtime"
    mkdir -p "$(dirname "$RUNTIME_DST")"
    cp -r "$RUNTIME_SRC" "$(dirname "$RUNTIME_DST")/" || fail "Failed to copy runtime to fresh clone"
fi

DOCKER_RUN_FLAGS=""
if sh -c ": >/dev/tty" >/dev/null 2>/dev/null; then
    info "/dev/tty is available and usable"
    DOCKER_RUN_FLAGS="-it"
fi

info "building binary..."
# check uid and maybe chown. see #8261
if [ ! -z "$ELECBUILD_COMMIT" ] ; then  # fresh clone (reproducible build)
    if [ $(id -u) != "1000" ] || [ $(id -g) != "1000" ] ; then
        info "need to chown -R FRESH_CLONE dir. prompting for sudo."
        sudo chown -R 1000:1000 "$FRESH_CLONE"
    fi
fi
docker run $DOCKER_RUN_FLAGS \
    --name pallectrum-appimage-builder-cont \
    -v "$PROJECT_ROOT_OR_FRESHCLONE_ROOT":/opt/pallectrum \
    --rm \
    --workdir /opt/pallectrum/contrib/build-linux/appimage \
    -e ARCH="$ARCH" \
    pallectrum-appimage-builder-img \
    ./make_appimage.sh

# make sure resulting binary location is independent of fresh_clone
if [ ! -z "$ELECBUILD_COMMIT" ] ; then
    mkdir --parents "$DISTDIR/"
    cp -f "$FRESH_CLONE/dist"/* "$DISTDIR/"
fi
