#!/bin/bash
#
# Total up list crushes
# Hack hack
# Note below about uniq.
#
# <honey@missprint.org>

CRUSHLIST="/home/missprint/crush/crushes"
LOG="/home/missprint/crush/log"
MONTH=`date +%m-%Y`

# See if only display top X crushees; if display all, don't display 
# postamble
if [ "$QUERY_STRING" = "" ]
then
 POSTAMBLE=
 TOPCRUSH="cat -"
 TOPWORDS=
else
 POSTAMBLE=yesplease
 TOPCRUSH="head -$QUERY_STRING"
 TOPWORDS="Top $QUERY_STRING"
# Override for now (sigh)
# TOPCRUSH=
# TOPWORDS=
fi

cat <<EOF
Content-type: text/html

<HTML>
<HEAD>
<TITLE>Sinister List Crushes</TITLE>
<link rel=stylesheet
href="/sinister/sinister.css" type=
"text/css"
      media=screen title="sinister">
</HEAD>

<BODY
BACKGROUND="/sinister/lazy-crop-circle.jpg"
NOSAVE
>
<H1><FONT COLOR="#993399">
<IMG SRC="/sinister/joyful.gif">&nbsp;
Sinister: Are You Lonesome Tonight?
</FONT></H1>

<!-- Page header -->

<H2>Current $TOPWORDS List Crushes</H2>

EOF

if [ "$POSTAMBLE" ] 
then
 echo "<A HREF=\"`basename $0`\"><strong>See all votes</strong></A>"
else
 echo "<A HREF=\"`basename $0?10`\">See top 10 votes and full instructions</A>"
 echo "<A HREF=\"`basename $0?5`\">(top 5)</A>"
 echo "<A HREF=\"`basename $0?20`\">(top 20)</A>"
fi

cat <<EOF
<p>
Here's the latest results of who's being crushed on - for all time
and for just this month.  Mmmmm.

<TABLE BORDER=0 CELLSPACING=30>
<TR> <TD VALIGN=TOP>

<TABLE BORDER=1 CELLSPACING=5>
<TR> <TD ALIGN=CENTER><B>Num of votes</B></TD> <TD><B>$TOPWORDS Crushees</B></TD>
EOF

# Haven't a CLUE why I need to have my own uniq binary
# Crushes of all time
awk '{print $2}' $CRUSHLIST | sort | uniq -c \
 | sort -k1,1rn -k2,2 | $TOPCRUSH \
 | awk '{printf("<TR> <TD ALIGN=CENTER>%d</TD> <TD>%s</TD>\n",$1,$2)}'

cat <<EOF
</TABLE>
</TD><TD VALIGN=TOP>
<TABLE BORDER=1 CELLSPACING=5>
<TR> <TD ALIGN=CENTER><B>Votes this month</B></TD> <TD><B>$TOPWORDS Crushees</B></TD>
EOF

# Crushes this month
awk "/$MONTH"'\ *\**$/{print $2}' $CRUSHLIST | sort | uniq -c \
 | sort -k1,1rn -k2,2 | $TOPCRUSH \
 | awk '{printf("<TR> <TD ALIGN=CENTER>%d</TD> <TD>%s</TD>\n",$1,$2)}'

cat <<EOF
</TABLE>
</TD></TR></TABLE>

The last 3 crush votes were for:<BR>
<STRONG>
EOF

tail -3 $CRUSHLIST | awk '{printf("%s<br>",$2)}'

cat <<EOF
</STRONG><P>

In all, Miss Crush has <STRONG>matched</STRONG> a total of <STRONG>
EOF

grep MATCH $LOG | wc -l | awk '{print $1}'

cat <<EOF
</STRONG> listees who voted for <STRONG>each other</STRONG>.
Let the love flow.<P>

<A HREF="/cgi-bin/photo.cgi?gallery=things&photo=MissCrush">Peek at Miss Crush</A><BR>


EOF

if [ "$POSTAMBLE" ] 
then
 echo "<A HREF=\"`basename $0`\"><strong>See all votes</strong></A>"
else
 echo "<A HREF=\"`basename $0?10`\">See top 10 votes and full instructions</A>"
 echo "<A HREF=\"`basename $0?5`\">(top 5)</A>"
 echo "<A HREF=\"`basename $0?20`\">(top 20)</A>"
fi

cat <<EOF
<H2>How to register a crush</H2>

<P>
Here's the quick explanation:

