#!/bin/sh
# Korak carobnjaka nakon upisa u organizaciju: token DeChaosa iz upisa ide
# korisniku u ~/.config/tossos/dechaos.token, a widget ga preuzme pri prijavi.
set -eu
U=/run/tossos/upis.json
[ -r "$U" ] || exit 0
TOKEN=$(python3 -c "import json; print(json.load(open('$U')).get('dechaos_token') or '')")
URL=$(python3 -c "import json; print(json.load(open('$U')).get('dechaos_url') or 'https://dechaos.humanitics.eu')")
[ -n "$TOKEN" ] || { echo "upis nema DeChaos token, widget ostaje na oglednim podacima"; exit 0; }
H=$(getent passwd "$TOSSOS_KORISNIK" | cut -d: -f6)
install -d -m 700 -o "$TOSSOS_KORISNIK" -g "$TOSSOS_KORISNIK" "$H/.config" "$H/.config/tossos"
umask 077
printf '%s\n%s\n' "$URL" "$TOKEN" > "$H/.config/tossos/dechaos.token"
chown "$TOSSOS_KORISNIK:$TOSSOS_KORISNIK" "$H/.config/tossos/dechaos.token"
echo "DeChaos token upisan za $TOSSOS_KORISNIK"
