#!/bin/bash

MKJPG="yes"

if [ $MKJPG == "yes" ]; then
 echo "Content-type: image/jpeg"
else
 echo "Content-type: text/html"
fi
echo

#[\/,a-z,A-Z,0-9,.,_]
USERNAME=`echo $PATH_INFO | sed -e "s/\/\~\([^\/]*\).*/\1/"`
if [ $MKJPG != "yes" ]; then
 echo $USERNAME
 echo "<BR>"
fi

YOURHOME=`cat /etc/passwd | grep ^$USERNAME: | cut -d : -f 6`
if [ $MKJPG != "yes" ]; then
 echo $YOURHOME
 echo "<BR>"
fi

SOURCE=$YOURHOME`echo $PATH_INFO | sed -e "s/\/\~[a-z]*/\/public_html/"`
if [ $MKJPG != "yes" ]; then
 echo $SOURCE
 echo "<BR>"
fi

DEST=`echo $SOURCE | sed -e "s/\/[^/]*$/\/.icons&/"`
if [ $MKJPG != "yes" ]; then
 echo $DEST
 echo "<BR>"
fi


FILETYPE=`echo $SOURCE | awk "/\./{print(tolower(gensub(/.*\./, \"\", 1)))}"`
if [ $MKJPG != "yes" ]; then
 echo $FILETYPE
 echo "<BR>"
fi


RAUS=""
case $FILETYPE in
 jpg) FILETYPE=jpeg;;
 "") RAUS=1;;
esac

if [ $RAUS ]
then
 exit
fi

if [ -f $DEST ]
then
 cat $DEST
else
 $FILETYPE"topnm" $SOURCE | pnmscale -xysize 48 48 | ppmtojpeg | tee $DEST
fi
