#!/bin/sh
# Download the parts of bubbros that Debian cannot distribute, and check
# them: three fan-made level sets, and the upstream nonfree/ folder (the
# sounds and musics of the subgames, and the wall patterns drawn for
# HouseOfFun).
set -e

GAME=/usr/share/games/bubbros
# upstream tag 2.4 (commit 74ecc48, 2026-11-23), the first with the wall
# patterns in nonfree/; the level files have not changed since 2003-2005
URL=https://raw.githubusercontent.com/alexmyczko/bubbros/74ecc483f41f88db0e88fbc515e451e4a16bec18

# fetch <path in the repository> <path below the game> <sha256>
fetch() {
    dest=$GAME/$2 sum=$3
    if [ -f "$dest" ] && echo "$sum  $dest" | sha256sum -c --status; then
        return 0
    fi
    mkdir -p "$(dirname "$dest")"
    tmp=$(mktemp "$(dirname "$dest")/.$(basename "$dest").XXXXXX")
    if curl -fsSL --connect-timeout 20 --max-time 120 --retry 2 \
            -o "$tmp" "$URL/$1" &&
       echo "$sum  $tmp" | sha256sum -c --status
    then
        chmod 644 "$tmp"
        mv "$tmp" "$dest"
    else
        rm -f "$tmp"
        return 1
    fi
}

failed=
while read -r kind name sum; do
    case $kind in
        # the levels go to levels/fanlevels/, the sounds to nonfree/
        level) fetch "bubbob/levels/$name" "bubbob/levels/fanlevels/$name" "$sum" ;;
        sound) fetch "nonfree/bubbob/$name" "nonfree/bubbob/$name" "$sum" ;;
        # the game uses the original walls when pat00.ppm is there: it
        # comes last, and only when everything before it arrived
        wall)  if [ "$name" != pat00.ppm ] || [ -z "$failed" ]; then
                   fetch "nonfree/bubbob/tmp/$name" "nonfree/bubbob/tmp/$name" "$sum"
               else
                   false
               fi ;;
    esac || failed="$failed $name"
done <<'LIST'
level Arena.bin a557264b0d7605910ffed62b76d9d4ba25b7d450a52b49f7041826e3a759beee
level HouseOfFun.bin b8780d989db685aa195c762faf3cecd98685f7107ce6fae002e7c809428993ff
level LostLevels.bin d010fb28f43c152b2ca6b3062721ef04675dd100c1ac52e3d85078b32cacac29
sound ext1/brick.wav 5847df99e73e0dba863d232fcd2dd73a76bb2cb122b206cc04adb0b8534a8ba0
sound ext1/music.wav 60426a83a19bbf05e9abef91068bc8b4bf5d4b860ff5b9c727db307a748f00de
sound ext1/wall.wav ca2baf2da63b19d57f92fce8faf4347e7dda545509a99c86507a07dadfb32839
sound ext2/music.wav c2987fd9a9b17d10b33b7999451de2070c5ad3bb446baf3565c31228983172e9
sound ext3/music.wav 15531cc00b677d4310e9a413a00c8c7f740beaeb7ee6a71cd3660f2bc07e70c1
sound ext3/shoot.wav f1dd97dbf8618fd7bb37383d0d1ebe1d426b7bc78d8a3d9ad00a54768cc8afd6
sound ext4/music.wav ff75142634a17850bd9b027ea93a8f93eda70ad94bc18bc946813af256efe872
sound ext5/music.wav d3a569910522e9f469e8ef3d8986232c37fdf9d8b0847628766d8e9bcfedaac1
sound ext5/ouch.wav 00934151ff95c266b0489d0cb7d544c2ed2b3ff7d9bce21b240620f8c83ee69c
sound ext6/crash.wav 4e73341b9bda769bb1d3658458bc9e7d8452f53da959ba394858276cf444b3df
sound ext6/music.wav b316373a2ad3a25a8faea09739824ea2feff3d18b9ebe0e57867db7b7d11ba4c
sound ext7/fire.wav 41f1460725234110526e2a3ae89b69a30e812c6538dd8a2f51271614908be88a
sound ext7/hit.wav eab86f1748b4c3675d4a77d64b6d52c71c0c6dd9cb783fdbb0467feb65141e75
sound ext7/music.wav a32cb6567b9db21681d58fe074f4fba70a716c27bf2123fb1725ea6f6b32f5c0
wall pat01.ppm 54b6cb1eabb9961a811602c4d6d9fe3cbc945b56b5c60be7d4def37012009d4a
wall pat02.ppm f871e2992651fc07e2317760b7f6f1a5b1821c472a822bd9371958ebc5c9aef8
wall pat03.ppm 5d04d378b5df7a3c8055d5b480e65e2e32f0d98dd51b65c20824f042b1285f05
wall pat04.ppm bc8eaea7570c7c243a73248f4d103925da15b3346ab269ffd3e7f000d94524ad
wall pat05.ppm 2f19ab2024cee7a3c0ee93d7157458da463e5a2c06e1fae0375609b1dc619a5d
wall pat06.ppm 6f4739cfa8a37249e81a5ef9f3b4bc567339e577c0f7cd44290160b0bdd3a2ba
wall pat07.ppm a5f7c85d3c409f82bcced0ae045853b114eaa73788820ee02694d5b9a2b64bcb
wall pat08.ppm d1a5853ed9c7bd2a7f21655d7df7606c5ed3ee138e2e04857ac2599920dc3d80
wall pat09.ppm 6a689ebe0c83dd13e0ef522089ff37c3a13e4e36c83f60387de2bba0171b5295
wall pat10.ppm 27d99ba906ea52307217e7fa4abd476301964dfff723e9520879dc12f646b470
wall pat11.ppm 9d780ccaa0cb2d681eea340038e9074259b6ba42e47e59f657375e29472f8ffb
wall pat12.ppm 5dc278fc87e0104546ee5dcb561b0b4fc8f4b3e178149f8173e2f5b09e1127c0
wall pat13.ppm 731f26ce6de33fdd9da6f9bd42b47db258a64696fd0b80c18d8ef32958e1e424
wall pat14.ppm 2840eefec88e4801dd5ba8ed265b1113de780ec1b32dfc010a54021581abc575
wall pat15.ppm 67bc36cefb4802a2e99535d505390be88f9f1006bf22344090eeb094b85cbc69
wall pat16.ppm d1cbae5180b2e2fa3654ef2eb286e5fc6fd1b64f004d08da572c02190e941f5b
wall pat17.ppm 0825d15a7296ed6856c6f7099627f23509be788d2b063f33cb307d95988bb97e
wall pat18.ppm 39787f821b8d6ec353b9259aa560515834a040bc7620eb90a26c05fe99697b88
wall pat19.ppm a9ac42cd54ca8238257dd53fcb1ed05fda6006c12f214f2cfa2622a6c934b2d7
wall pat20.ppm 59ec0c8549203930cf3005b0de8ab09d44d8ba812fb90755130424761f3daf51
wall pat00.ppm 7f6c0967e1e4737feb51fc116a89310097862896d282cc709fb85529b0c9153d
LIST
if [ -n "$failed" ]; then
    echo "bubbros-nonfree-installer: could not download$failed." >&2
    echo "Check the network connection, then run" >&2
    echo "  dpkg-reconfigure bubbros-nonfree-installer" >&2
    exit 1
fi
