#!/bin/ksh
# (c)1993 [email protected]
# 2003 some modifications, but I don't want to go too far.
# 2007 little things, I really don't want to go too far..
# 2010 moved Type into a func, added Forward and Back commands.
# 2013 customized a bit for LCM, hotkeys, posting confirm, delete old code
#      replaced with less crappy code.
#
post=`head -5 /usr/news/.bboard|tail -1|cut -d\[ -f2|cut -d\] -f1`

Getchar(){
stty raw -echo
dd count1 2>/dev/null
stty -raw echo
}

Num(){
case $1 in
  ?) echo "000$1" ;;
 ??) echo "00$1" ;;
???) echo "0$1" ;;
????) echo "$1" ;;
esac
}

wc=`echo ${LOGNAME}|wc -c|awk '{print $1}'`
case $wc in
2) ws="       " ;;
3) ws="      " ;;
4) ws="     " ;;
5) ws="    " ;;
6) ws="   " ;;
7) ws="  " ;;
8) ws=" " ;;
9) ws="" ;;
esac


Type(){
post=`Num ${1}`
if [ "$post" = "" ]
then echo "\nUsage: TYPE <n>\n"
else file=`awk '{print $2}' /usr/news/.posts/${post}`
     if [ "${file}" = "" ] || [ ! -f /usr/news/${file} ]
     then echo "\n?No such bulletin\n"
          return 22
     else echo
          pg -ce /usr/news/${file}
          echo
     fi
fi
}

Forward(){
post=`expr ${post} + 1`
post=`Num ${post}`
Type ${post}
if [ "$?" = "22" ]
then post=`expr ${post} - 1`
    post=`Num ${post}`
fi
}

Back(){
if [ "${post}" = "0000" ]
then echo "\n?At the beginning\n"
    return 0
fi
post=`expr ${post} - 1`
post=`Num ${post}`
Type ${post}
}

#echo "lcm3b2 Bulletin Board\n"
#echo "[id] [Time Stamp]  [Subject/Tacker]"
#echo "----------------------------------------------------------"
pg -ce /usr/news/.bboard
echo
while true
do
echo "[${post}] Command: \c"
command=`getchar`
case $command in

    h*|H*) echo "\n  *"
           echo "  *  BBOARD COMMANDS - (c)1993 [email protected]"
           echo "  *"
           echo "  *  [L]IST      List bulletin summaries"
           echo "  *  [T]YPE <n>  Displays the selected bulletin"
           echo "  *  [F]ORWARD   Displays bulletins forward"
           echo "  *  [B]ACK      Displays bulletins backward"
           echo "  *  [P]OST      Post a new bulletin"
           #echo "  *  [R]EMOVE <n> - Remove a posted bulletin"
           echo "  *  [Q]UIT      Quit to the shell"
           echo "  *";;
    l*|L*) #echo "[id] [Time Stamp]  [Subject/Tacker]"
           #echo "----------------------------------------------------------"
           pg -ce /usr/news/.bboard;echo;;
    p*|P*) echo "POST"
           fullname=`grep ^$LOGNAME: /etc/passwd|cut -d: -f5`
           #file="`date +%e%b.%H:%M:%S|awk '{print $1}'`"
           file="`date +%d%m%y_%H%M%S`"
           echo "\nSubject: \c"
           read subject
           echo "Tacker:   $LOGNAME ($fullname)" > /tmp/$file
           echo "Date:     `date +\"%m/%d/%y %H:%M:%S\"" >> /tmp/$file
           echo "Subject:  $subject\n" >> /tmp/$file
           echo "\n(CTRL-D to end message)\n"
           cat >> /tmp/$file
           pg -ce /tmp/${file}
           echo "\nPost this message? (y/n) \c"
           case `getchar` in
           y|Y) echo "YES"
                mv /tmp/$file /usr/news/$file
                echo "\nUpdating summary tables..."
                touch /usr/news/.update 2>/dev/null
                lastpost=`ls /usr/news/.posts|tail -1`
                newpost=`expr ${lastpost} + 1`
                newpost=`Num ${newpost}`
                echo "[${newpost}]  ${file}   (${LOGNAME})${ws}  `echo ${subject}|cut -c1-44`" > /usr/news/.posts/${newpost}
                echo "Bulletin queued! (will post in about 2 minutes)\n"
                ;;
             *) echo "NO"
                rm -f /tmp/${file}
                echo "\n% POSTing aborted.\n"
                ;;
           esac ;;
# rem*|REM*) post=`echo $command|awk '{print $2}'`
#            if [ "$post" = "" ]
#             then echo "\nUsage: REMOVE <n>\n"
#             else file=`grep "\[$post\]" /usr/news/.bboard|awk '{print $2}'`
#                  rm -rf /usr/news/$file
#                  echo "\nBulletin removed\nUpdating summary tables..."
#                  summary
#                  echo "Done!\n"
#            fi;;
    t*|T*) echo "TYPE (POST) \c"
           read post
           Type ${post} ;;
        b*|B*) echo "BACKWARD"
               Back ;;
        f*|F*) echo "FORWARD"
               Forward ;;
    q*|Q*) echo "QUIT"
           exit 0 ;;
        *) echo "\n\nType H for Commands.\n"
esac
done