<OL>
<LI> Send the command <STRONG>register</STRONG> in the body of an email. 
to <A HREF="mailto:misscrush@missprint.org?Body=register">misscrush@missprint.org</A>
</LI>

<LI>When I send you a code back, send me the command:<BR>

<STRONG>crush <I>code email-address-of-your-crush</I></STRONG>
</LI>
</OL>

<P>
Note also that if you've just forgotten your code, you can send the
<STRONG>register</STRONG> command again and I'll send it back.

<P>
If you need to check for your own or your crush's email address, go
to the bottom of this page.

EOF

if [ "$POSTAMBLE" ]
then
 cat <<EOF
Read on for a better explanation and some rules.

<H2>Eh? List Crush?</H2>

<p>
Hello, 
<a href="/cgi-bin/photo.cgi?gallery=things&photo=MissCrush">Miss Crush</a> 
here, an employee at 
<a href="/cgi-bin/photo.cgi?gallery=things&photo=MissPrint">Miss Print's</a> 
Printing House.  
I run the list 
crush for those randy little devils on the Sinister list.  The list
crush was invented by 
<A HREF="mailto:skg21@hermes.cam.ac.uk">Big Stu</A> 
a long time ago so that people on the
list could vote anonymously for their latest object of fascination or
desire on the list.  I'm very please to be announce that it's back,
less fraudulent, and a bit more cheeky.

<P>
You're supposed to vote for someone based
on the crush-worthiness of their words, so no sneaking over to the
<A HREF="/cgi-bin/photo.cgi?gallery=people">
photo gallery</A> before you vote, or, god forbid, the
<A HREF="/cgi-bin/photo.cgi?gallery=bodyparts">
bodyparts page</A>.

<P>
The list crush now runs under strict BPI rules to prevent fraudulent
use and uncrushworthy behaviour.
To vote you need to do this via email, so I can check if you're
cheating or voting for your hamster, and you vote for a list member
by their email addresses.  I'm very strict, but discipline
can be sexy.  Both you and your crush must be on the list and not in
the nursery.  You also can only vote for one person at a time.  You
can however change your mind and replace your current obsession with
someone new, but you can only change your vote
once a day.  To find out how to register your own vote, mail me at
<A HREF="mailto:misscrush@missprint.org?Body=help">misscrush@missprint.org</A> 
with the word
<strong>help</strong> in the <strong>body</strong> of your mail message,
first line (i.e. not the subject field).  

<P>
When you're ready to vote, mail 
<A HREF="mailto:misscrush@missprint.org?Body=register">me again</A> 
with the word <strong>register</strong> in the same way.  I send you
back a special secret service code, which you return to me with your
vote by saying:

<P>
<strong>crush <I>code email-address-of-your-crush</I></strong> 

<P>
to me.  If you ever forget your
code, just send me <strong>register</strong> again.  It's not hard,
honestly.

<P>
If you don't know someone's full email address you can search for it
below, as long as you know a part of it.

<P>
Here's the good bit.  Your votes always stay anonymous, except in the
following case:
if you vote for someone who's voted for you already, then I make a
match, draw a big ring round your names in lipstick, and mail you
both telling you you're matched.  It's ok, no-one else will know :)
You then blush wildly, mail each
other, loosen your clothing and get stuck in.

<P>
By the way, I'm still being tested: I'm a new employee.  If something
goes wrong and you've read all my help instructions and still can't
figure it out, mail <a href="mailto:honey@missprint.org">Honey</A>.
EOF
fi

# Search for an email address
cat <<EOF
<H2>Search for an email address</H2>

<p>
Use this to look for an email address subscribed to the list. You can
enter any search term and it looks for any addresses containing
whatever you want. 
<form method="GET"
      action="listee.cgi"
      enctype="application/x-www-form-urlencoded">
<input type="text" value="$QUERY" name="query" size=20>
<input type="submit" name="Search"
    value="Search">
</form>
EOF

cat <<EOF
<!-- Page footer -->
<P>
<HR WIDTH="100%">
<CENTER><B>Click to go...&nbsp;<A
HREF="/sinister">
<IMG SRC="/sinister/back.gif" 
 ALT="Back" BORDER=1 HEIGHT=57 WIDTH=95 ALIGN=ABSCENTER></A>
&nbsp;to the main page</B><BR>
<IMG SRC="/sinister/waitress-hand.gif" 
 HEIGHT=41 WIDTH=42 ALIGN=ABSCENTER><B>
Table Service by <A
HREF="/sinister/shanta.html">Honey
</A></B></CENTER>
</BODY>
</HTML>
EOF

exit
