#!/bin/bash

# Generated by claude.ai

TAG="$1"
REPO="$2"
IS_BETA="${3:-false}"

TAG_NO_V="${TAG#v}"
VERSION="${TAG_NO_V%%[+-]*}"
SUFFIX=$([ "$IS_BETA" = "true" ] && echo "-beta" || echo "")
PREFIXRAW="${VERSION}${SUFFIX}"
PREFIX="v${PREFIXRAW}"

ENCODED_TAG="${TAG//+/%2B}"
BASE_URL="https://github.com/${REPO}/releases/download/${ENCODED_TAG}"

link() {
  local label="$1"
  local filename="$2"
  printf '<a href="%s/%s"><b><code>%s</code></b></a>' "$BASE_URL" "$filename" "$label"
}

bullets() {
  printf '<ul style="list-style:none; padding:0; margin:0; text-align:left;">\n'
  for entry in "$@"; do
    IFS='|' read -r label filename <<< "$entry"
    printf '  <li>%s</li>\n' "$(link "$label" "$filename")"
  done
  printf '</ul>'
}

row() {
  local platform="$1"
  shift
  printf '<tr>\n'
  printf '  <td><b>%s</b></td>\n' "$platform"
  printf '  <td>%s</td>\n' "$(bullets "$@")"
  printf '</tr>\n'
}

cat << EOF

---

<div>
<table>
<tr>
  <th>Platform</th>
  <th>Downloads</th>
</tr>
$(row "Android" \
  "${PREFIX}-arm64-v8a.apk|namida-${PREFIX}-arm64-v8a.apk" \
  "${PREFIX}-armeabi-v7a.apk|namida-${PREFIX}-armeabi-v7a.apk")
EOF

if [ "$IS_BETA" = "true" ]; then
  row "Android (Clone)" \
    "${PREFIX}-clone-arm64-v8a.apk|namida-${PREFIX}-clone-arm64-v8a.apk" \
    "${PREFIX}-clone-armeabi-v7a.apk|namida-${PREFIX}-clone-armeabi-v7a.apk"
fi

row "Windows" \
  "${PREFIX}-Installer-x86_64.exe|Namida-x86_64-${PREFIXRAW}-Installer.exe" \
  "${PREFIX}-Portable-x86_64.zip|Namida-x86_64-${PREFIXRAW}-Portable.zip"

if [ "$IS_BETA" = "true" ]; then
  row "Linux" \
    "${PREFIX}.linux.tar.gz|namida-${PREFIX}.linux.tar.gz" \
    "${PREFIX}.linux.deb|namida-${PREFIX}.linux.deb" \
    "${PREFIX}.linux.rpm|namida-${PREFIX}.linux.rpm"
  row "Linux (Login)" \
    "${PREFIX}_login.linux.tar.gz|namida-${PREFIX}_login.linux.tar.gz" \
    "${PREFIX}_login.linux.deb|namida-${PREFIX}_login.linux.deb" \
    "${PREFIX}_login.linux.rpm|namida-${PREFIX}_login.linux.rpm"
fi

cat << EOF
</table>
</div>

EOF