#!/bin/ksh
# Jordan likes command-line interfaces...
#
# What is this nonsense?
# - It lets me post updates to my homepage from the commandline.
# - The $USER and $PASS variables are sourced from /u9/psionic/mysql_login, so
# if you intend on playing with this script, you'll have to make the
# necessary changes.
#
# Requirements: vim, ggrep, mysql (the client)
# I'll eventually add support for $EDITOR, but ... eh.. I use vim.
#
# Updates
# * [7/8/03]
# - Added "active saving" support. It now runs another copy of post in the
# background that watches the temp file and checks for changes. When new
# changes are detected, it will automatically post to the website.
# Yes, I am indeed pathetic.
#
# * [7/5/03]
# - Added real image and file support. Thumbnails are autogenerated by
# ImageMagick's convert. Simply prefix a line with File: or Image: and
# the necessary actions will be taken. w00t I'm so pathetically sad.
# {{{ Variables...
. /u9/psionic/mysql_login
ECHO=/usr/ucb/echo
MYSQL_DB="psionic"
MYSQL_SERVER="db.csh.rit.edu"
MYSQL_COMMAND="mysql"
MYSQL_ARGS="--user=${USER} --password=${PASS} --host=${MYSQL_SERVER} ${MYSQL_DB}"
MYSQL_SILENT="-s"
CMD="post"
TMP="/tmp/postandstuff"
WEBDIR="/u9/psionic/public_html"
WEBLOC="http://www.csh.rit.edu/~psionic"
# Convert!
CONVERT=/usr/local/bin/convert
CONVERT_ARGS="-size 127x82"
CONVERT_ARGS2="-resize 127x82"
WATCHFILE=
ID=
# }}}
# {{{ Argument Processing
while [ $# != 0 ]; do
case $1 in
--post|-p)
CMD="post"
;;
--edit|-e)
CMD="edit"
;;
--delete|-d)
CMD="delete"
;;
--force|-f)
rm $TMP $TMP.foo $TMP.orig
;;
-w) # You don't need to execute this, it's just jordan being sad.
WATCHFILE=$2
shift
;;
-i) # Don't use this either unless you really need to, eh?
ID=$2
shift
;;
esac
shift
done
#}}}
# {{{ If -w, watch the file...
if [ "$WATCHFILE" ]; then
TMP=${WATCHFILE}
HUP=0
trap "HUP=1" HUP
touch -f ${TMP} ${TMP}.watch
while [ $HUP -le 3 ]; do
if [ "${TMP}" -nt "${TMP}.watch" ]; then
diff $TMP $TMP.orig > /dev/null 2>&1
if [ $? = 1 ]; then
SUBJ=`head -n 1 $TMP | sed -e 's/^Subject: //'`
LINES=`wc -l $TMP | awk '{print $1}'`
LINES=`expr ${LINES} - 1`
BODY=`tail -${LINES} ${TMP} | sed -e 's/\n/\\'`
BODY=`${ECHO} "${BODY}" | sed -e 's/\"/\\\"/g'`
SQL="UPDATE news SET subject=\"${SUBJ}\", content=\"${BODY}\" WHERE id=${ID};"
${ECHO} "${SQL}" | sh -c "${MYSQL_COMMAND} ${MYSQL_ARGS}" > /dev/null 2>&1
cp $TMP $TMP.orig
fi
touch -f "${TMP}" "${TMP}.watch"
fi
if [ $HUP -ge 1 ]; then
HUP=$(($HUP + 1))
fi
done
echo "Done."
exit;
fi
#}}}
if [ $CMD = "post" ]; then
# {{{ Post a new article
${ECHO} "Subject: " > $TMP
${ECHO} >> $TMP
cp $TMP $TMP.orig
vim -n $TMP
head -n 1 $TMP | grep '^Subject: .*' > /dev/null 2>&1
while [ $? -ne 0 ]; do
cp $TMP $TMP.foo
vim -n -c "${ECHO} 'Invalid Subject line format!'" $TMP
diff $TMP $TMP.foo > /dev/null 2>&1
if [ $? -eq 0 ]; then
${ECHO} "Looks like you want to quit..."
exit
fi
head -n 1 $TMP | grep '^Subject: .*' > /dev/null 2>&1
done
diff $TMP $TMP.orig > /dev/null 2>&1
if [ $? -eq 1 ]; then
# We want to to post this onw.
SUBJ=`head -n 1 $TMP | sed -e 's/^Subject: //'`
IML=
rm -f $TMP.file.* 2> /dev/null
ggrep -E '^(File|Image): ' $TMP | sed -e 's/^File: //' | sed -e 's/^Image: //' | split -l 1 - $TMP.file.
#for linkto in `ls $TMP.file.*`; do
for linkto in `cat $TMP.file.* 2> /dev/null`; do
# Verify that this file is indeed under ${WEBDIR}
# < not implemented, jordan is lazy >
if [ "${linkto%%.png}" != "${linkto}" ] || \
[ "${linkto%%.jpg}" != "${linkto}" ]; then
# It's an image. Make a thumbnail and move on.
img=`basename ${linkto}`
dir=`echo $linkto | sed -e "s/${img}\$//"`
thumb="${dir}thumb_${img}"
${CONVERT} ${CONVERT_ARGS} ${WEBDIR}/${linkto} ${CONVERT_ARGS2} ${WEBDIR}/${thumb}
IML="${IML}
"
else
file=`basename ${linkto}`
size=`ls -ld /u9/psionic/public_html/${linkto} | awk '{print $5}'`
IML="${IML}
![]() |
${file}
Size: ${size}B |