# bbs/connect - connect to BBS server
# usage: connect [dialstring]
#
# Default: l2cap!?!0 (BLE scan for nearby BBS)
# Examples:
#   bbs/connect                         - scan BLE
#   bbs/connect tcp!localhost!9777      - TCP server
#   bbs/connect l2cap!AA:BB:CC:DD:EE!0 - specific BLE device

bbs=/n/bbs

if (test -d $bbs) {
    echo 'Already connected to BBS'
    exit 0
}

dial=$1
if (test -z $dial) { dial=l2cap!?!0 }

echo 'Connecting to' $dial '...'

# TCP aetherd exports BBS directly at root — no aname needed.
# L2CAP/Aether nodes export their full namespace — BBS lives
# at srv/bbs, so we pass it as aname to attach there directly.
if (~ $dial tcp!*) {
    srv $dial bbs
}
if not {
    srv $dial bbs srv/bbs
}

if (test $status -ne 0) {
    echo 'Connection failed'
    exit 1
}

mount /srv/bbs $bbs

if (test -d $bbs) {
    echo 'Connected to BBS at' $bbs
    echo ''
    echo 'Try: info, rooms, help'
}
if not {
    echo 'Mount failed'
    exit 1
}
