#!/bin/bash
# YoLinux.com
# Greg Ippolito

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

echo '<html>'
echo '<head>'
echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">'
echo '<title>SVN SVD List Generator</title>'
echo '</head>'
echo '<body>'
echo '<h2>SVN SVD List Generator:</h2>'
echo '(<a href="/cgi-bin/SubversionSVD.cgi">new query</a>)<p><hr><p>'
  echo "<form method=GET action=\"${SCRIPT}\">"

  echo '<input type="radio" name="val_c" value="1" checked> ProjX<br>'\
       '<input type="radio" name="val_c" value="2"> ProjY<br>' \
       '<input type="radio" name="val_c" value="3"> BigProj AppZ<br>' \
       '<input type="radio" name="val_c" value="0"> Other'

  echo '<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SVN URL: <input type="text" name="input_svnurl" size=132 value="http://localhost/svn/ProjX/tags/Tag-Goes-Here/">'

  echo '<p><input type="submit" value="Process SVD">'\
       '<input type="reset" value="Reset"></form>'
       '</form>'

  # Make sure we have been invoked properly.

  if [ "$REQUEST_METHOD" != "GET" ]; then
        echo "<hr>Script Error:"\
             "<br>Usage error, cannot complete request, REQUEST_METHOD!=GET."\
             "<br>Check your FORM declaration and be sure to use METHOD=\"GET\".<hr>"
        exit 1
  fi

  # If no search arguments, exit gracefully now.

  if [ -z "$QUERY_STRING" ]; then
        exit 0
  else
     # No looping this time, just extract the data you are looking for with sed:
     CC=`echo "$QUERY_STRING" | sed -n 's/^.*val_c=\([^&]*\).*$/\1/p' | sed "s/%20/ /g"`
  fi

  echo '<hr><p>'
 if [[ $CC == 1 ]]; then
     SVN_URL='http://cm.megacorp.com/svn/trunk/projX/'
     echo '<h2>ProjX</h2><br>'
     echo $SVN_URL
     echo '<p><hr><p>'
  fi
  if [[ $CC -eq 2 ]]; then
     SVN_URL='http://cm.megacorp.com/svn/trunk/projY/'
     echo '<h2>ProjY</h2>'
     echo $SVN_URL
     echo '<p><hr><p><br>'
  fi
  if [[ $CC -eq 3 ]]; then
     SVN_URL='http://cm.megacorp.com/svn/BigProj/trunk/AppZ/'
     echo '<h2>Proj AppZ</h2>'
     echo '<p><hr><p>'
  fi
  if [[ $CC -eq 0 ]]; then
     SVN_URL=`echo  "$QUERY_STRING" | sed -n 's/^.*input_svnurl=\([^&]*\).*$/\1/p' | sed "s/%3A/:/g" | sed "s/%2F/\//g" | sed  "s/%20/ /g"`
     echo "<h2>$SVN_URL</h2>"
     echo '<p><hr><p>'
  fi

  echo '<pre>'
  /usr/bin/svn --username batchsvn --password supersecret --non-interactive info -R $SVN_URL | awk -f /opt/bin/svd_report.awk
  echo '</pre>'

echo '</body>'
echo '</html>'

exit 0
