#!/bin/bash
set -e

FLAVOUR="${1:-cvg}"
VERSION_GLIDE2="${2:-2.60-1}"
VERSION_GLIDE3="${3:-3.10.0-1}"
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}"

build_pkg_glide2() {
    PKGDIR="${BUILDROOT}/${PKG2}"
    mkdir -p "${PKGDIR}/DEBIAN" "${PKGDIR}/usr/lib"

    cp -a "${GLIDE2_SRC}/libglide.so.2.60" "${PKGDIR}/usr/lib/"
    cp -a "${GLIDE2_SRC}/libglide.a" "${PKGDIR}/usr/lib/"
    ln -s libglide.so.2.60 "${PKGDIR}/usr/lib/libglide.so.2"
    ln -s libglide.so.2.60 "${PKGDIR}/usr/lib/libglide.so"
    ln -s libglide.so.2.60 "${PKGDIR}/usr/lib/libglide2x.so"

    cat > "${PKGDIR}/DEBIAN/control" <<EOF
Package: ${PKG2}
Version: ${VERSION_GLIDE2}
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

    # retire self-conflict
    sed -i "s/, ${PKG2}//" "${PKGDIR}/DEBIAN/control"

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

build_pkg_glide3() {
    PKGDIR="${BUILDROOT}/${PKG3}"
    mkdir -p "${PKGDIR}/DEBIAN" "${PKGDIR}/usr/lib"

    cp -a "${GLIDE3_SRC}/libglide3.so.3.10.0" "${PKGDIR}/usr/lib/"
    cp -a "${GLIDE3_SRC}/libglide3.a" "${PKGDIR}/usr/lib/"
    ln -s libglide3.so.3.10.0 "${PKGDIR}/usr/lib/libglide3.so.3"
    ln -s libglide3.so.3.10.0 "${PKGDIR}/usr/lib/libglide3.so"
    ln -s libglide3.so.3.10.0 "${PKGDIR}/usr/lib/libglide3x.so"

    cat > "${PKGDIR}/DEBIAN/control" <<EOF
Package: ${PKG3}
Version: ${VERSION_GLIDE3}
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

    # retire self-conflict
    sed -i "s/, ${PKG3}//" "${PKGDIR}/DEBIAN/control"

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

build_pkg_glide2
build_pkg_glide3

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