# bbs/post - post a message to a room
# usage: post room subject
#   Type message interactively, or pipe it in.
#
# examples:
#   post lobby Greetings              type interactively, ^D to send
#   echo 'Hello!' | post lobby Hi    pipe message

if (test $#* -lt 2) {
    echo 'usage: post room subject'
    echo ''
    echo 'examples:'
    echo '  post lobby Greetings              type, ^D to send'
    echo "  echo 'Hello!' | post lobby Hi     pipe message"
    echo "  cat draft.txt | post lobby Title  from file"
    exit 1
}

bbs=/n/bbs

if (test ! -d $bbs) {
    echo 'BBS not connected. Run: bbs/connect'
    exit 1
}

room=$1
# Build subject from $2 onwards (no shift available)
subject=$2
if (test -n $3) { subject=$2' '$3 }
if (test -n $4) { subject=$2' '$3' '$4 }
if (test -n $5) { subject=$2' '$3' '$4' '$5 }

echo 'Posting to '$room': '$subject
echo 'Type your message. ^D to send.'
echo ''

# Build message in temp file, then write to server
tmp=/n/local/_post.tmp
echo 'From: '$user > $tmp
echo 'Subject: '$subject >> $tmp
echo 'X-Client: cyberterminal/1.0' >> $tmp
echo '' >> $tmp
while (read line) {
    echo $line >> $tmp
}
cat $tmp > $bbs/rooms/$room/new
rm $tmp

echo ''
echo 'Posted to' $room
