#!/bin/bash




echo Content-Type: text/html
echo
HOSTNAME=$( hostname )

CURRLOCATION=$( echo "$REQUEST_URI" | sed -n 's/^.*DIR=\([^&]*\).*$/\1/p' | sed s/+/\ /g )



if [ "$CURRLOCATION" == "" ]
then


PRINTLOCAT="/"
else
PRINTLOCAT="$CURRLOCATION"

fi

WEBDIR="/var/www/localhost/htdocs/Library"


# Work out what the parent directory is

cd "$WEBDIR/$CURRLOCATION"
cd ..
pwd > /tmp/dir.parent.1
sed s/"\/var\/www\/localhost\/htdocs\/Library\/"//g /tmp/dir.parent.1 > /tmp/dir.parent.2
PARENTDIR=$( cat /tmp/dir.parent.2 )

# We can't return to the root directory without some extra work!

if [ "$PARENTDIR" == "/var/www/localhost/htdocs/Library" ]
then
PARENTDIR="/"
fi

# We also get errors if we are already in the root directory

if [ "$PARENTDIR" == "/var/www/localhost/htdocs" ]
then
SHOWPARENT="N"
fi


# Return to the temporary directory

cd /tmp





ls -l "/var/www/localhost/htdocs/Library/$CURRLOCATION" > /tmp/dir.list.test2
LINECOUNT=$( wc -l /tmp/dir.list.test2 | awk -F\  '{print $1}' )
RETRIEVECOUNT=$(( $LINECOUNT - 1 ))
tail -n $RETRIEVECOUNT /tmp/dir.list.test2 > /tmp/dir.list.test

/bin/cat << EOM
<html>
<head>
<title>Document Library - Current Location $PRINTLOCAT</title>
</head>
<body bgcolor="white">
<b><center>Document Library</center></b>
<br><br>

EOM

# Check whether the Database is being updated in the background

if [ -e Database_library.updating.lock ]
then
# Print a warning

/bin/cat << EOM
<b><font size="+2" color="red">Warning: Search Database Updating - System may run slowly until complete</font></b>
<br><br>

EOM

fi


/bin/cat << EOM
<b><font size="+2">Current Location: $PRINTLOCAT</b></font>
<br><br>

<!-- Navigation Bar -->
<hr>
<table
style="margin-left: auto; margin-right: auto; background-color: rgb(204, 204, 255); width: 100%; text-align: center;"
border="0" cellpadding="2" cellspacing="2">
<tr>

<td>
<a href="ftp://doclibrary@$HOSTNAME/var/www/localhost/htdocs/Library/$CURRLOCATION" target=_blank>Upload New File Here</a>
</td>

<td>
Search Document Library
</td>

<td>
<A href="/Resources.html">Useful Resources</a>
</td>

<td>
<a href="Help.html">Help</a>
</td>
</tr>
</table>
<hr>
<br><br>
EOM






if [ "$SHOWPARENT" != "N" ]
then
/bin/cat << EOM
<table width="100%">
<tr>
<td>
<a href="Index.shtml?DIR=$PARENTDIR" title="Move up one directory">Parent Directory</a>
</td>
<td>
<a href="Index.shtml?DIR=/" title="Return to the Document Library Frontpage">Root Directory</a><br>
</td>
</tr>
</table>
<br><br>
EOM
fi

/bin/cat << EOM
<table>
<tr>
<td></td>
<td><b>Location</b></td>
<td><br><br></td>
<td><b><center>Size</center></size></td>
<td><br></td>
<td><b><center>Last Updated</center></b></td>
</tr>
EOM

ITEMCOUNT="0"

while read -r a
do

ITEMCOUNT=$(( $ITEMCOUNT + 1 ))
echo "$a" > /tmp/dir.list.test1
LOCATION=$( awk -F\  '{print $9}' /tmp/dir.list.test1 )
UPDATED1=$( awk -F\  '{print $6}' /tmp/dir.list.test1 )
UPDATED2=$( awk -F\  '{print $7}' /tmp/dir.list.test1 )
UPDATED3=$( awk -F\  '{print $8}' /tmp/dir.list.test1 )
UPDATED="$UPDATED1 $UPDATED2 $UPDATED3"
SIZE=$( awk -F\  '{print $5}' /tmp/dir.list.test1 )
ATTRIBS=$( awk -F\  '{print $1}' /tmp/dir.list.test1 )


# Need to check if it's a symbolic link, cos if it is it won't work as expected

