#!/bin/bash
#
# TODO: Rewrite this in clisp...
#
cd $(dirname "$0")
BASEDIR="$(pwd)"
quoter=sales@ogamita.com
#sendmail=$BASEDIR/bin/`uname -s`-`uname -m`/mini_sendmail
sendmail=/usr/sbin/sendmail
tmp=/tmp/quote-$$.tmp
log=/tmp/quote-$$.log
if [ -d "$DOCUMENT_ROOT/../logs" ] ; then
    qlog="$DOCUMENT_ROOT/../logs/quote.log"
else
    qlog="$BASEDIR/quote.log"
fi
trap "cat $log>>$qlog;rm -rf $tmp $log" 0


( echo "From quote.cgi  `date`" ;
  echo "X-REMOTE-ADDR: $REMOTE_ADDR"
  echo "X-REMOTE-PORT: $REMOTE_PORT"
  echo "X-REQUEST-METHOD: $REQUEST_METHOD"
  echo "X-HTTP-REFERER: $HTTP_REFERER"
  echo "X-HTTP-USER-AGENT: $HTTP_USER_AGENT"
  echo "X-HTTP-X-FORWARDED-FOR: $HTTP_X_FORWARDED_FOR"
) >> $log

data="$(cat)"
fvs="${data//&/ }"
for fv in $fvs ; do
    f="${fv/=*/}"
    v="${fv/[^=]*=/}"
    #(for i 32 127 (printf " -e 's/%%%02x/%c/g' \\\n" i i))
    v=$(echo $v | sed \
     -e 's/+/ /g' \
     -e 's/%0[Aa]/ /g' \
     -e 's/%0[Dd]/ /g' \
     -e 's/%20/ /g' \
     -e 's/%21/!/g' \
     -e 's/%22/"/g' \
     -e 's/%23/#/g' \
     -e 's/%24/$/g' \
     -e 's/%26/\&/g' \
     -e 's/%27/'"'/g" \
     -e 's/%28/(/g' \
     -e 's/%29/)/g' \
     -e 's/%2[Aa]/*/g' \
     -e 's/%2[Bb]/+/g' \
     -e 's/%2[Cc]/,/g' \
     -e 's/%2[Dd]/-/g' \
     -e 's/%2[Ee]/./g' \
     -e 's/%2[Ff]/\//g' \
     -e 's/%30/0/g' \
     -e 's/%31/1/g' \
     -e 's/%32/2/g' \
     -e 's/%33/3/g' \
     -e 's/%34/4/g' \
     -e 's/%35/5/g' \
     -e 's/%36/6/g' \
     -e 's/%37/7/g' \
     -e 's/%38/8/g' \
     -e 's/%39/9/g' \
     -e 's/%3[Aa]/:/g' \
     -e 's/%3[Bb]/;/g' \
     -e 's/%3[Cc]/</g' \
     -e 's/%3[Dd]/=/g' \
     -e 's/%3[Ee]/>/g' \
     -e 's/%3[Ff]/?/g' \
     -e 's/%40/@/g' \
     -e 's/%41/A/g' \
     -e 's/%42/B/g' \
     -e 's/%43/C/g' \
     -e 's/%44/D/g' \
     -e 's/%45/E/g' \
     -e 's/%46/F/g' \
     -e 's/%47/G/g' \
     -e 's/%48/H/g' \
     -e 's/%49/I/g' \
     -e 's/%4[Aa]/J/g' \
     -e 's/%4[Bb]/K/g' \
     -e 's/%4[Cc]/L/g' \
     -e 's/%4[Dd]/M/g' \
     -e 's/%4[Ee]/N/g' \
     -e 's/%4[Ff]/O/g' \
     -e 's/%50/P/g' \
     -e 's/%51/Q/g' \
     -e 's/%52/R/g' \
     -e 's/%53/S/g' \
     -e 's/%54/T/g' \
     -e 's/%55/U/g' \
     -e 's/%56/V/g' \
     -e 's/%57/W/g' \
     -e 's/%58/X/g' \
     -e 's/%59/Y/g' \
     -e 's/%5[Aa]/Z/g' \
     -e 's/%5[Bb]/[/g' \
     -e 's/%5[Cc]/\\/g' \
     -e 's/%5[Dd]/]/g' \
     -e 's/%5[Ee]/^/g' \
     -e 's/%5[Ff]/_/g' \
     -e 's/%60/`/g' \
     -e 's/%61/a/g' \
     -e 's/%62/b/g' \
     -e 's/%63/c/g' \
     -e 's/%64/d/g' \
     -e 's/%65/e/g' \
     -e 's/%66/f/g' \
     -e 's/%67/g/g' \
     -e 's/%68/h/g' \
     -e 's/%69/i/g' \
     -e 's/%6[Aa]/j/g' \
     -e 's/%6[Bb]/k/g' \
     -e 's/%6[Cc]/l/g' \
     -e 's/%6[Dd]/m/g' \
     -e 's/%6[Ee]/n/g' \
     -e 's/%6[Ff]/o/g' \
     -e 's/%70/p/g' \
     -e 's/%71/q/g' \
     -e 's/%72/r/g' \
     -e 's/%73/s/g' \
     -e 's/%74/t/g' \
     -e 's/%75/u/g' \
     -e 's/%76/v/g' \
     -e 's/%77/w/g' \
     -e 's/%78/x/g' \
     -e 's/%79/y/g' \
     -e 's/%7[Aa]/z/g' \
     -e 's/%7[Bb]/{/g' \
     -e 's/%7[Cc]/|/g' \
     -e 's/%7[Dd]/}/g' \
     -e 's/%7[Ee]/~/g' \
     -e 's/%25/%/g' \
    )
    f=${f//-/_}
    echo "X-QUERY-VARIABLE: $f=$v" >> $log
    eval $f=\"$v\"
done

if [ -z "$CONTACT_PERSON" ] ; then
    CONTACT_PERSON="Anonymous"
fi
if [ -z "$EMAIL" ] ; then
    EMAIL="nobody"
fi


cat >$tmp <<EOF
To:       $quoter
From:     $CONTACT_PERSON <$EMAIL>
Reply-To: $EMAIL
Error-To: $quoter
Subject:  [QUOTE-REQUEST] $COMPANY_NAME / $CONTACT_PERSON


Here is a quote request:

 _______________________________________________________________________

    Company Name:    $COMPANY_NAME

    Contact Person:  $CONTACT_PERSON

    Email:           $EMAIL

    Fax:             $FAX

    Phone:           $PHONE

    Contact Type:    $CONTACT_ME

    Request Type:    $REQUEST_TYPE

    Message:

    $MESSAGE

 -----------------------------------------------------------------------
$(env|sort|sed -e 's/^/ /')
 -----------------------------------------------------------------------
$data
 -----------------------------------------------------------------------


EOF
cat $tmp >> $log
#$sendmail -t -f "$EMAIL" 1>>$log 2>&1 <$tmp
$sendmail -t -f "$EMAIL" 1>>$log 2>&1 <$tmp
sent=$?

echo "Content-Type: text/html"
echo ""

if [ $sent -ne 0 ] ; then
    file=quote-nak
else
    file=quote-ack
fi

terminate () {
    echo '' >> $log
    exit 0
}

# if we have a referer that is a specific language
# we try to answer in the same language.
baseref="$(basename "$HTTP_REFERER")"
case "$baseref" in
quote-*)
    reflang="${baseref/quote-}"
    if [ -r "${file}-${reflang}.html" ] ; then
        if [ -z "$CONTACT_PERSON" ] ; then
            cat "${file}-${reflang}.html"
        else
            sed -e "s/<!--CONTACT-PERSON-->/, $CONTACT_PERSON, /" \
                < "${file}-${reflang}.html"
        fi
        terminate
    fi
    ;;
esac

langs=($(for f in ${HTTP_ACCEPT_LANGUAGE//,/ } ; do echo "$f" ; done | sed -e 's/[;-].*//'))

for lang in "${langs[@]}" en ; do
    if [ -r ${file}.html.${lang} ] ; then
        if [ -z "$CONTACT_PERSON" ] ; then
            cat ${file}.html.${lang}
        else
            sed -e "s/<!--CONTACT-PERSON-->/, $CONTACT_PERSON, /" < ${file}.html.${lang}
        fi
        break
    fi
done
terminate
#### THE END ####
ViewGit