#!/bin/bash
set -ueE -o pipefail

NAME=$(<Cargo.toml grep '^name =' | cut -d '"' -f 2)
VERSION=$(<Cargo.toml grep '^version =' | cut -d '"' -f 2)
MOS_NAME=$(<mos/Cargo.toml grep '^name =' | cut -d '"' -f 2)
MOS_VERSION=$(<mos/Cargo.toml grep '^version =' | cut -d '"' -f 2)
RELEASES_DIR="./release"


panic() {
  echo "ERROR: $*"
  exit 1
}

[ -x ~/.cargo/bin/cross ] || cargo install cross --git https://github.com/cross-rs/cross \
  || panic "Cannot install cross compiler. Install it manually, see https://github.com/cross-rs/cross ."

[ -x ~/.cargo/bin/cargo-xwin ] || cargo install cargo-xwin \
  || panic "Cannot install cargo-xwin. Install it manually: cargo install cargo-xwin"

which clang >/dev/null 2>&1 || panic "clang is not installed. Install it: sudo dnf install clang lld"
which llvm-lib >/dev/null 2>&1 || panic "llvm-lib is not installed. Install it: sudo dnf install llvm"

cargo clean || panic "Cannot clean \"target\" directory."
rm -rf "$RELEASES_DIR/"* || panic "Cannot clean \"$RELEASES_DIR\" directory."
mkdir -p "$RELEASES_DIR" || panic "Cannot create \"$RELEASES_DIR\" directory."

ARCHES=(
  x86_64-unknown-linux-gnu
  x86_64-pc-windows-msvc
#  aarch64-unknown-linux-gnu
)

for TARGET in "${ARCHES[@]}"
do
  echo "--- Building for $TARGET ---"
  if [[ "$TARGET" == *"-msvc" ]]; then
    # Create temporary bin directory with symlinks to satisfy tool expectations
    BIN_DIR="$(mktemp -d)"
    ln -sf "$(which llvm-rc)" "$BIN_DIR/rc.exe"
    ln -sf "$(which llvm-lib)" "$BIN_DIR/lib.exe"
    # Export RC for winres and update PATH
    export RC="$BIN_DIR/rc.exe"
    PATH="$BIN_DIR:$PATH" cargo xwin build --release --target "$TARGET" --features embed-locales || { rm -rf "$BIN_DIR"; panic "Cannot build release for \"$TARGET\" target using cargo-xwin."; }
    PATH="$BIN_DIR:$PATH" cargo xwin build --release --target "$TARGET" --manifest-path mos/Cargo.toml --target-dir target || { rm -rf "$BIN_DIR"; panic "Cannot build $MOS_NAME for \"$TARGET\" target using cargo-xwin."; }
    unset RC
    rm -rf "$BIN_DIR"
  else
    cross build --release --target "$TARGET" --features embed-locales || panic "Cannot build release for \"$TARGET\" target using cross."
    cross build --release --target "$TARGET" --manifest-path mos/Cargo.toml --target-dir target || panic "Cannot build $MOS_NAME for \"$TARGET\" target using cross."
  fi

  TARGET_BUILD_DIR="./target/$TARGET/release/"

  if [ -x "$TARGET_BUILD_DIR/$NAME" ]
  then
    EXECUTABLE="$NAME"
    ARCHIVE="$RELEASES_DIR/egui_pinger_$TARGET-$VERSION.tar.gz"

    echo "--- Packaging $ARCHIVE ---"
    TEMP_DIR=$(mktemp -d)
    mkdir -p "$TEMP_DIR/bin" "$TEMP_DIR/share/applications" "$TEMP_DIR/share/icons/hicolor/scalable/apps"

    cp "$TARGET_BUILD_DIR/$EXECUTABLE" "$TEMP_DIR/bin/"
    cp "$TARGET_BUILD_DIR/$MOS_NAME" "$TEMP_DIR/bin/"
    cp "assets/linux/com.github.vlisivka.EguiPinger.desktop" "$TEMP_DIR/share/applications/"
    cp "assets/linux/com.github.vlisivka.EguiPinger.svg" "$TEMP_DIR/share/icons/hicolor/scalable/apps/"

    rm -f "$ARCHIVE"
    tar -zcf "$ARCHIVE" -C "$TEMP_DIR" bin share || panic "Cannot make archive \"$ARCHIVE\" from \"$TEMP_DIR\"."

    rm -rf "$TEMP_DIR"

    # Packaging separate mos utility
    MOS_ARCHIVE="$RELEASES_DIR/${MOS_NAME}_$TARGET-$MOS_VERSION.tar.gz"
    echo "--- Packaging $MOS_ARCHIVE ---"
    TEMP_DIR=$(mktemp -d)
    mkdir -p "$TEMP_DIR/bin"
    cp "$TARGET_BUILD_DIR/$MOS_NAME" "$TEMP_DIR/bin/"
    rm -f "$MOS_ARCHIVE"
    tar -zcf "$MOS_ARCHIVE" -C "$TEMP_DIR" bin || panic "Cannot make archive \"$MOS_ARCHIVE\" from \"$TEMP_DIR\"."
    rm -rf "$TEMP_DIR"

  elif [ -x "$TARGET_BUILD_DIR/$NAME.exe" ]

  then
    EXECUTABLE="$NAME.exe"
    MOS_EXECUTABLE="$MOS_NAME.exe"
    ARCHIVE=$(readlink -f "$RELEASES_DIR/egui_pinger_$TARGET-$VERSION.zip")

    rm -f "$ARCHIVE"
    ( cd "$TARGET_BUILD_DIR" && zip "$ARCHIVE" "$EXECUTABLE" "$MOS_EXECUTABLE" ) || panic "Cannot make archive \"$ARCHIVE\" using files \"$EXECUTABLE\" and \"$MOS_EXECUTABLE\" from \"$TARGET_BUILD_DIR\" ."

    # Packaging separate mos utility
    MOS_ARCHIVE=$(readlink -f "$RELEASES_DIR/${MOS_NAME}_$TARGET-$MOS_VERSION.zip")
    rm -f "$MOS_ARCHIVE"
    ( cd "$TARGET_BUILD_DIR" && zip "$MOS_ARCHIVE" "$MOS_EXECUTABLE" ) || panic "Cannot make archive \"$MOS_ARCHIVE\" using file \"$MOS_EXECUTABLE\" from \"$TARGET_BUILD_DIR\" ."


  else
    panic "Cannot find executable for \"$TARGET\" target."
  fi

done