if [ -h $LOCATION ]
then
DIR_CHECK="d"
else
# Check if it's a proper directory

DIR_CHECK=$( echo ${ATTRIBS:0:1} )

fi




if [ "$DIR_CHECK" == "d" ]
then

# It's a Directory, use a custom link
/bin/cat << EOM
<tr>
<td>
<center>
<img src="/Icons/directory-icon.png" width=16 height=16 alt="Directory" title="Directory">
</td>

<td>

<a href="Index.shtml?DIR=$CURRLOCATION/$LOCATION">$LOCATION</a>

</td>
<td>
</td>
<td><center>
$SIZE
</center>
</td><td></td>
<Td><center>
$UPDATED
</center>
</td>
</tr>



EOM

else

# It's a file of some form, use an ordinary link
# Use an Icon based on filetype
KNOWN="N"
LINKY="N"

echo "$LOCATION" | grep -i [.]pdf > /dev/null
if [ "$?" == "0" ]
then
ICONTEXT="adobereader.jpg"
ICONTITLE="Adobe PDF Format"
KNOWN="Y"
fi


echo "$LOCATION" | grep -i [.]txt > /dev/null
if [ "$?" == "0" ]
then
ICONTEXT="Text-Icon.png"
ICONTITLE="Standard Text File"
KNOWN="Y"
fi


echo "$LOCATION" | grep -i [.]jpg > /dev/null
if [ "$?" == "0" ]
then
ICONTEXT="JPEG.bmp"
ICONTITLE="JPEG Image File"
KNOWN="Y"
fi
echo "$LOCATION" | grep -i [.]png > /dev/null
if [ "$?" == "0" ]
then
ICONTEXT="JPEG.bmp"
ICONTITLE="Image File"
KNOWN="Y"
fi

echo "$LOCATION" | grep -i [.]pnm > /dev/null
if [ "$?" == "0" ]
then
ICONTEXT="JPEG.bmp"
ICONTITLE="Image File"
KNOWN="Y"
fi



echo "$LOCATION" | grep -i [.]gif > /dev/null
if [ "$?" == "0" ]
then
ICONTEXT="JPEG.bmp"
ICONTITLE="Image File"
KNOWN="Y"
fi


echo "$LOCATION" | grep -i [.]odt > /dev/null
if [ "$?" == "0" ]
then
ICONTEXT="ODF.png"
ICONTITLE="Open Document Format File"
KNOWN="Y"
fi


echo "$LOCATION" | grep -i [.]ods > /dev/null
if [ "$?" == "0" ]
then
ICONTEXT="ODF.png"
ICONTITLE="Open Document Format File"
KNOWN="Y"
fi

echo "$LOCATION" | grep -i [.]odg > /dev/null
if [ "$?" == "0" ]
then
ICONTEXT="ODF.png"
ICONTITLE="Open Document Format File"
KNOWN="Y"
fi

echo "$LOCATION" | grep -i [.]doc > /dev/null
if [ "$?" == "0" ]
then
ICONTEXT="Office-icon.png"
ICONTITLE="Microsoft Office Format File"
KNOWN="Y"
fi

echo "$LOCATION" | grep -i [.]xls > /dev/null
if [ "$?" == "0" ]
then
ICONTEXT="Office-icon.png"
ICONTITLE="Microsoft Office Format File"
KNOWN="Y"
fi

echo "$LOCATION" | grep -i [.]ppt > /dev/null
if [ "$?" == "0" ]
then
ICONTEXT="Office-icon.png"
ICONTITLE="Microsoft Office Format File"
KNOWN="Y"
fi

echo "$LOCATION" | grep -i [.]mdb > /dev/null
if [ "$?" == "0" ]
then
ICONTEXT="Office-icon.png"
ICONTITLE="Microsoft Office Format File"
KNOWN="Y"
fi

echo "$LOCATION" | grep -i [.]rtf > /dev/null
if [ "$?" == "0" ]
then
ICONTEXT="Office-icon.png"
ICONTITLE="Microsoft Office Rich Text Format File"
KNOWN="Y"
fi

echo "$LOCATION" | grep -i [.]sh > /dev/null
if [ "$?" == "0" ]
then
ICONTEXT="shellscript-icon.png"
ICONTITLE="Shell Script"
KNOWN="Y"
fi


