#!/bin/sh
#
# xedit - cgi-bin to fire up an xedit for the REMOTE_USER !
#
#	This script is part of WWWutil Copyright Mark Metson 1996
#	Licensed under the GNU General Public License version 2.
#		(See the file COPYING for license details.)
#


WWWUTIL="/var/lib/WWWutil"
WWWDBS="/var/lib/WWWutil/qdDBs"
WWWDB=$WWWDBS/WWWutil
WWWPROJECT=Staffroom

QBIN="/usr/local/qddb/bin"
QUERY=$QBIN/query

SUPER="/usr/bin/super"
XEDIT=/usr/X11/bin/xedit
AWK="/usr/bin/awk"

echo Content-type: text/html
echo

HEADER="`$QUERY $WWWDB -sep ' ' Name $WWWPROJECT -print Header`"
FOOTER="`$QUERY $WWWDB -sep ' ' Name $WWWPROJECT -print Footer`"


if [ -z $REMOTE_USER ]; then
  TITLE="Function requires logged-in user"
  printf "$HEADER" "$TITLE" "$TITLE"
  echo "This routine only works if you are first logged"
  echo "in on the server, because otherwise I have no idea"
  echo "which files you should be permitted to edit!"
  printf "$FOOTER"
  exit 1
fi

uid=`id -u $REMOTE_USER`

if [ "$REMOTE_ADDR" = "198.166.248.1" ]; then
  TITLE="Xwindows Display Unknown"
  printf "$HEADER" "$TITLE" "$TITLE"
  echo "This routine only works if you are not using a proxy,"
  echo "because I need to know where you are located in order"
  echo "to tell the editor where your Xwindows DISPLAY is."
  printf "$FOOTER"
  exit 1
fi

file=`echo "$*" | $AWK -f webpaths.awk`

if [ -z $file ]; then
  TITLE="Remote Editor"
  printf "$HEADER" "$TITLE" "$TITLE"
  cat <<-EOM
	This is a form for using an Xwindows text-editor to edit a file
	on this webserver host.
	<P>
	<ISINDEX>

	Enter the pathname of the file you wish to edit in your browser's
	search input.
EOM
  printf "$FOOTER"
else
  TITLE="Remote Editor"
  printf "$HEADER" "$TITLE" "$TITLE"
  DISPLAY="$REMOTE_ADDR:0.0"
  $SUPER doas $uid $XEDIT -display $DISPLAY $file &
  echo "Editor fired up, if all goes well it should appear on your Xwindows"
  echo "display at $DISPLAY"
  echo "<P>"
  printf "$FOOTER"
fi
