#!/bin/bash

SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )

ROOT_DIR=$(dirname $SCRIPT_DIR)

echo "Building dropshell from $ROOT_DIR"

# Build the builder image
#docker build -t dropshell_alpine_builder $ROOT_DIR -f $SCRIPT_DIR/Dockerfile.build

rm -rf $SCRIPT_DIR/output
mkdir -p $SCRIPT_DIR/output

MYUID=$(id -u)  
MYGID=$(id -g)

# Build for x86_64
echo "Building for x86_64..."
docker run --rm -tt --env CHOWN_USER=$MYUID --env CHOWN_GROUP=$MYGID \
    -v $SCRIPT_DIR/output:/output \
    -e TARGET_ARCH=x86_64 \
    -e CC=gcc \
    -e CXX=g++ \
    gitea.jde.nz/j/dropshell_builder:latest

mv $SCRIPT_DIR/output/dropshell $SCRIPT_DIR/output/dropshell_x86_64

$SCRIPT_DIR/output/dropshell_x86_64 version

echo "dropshell built in $SCRIPT_DIR/output/dropshell_x86_64"


# Build for arm64
echo "Building for arm64..."
docker run --rm -tt --env CHOWN_USER=$MYUID --env CHOWN_GROUP=$MYGID \
    -v $SCRIPT_DIR/output:/output \
    -e TARGET_ARCH=aarch64 \
    -e CC=aarch64-linux-gnu-gcc \
    -e CXX=aarch64-linux-gnu-g++ \
    gitea.jde.nz/j/dropshell_builder:latest

mv $SCRIPT_DIR/output/dropshell $SCRIPT_DIR/output/dropshell_aarch64


echo "dropshell built in $SCRIPT_DIR/output/dropshell_aarch64"