echo "$LOCATION" | grep -i [.]html > /dev/null
if [ "$?" == "0" ]
then
ICONTEXT="html-icon.png"
ICONTITLE="HTML Web Page"
KNOWN="Y"
fi

echo "$LOCATION" | grep -i [.]zip > /dev/null
if [ "$?" == "0" ]
then
ICONTEXT="Archive-icon.png"
ICONTITLE="ZIP File Archive"
KNOWN="Y"
fi


echo "$LOCATION" | grep -i [.]gz > /dev/null
if [ "$?" == "0" ]
then
ICONTEXT="Archive-icon.png"
ICONTITLE="Gzipped File"
KNOWN="Y"
fi



echo "$LOCATION" | grep -i [.]tar > /dev/null
if [ "$?" == "0" ]
then
ICONTEXT="Archive-icon.png"
ICONTITLE="Tarball Archive"
KNOWN="Y"
fi


echo "$LOCATION" | grep -i [.]bz > /dev/null
if [ "$?" == "0" ]
then
ICONTEXT="Archive-icon.png"
ICONTITLE="Bzipped File"
KNOWN="Y"
fi


echo "$LOCATION" | grep -i [.]tar[.]gz > /dev/null
if [ "$?" == "0" ]
then
ICONTEXT="Archive-icon.png"
ICONTITLE="Gzipped Tarball Archive"
KNOWN="Y"
fi

echo "$LOCATION" | grep -i [.]tar[.]bz > /dev/null
if [ "$?" == "0" ]
then
ICONTEXT="Archive-icon.png"
ICONTITLE="Bzipped Tarball Archive"
KNOWN="Y"
fi

echo "$LOCATION" | grep -i [.]msg > /dev/null
if [ "$?" == "0" ]
then
ICONTEXT="email_icon.jpg"
ICONTITLE="Exported E-mail"
KNOWN="Y"
fi


echo "$LOCATION" | grep -i [.]url > /dev/null
if [ "$?" == "0" ]
then
ICONTEXT="external_link.png"
ICONTITLE="Link to another location"
KNOWN="Y"
LINKY="Y"
fi

if [ "$KNOWN" != "Y" ]
then
# Couldn't Identify the filetype
ICONTEXT="icon_unknown.png"
ICONTITLE="Couldn't Identify Format"
fi


# If the item is a .url file we need to process slightly differently


/bin/cat << EOM
<tr>
<td>
<center>
<img src="/Icons/$ICONTEXT" width=16 height=16 title="$ICONTITLE" alt="$ICONTITLE">
</center>
</td>
EOM

if [ "$LINKY" != "Y" ]
then

/bin/cat << EOM
<td>
<a href="/Library/$CURRLOCATION/$LOCATION" target=_blank title="Click to Download">$LOCATION</a>

</td>
<td>
</td>
<td><center>
$SIZE
</center>
</td><td></td>
<Td><center>
$UPDATED
</center>
</td>
</tr>



EOM



else

# Link should go to the contents of the file

LINKTARGET=$( cat "$WEBDIR/$CURRLOCATION/$LOCATION" )

/bin/cat << EOM

<td>
<a href="$LINKTARGET" target=_blank title="Click to Open">$LOCATION</a>

</td>
<td>
</td>
<td><center>

</center>
</td><td></td>
<Td><center>
$UPDATED
</center>
</td>
</tr>
EOM

fi


fi

done < /tmp/dir.list.test


/bin/cat << EOM
<tr>
<td><br></td>
</tr>
<tr><td></td>
<td>$ITEMCOUNT Items</td>
</tr>
</table>
<br><br>
<hr>

<table
style="margin-left: auto; margin-right: auto; background-color: rgb(204, 204, 255); width: 100%; text-align: center;"
border="0" cellpadding="2" cellspacing="2">
<tr>

<td>
Admin
</td>

<td>

</td>

<td>

</td>

<td>

</td>
</tr>
</table>
<hr>

<br><br><b>
Document Library Copyright 2010 Ben Tasker<br>
Released under GNU GPL V3<br>
See <a href="http://benscomputer.no-ip.org/LICENSE3" target=_blank>License</a> for details</b>
<br><br>
</body>
</html>
EOM


# Tidy up
rm -f /tmp/dir.list.test 2> /dev/null
rm -f /tmp/dir.list.test1 2> /dev/null
rm -f /tmp/dir.list.test2 2> /dev/null
rm -f /tmp/dir.parent.1 2> /dev/null
rm -f /tmp/dir.parent.2 2> /dev/null


exit