#!/bin/bash
set -euo pipefail

FLAVOUR="${1:-cvg}"
ARCH="${ARCH:-i386}"
OUTDIR="${OUTDIR:-$(pwd)}"

if [[ "$FLAVOUR" == "sst1" ]]; then
    GLIDE2_SRC="/usr/src/glide/glide2x/sst1/lib/sst1"
    GLIDE3_SRC="/usr/src/glide/glide3x/sst1/lib/sst1"
elif [[ "$FLAVOUR" == "sst96" ]]; then
    GLIDE2_SRC="/usr/src/glide/glide2x/sst1/lib/sst96"
    GLIDE3_SRC="/usr/src/glide/glide3x/sst1/lib/sst96"
else
    GLIDE2_SRC="/usr/src/glide/glide2x/${FLAVOUR}/lib"
    GLIDE3_SRC="/usr/src/glide/glide3x/${FLAVOUR}/lib"
fi

PKG2="libglide2-${FLAVOUR}"
PKG3="libglide3-${FLAVOUR}"

BUILDROOT="$(pwd)/pkg-glide"
rm -rf "${BUILDROOT}"
mkdir -p "${BUILDROOT}" "${OUTDIR}"

find_unique_regular_file() {
    local dir="$1"
    local pattern="$2"
    local matches=()

    while IFS= read -r -d '' f; do
        matches+=("$f")
    done < <(find "$dir" -maxdepth 1 -type f -name "$pattern" -print0)

    if [[ ${#matches[@]} -eq 0 ]]; then
        echo "ERROR: no regular file matching '${pattern}' found in '${dir}'" >&2
        exit 1
    fi

    if [[ ${#matches[@]} -ne 1 ]]; then
        echo "ERROR: multiple regular files matching '${pattern}' found in '${dir}':" >&2
        printf '  %s\n' "${matches[@]}" >&2
        exit 1
    fi

    basename "${matches[0]}"
}

extract_glide2_pkg_version() {
    local real_so="$1"
    local ver="${real_so#libglide.so.}"
    echo "${ver}-1"
}

extract_glide3_pkg_version() {
    local real_so="$1"
    local ver="${real_so#libglide3.so.}"
    echo "${ver}-1"
}

build_pkg_glide2() {
    local PKGDIR="${BUILDROOT}/${PKG2}"
    local REAL_SO
    local STATIC_A="libglide.a"
    local PKG_VERSION

    mkdir -p "${PKGDIR}/DEBIAN" "${PKGDIR}/usr/lib"

    REAL_SO="$(find_unique_regular_file "${GLIDE2_SRC}" 'libglide.so.2.*')"
    PKG_VERSION="$(extract_glide2_pkg_version "${REAL_SO}")"

    ls -al "${GLIDE2_SRC}/"

    cp -a "${GLIDE2_SRC}/${REAL_SO}" "${PKGDIR}/usr/lib/"
    cp -a "${GLIDE2_SRC}/${STATIC_A}" "${PKGDIR}/usr/lib/"

    ln -s "${REAL_SO}" "${PKGDIR}/usr/lib/libglide.so.2"
    ln -s "${REAL_SO}" "${PKGDIR}/usr/lib/libglide.so"
    ln -s "${REAL_SO}" "${PKGDIR}/usr/lib/libglide2x.so"

    cat > "${PKGDIR}/DEBIAN/control" <<EOF
Package: ${PKG2}
Version: ${PKG_VERSION}
Section: libs
Priority: optional
Architecture: ${ARCH}
Maintainer: RetroDebian <root@retrodebian.local>
Provides: libglide2
Conflicts: libglide2, libglide2-sst1, libglide2-cvg, libglide2-h3, libglide2-h5
Replaces: libglide2, libglide2-sst1, libglide2-cvg, libglide2-h3, libglide2-h5
Description: Glide 2 libraries for 3Dfx ${FLAVOUR}
 Glide 2 runtime and static libraries for the 3Dfx ${FLAVOUR} family.
EOF

    sed -i "s/, ${PKG2}//" "${PKGDIR}/DEBIAN/control"

    dpkg-deb --build "${PKGDIR}" "${OUTDIR}/${PKG2}_${PKG_VERSION}_${ARCH}.deb"
}

build_pkg_glide3() {
    local PKGDIR="${BUILDROOT}/${PKG3}"
    local REAL_SO
    local STATIC_A="libglide3.a"
    local PKG_VERSION

    mkdir -p "${PKGDIR}/DEBIAN" "${PKGDIR}/usr/lib"

    REAL_SO="$(find_unique_regular_file "${GLIDE3_SRC}" 'libglide3.so.3.*')"
    PKG_VERSION="$(extract_glide3_pkg_version "${REAL_SO}")"

    ls -al "${GLIDE3_SRC}/"

    cp -a "${GLIDE3_SRC}/${REAL_SO}" "${PKGDIR}/usr/lib/"
    cp -a "${GLIDE3_SRC}/${STATIC_A}" "${PKGDIR}/usr/lib/"

    ln -s "${REAL_SO}" "${PKGDIR}/usr/lib/libglide3.so.3"
    ln -s "${REAL_SO}" "${PKGDIR}/usr/lib/libglide3.so"
    ln -s "${REAL_SO}" "${PKGDIR}/usr/lib/libglide3x.so"

    cat > "${PKGDIR}/DEBIAN/control" <<EOF
Package: ${PKG3}
Version: ${PKG_VERSION}
Section: libs
Priority: optional
Architecture: ${ARCH}
Maintainer: RetroDebian <root@retrodebian.local>
Provides: libglide3
Conflicts: libglide3, libglide3-sst1, libglide3-cvg, libglide3-h3, libglide3-h5
Replaces: libglide3, libglide3-sst1, libglide3-cvg, libglide3-h3, libglide3-h5
Description: Glide 3 libraries for 3Dfx ${FLAVOUR}
 Glide 3 runtime and static libraries for the 3Dfx ${FLAVOUR} family.
EOF

    sed -i "s/, ${PKG3}//" "${PKGDIR}/DEBIAN/control"

    dpkg-deb --build "${PKGDIR}" "${OUTDIR}/${PKG3}_${PKG_VERSION}_${ARCH}.deb"
}

build_pkg_glide2
build_pkg_glide3

REAL_SO2="$(find_unique_regular_file "${GLIDE2_SRC}" 'libglide.so.2.*')"
REAL_SO3="$(find_unique_regular_file "${GLIDE3_SRC}" 'libglide3.so.3.*')"
PKG_VERSION2="$(extract_glide2_pkg_version "${REAL_SO2}")"
PKG_VERSION3="$(extract_glide3_pkg_version "${REAL_SO3}")"

echo "Packages created:"
echo "  ${OUTDIR}/${PKG2}_${PKG_VERSION2}_${ARCH}.deb"
echo "  ${OUTDIR}/${PKG3}_${PKG_VERSION3}_${ARCH}.deb"