# lib/setup - first-boot identity setup
#
# Self-contained on purpose: it talks to /dev/factotum/ctl directly rather
# than calling the `id` script.  zrc cannot reliably source one script from
# inside another -- the inner script's positional args ($1) and locals don't
# propagate, so `id gen` invoked from here mis-fires.  The two ctl writes
# below (set handle, generate key) are the same thing `id` does, inline.
# (`id` remains the command users run interactively to manage identity.)

# Already set up?  Don't clobber an existing key.
if (test -e /dev/factotum/key) {
    echo ''
    echo 'You already have an identity:'
    cat /dev/factotum/ctl
    echo ''
    echo 'Use id to manage it (id del to start over).'
    exit 0
}

echo ''
echo '=== Identity Setup ==='
echo ''
echo 'You need a handle and a cryptographic address'
echo '(CGA) to participate on Aether networks.'
echo ''
echo 'Your CGA proves you are you, without needing'
echo 'a central authority. It is tied to your handle.'
echo ''
echo 'Your handle can be anything -- a nickname, your'
echo 'real name, or something anonymous. Some Aether'
echo 'communities prefer real names. Choose whatever'
echo 'feels right for you.'
echo ''
echo 'Choose a handle (letters/numbers, no spaces):'
handle=frst
read handle
if (test -z $handle) { handle=frst }

# Mint: set the handle, then generate the keypair.  Both are plain top-level
# ctl writes -- no nested script sourcing, no multi-line nesting.
echo 'user '$handle > /dev/factotum/ctl
echo genkey > /dev/factotum/ctl

echo ''
echo 'Your identity is ready:'
cat /dev/factotum/ctl
echo ''
echo 'Type id anytime to see it.'
echo ''
