#!/usr/bin/env bash
set -uo pipefail
cd /root/switch

SW_TOKEN=$(awk -F'"' '/^SW_TOKEN=/{print $2; exit}' install-switch.sh)
echo "token: len=${#SW_TOKEN} head=${SW_TOKEN:0:12}"

mkdir -p /etc/apt/auth.conf.d
printf 'machine freeswitch.signalwire.com login signalwire password %s\n' "$SW_TOKEN" \
  > /etc/apt/auth.conf.d/freeswitch.conf
chmod 600 /etc/apt/auth.conf.d/freeswitch.conf

U=https://freeswitch.signalwire.com/repo/deb/debian-release/dists/bookworm/Release
curl -s -o /dev/null -w 'A full-token      : %{http_code}\n' -u "signalwire:$SW_TOKEN" "$U"
curl -s -o /dev/null -w 'B token-no-prefix : %{http_code}\n' -u "signalwire:${SW_TOKEN#swpat_}" "$U"
curl -s -o /dev/null -w 'C user-is-token   : %{http_code}\n' -u "$SW_TOKEN:" "$U"
curl -s -o /dev/null -w 'D no-auth         : %{http_code}\n' "$U"

cp -f install-switch.sh install-switch.sh.orig
sed -i -E 's#/debian-release/ \$\([A-Za-z_]+ -sc\) main#/debian-release/ bookworm main#g; s#/debian-release/ `[A-Za-z_]+ -sc` main#/debian-release/ bookworm main#g' install-switch.sh
chmod 755 install-switch.sh
rm -f install-switch.shxgxch
echo "--- repo line now:"
grep -n 'debian-release/' install-switch.sh
