#! /bin/sh

# This should be called with name= given it in the query string

echo Content-type: text/html
echo

echo "<html><head><title>Enter a new road that has already been linked</title></head>"
echo "<body><h1>Enter a new road that has already been linked</h1>"

echo "<p><strong>This is still under development</strong></p>"

echo "<p>If you want to make a new road that is not on the list, you must first"
echo "make it a side-road of an existing road, by editing one of the roads to which"
echo "it connects. It will then appear on the list of roads to add.</p>"

echo "<p>Pleaes do not enter copyright data; don't just type in roads from published"
echo "maps, but survey them afresh; this material will be in the public domain.</p>"

ROADLIST=../edit-requests/r$$
ROADOPTS=../edit-requests/o$$
AREAOPTS=../edit-requests/a$$
FGREPTEMP=../edit-requests/f$$

DUMMIES="    A<br>few<br>dummy<br>entries<br>will<br>be<br>provided<br>along<br>here<br>for<br>you<br>to<br>fill<br>in<br>later"

echo $QUERY_STRING | tr '&+' "\012 " | sed -e "s/=/=\"/" | sed -e s/$/\"/ | egrep "^name=\"[^\"]*\"$" | sed s/name/link/ > $FGREPTEMP

# echo "<pre>Fgreptemp:"
# cat $FGREPTEMP
# echo "</pre>"

echo "Roads already linking this road (so this road should link them):"
echo "<ul>"
fgrep -l -f $FGREPTEMP ../roads/*.attr | sed "s:../roads/:  <li> :" | sed "s:.attr::"
echo "</ul>"


ls -1 ../areas/*.attr | sed "s:^\\.\\./areas/\\(.*\\)\\.attr$:    <option> \\1:" | sort > $AREAOPTS
./list-existing-roads > $ROADLIST
sed "s/^/      <option> /" < $ROADLIST > $ROADOPTS

echo "<form method=\"post\" action=\"addpage.cgi\">"
echo "<table align=\"center\" border>"
echo "<tr align=\"center\">"
echo "  <td valign=\"center\">"
echo "  </td>"
echo "  <td valign=\"center\">"
echo "Top road "
echo "    <select name=\"top\">"
echo "      <option selected>none"
cat $ROADOPTS
echo "    </select>"
echo "  </td>"
echo "  <td valign=\"center\">"
echo "  </td>"
echo "</tr>"
echo "<tr align=\"center\">"
echo "  <td valign=\"center\">"
echo "    Top left road "
echo "    <select name=\"top_left\">"
echo "      <option selected>none"
cat $ROADOPTS
echo "    </select>"
echo "  </td>"
echo "  <td valign=\"center\">"
echo "    Junction<br>(if needed)"
echo "  </td>"
echo "  <td valign=\"center\">"
echo "    Top right road "
echo "    <select name=\"top_right\">"
echo "      <option selected>none"
cat $ROADOPTS
echo "    </select>"
echo "  </td>"
echo "</tr>"
echo "<tr align=\"center\">"
echo "  <td align=\"right\" valign=\"center\">"
echo "    $DUMMIES"
echo "  </td>"
echo "  <td valign=\"center\">"
echo "Road name "
echo "    <select name=\"name\">"
./list-missing-roads | ./sel-opt $QUERY_STRING
echo "    </select>"
echo "  </td>"
echo "  <td align=\"left\" valign=\"center\">"
echo "    $DUMMIES"
echo "  </td>"
echo "</tr>"
echo "<tr align=\"center\">"
echo "  <td valign=\"center\">"
echo "    Bottom left road "
echo "    <select name=\"bot_left\">"
echo "      <option selected>none"
cat $ROADOPTS
echo "    </select>"
echo "  </td>"
echo "  <td valign=\"center\">"
echo "    Junction<br>(if needed)"
echo "  </td>"
echo "  <td valign=\"center\">"
echo "    Bottom right road "
echo "    <select name=\"bot_right\">"
echo "      <option selected>none"
cat $ROADOPTS
echo "    </select>"
echo "  </td>"
echo "</tr>"
echo "<tr align=\"center\">"
echo "  <td valign=\"center\">"
echo "  </td>"
echo "  <td valign=\"center\">"
echo "    Bottom road "
echo "    <select name=\"bot\">"
echo "      <option selected>none"
cat $ROADOPTS
echo "    </select>"
echo "  </td>"
echo "  <td valign=\"center\">"
echo "  </td>"
echo "</tr>"
echo "</table>"
echo "<a href=\"getplan.cgi?type=settlement&name=cambridge&detail=on\">Area</a>:"
echo "  <select name=\"area\">"
cat $AREAOPTS
echo "  </select><br>"
# echo "Description (may use <a href=\"../doc/authoring/some-html.html\">simple HTML</a> markup):"
# echo "<textarea name=\"description\" rows=\"7\" cols=\"70\">"
# echo "</textarea>"
echo "Please check your entry carefully, then <input type=\"submit\"> it -- thanks!"
echo "</form>"

rm -f $ROADLIST $ROADOPTS $AREAOPTS $FGREPTEMP

echo "</body></html>"
echo
