#!/bin/csh -f

# Restrict the web access for the current directory to users in
# authorization domain $1, which will also be the name of the group of
# User IDs in $groupfile (see below).  If authorization domain $1 does
# not exist, it will be created with the User IDs given by the
# remaining arguments; if ther are no additional arguments, the IDs
# "rig arg chana" will be used.  Passwords in $passwdfile for any new
# User IDs must be specified via htpasswd.

# If there is no $1, an attempt will be made to find the necessary
# information to a complete setup of limitation to an authorization
# domain, specification of group members, and specification of
# passwords by obtaining information from the file grades.txt that has
# User IDs in a column labeled "EADDR" and passwords in a column
# labeled "NUMBER" (as per the set up for labeled columns recognized
# by excol).  Also consulted is grades.access (also used by the
# "courseeaddrs" script.), each line of which should contain a User ID
# and password in the second and third fields for an additional person
# to be granted access.  (Also sets up web directory, if it does not
# already exist, along with grades subdirectory and some desired
# links.)

set tempfile = /tmp/wwwac.$user.$$

set authdir = ~/.www/.wwwauths

set groupfile = $authdir/.htgroup
set passwdfile = $authdir/.htpasswd

set tmpgroup = /tmp/wwwac.htgroup.$user.$$

set dir = "`pwd`"

if ($#argv >= 1) then
  grep -v "^${argv[1]}:" $groupfile > $tmpgroup
  grep "^${argv[1]}:" $groupfile >> $tmpgroup
  if ($status) echo "${argv[1]}: rig arg chana" >> $tmpgroup
  if ($#argv >= 2) then
    echo y | subst "+^\(${argv[1]}\)"':.*$+\1: '" $argv[2-]+" $tmpgroup
  endif
  if (`cat $tmpgroup | wc -l` < `cat $groupfile | wc -l`) then
    echo "wwwac: $tmpgroup not moved to $groupfile due to reduction in number of lines."
    exit (2)
  else
    mv $tmpgroup $groupfile
  endif
  set group = $1
else
  set groupfile = $authdir/luccourses.htgroup
  set passwdfile = $authdir/lucstudents.htpasswd
  set term = $dir:t
  set headofdir = $dir:h
  set course = $headofdir:t
  set headofheadofdir = $headofdir:h
  set root = $headofheadofdir:t
  if (("$root" != "teaching") && ("$root" != "grants")) then
    echo 'wwwac: Exiting since no arguments and not in "teaching" directory'
    exit (3)
  endif
  set group = "$course$term-students"
  chmod go-rx $dir
  set dir = ~/.www/courses/$course/$term
  set submitdir = ~rig/.www/cgi-write/hwsubmit/$course/$term
  mkdir -p $submitdir/core
  chmod go-r $submitdir/core
  chmod go-r $submitdir
  chmod go-r $submitdir:h
  mkdir -p $dir
  if (! -e $dir/index.html) cp prior/web/index.html $dir
  touch $dir/index.html
  if (! -e $dir/$course$term.html) ln -s index.html $dir/$course$term.html
  if (! -e web) ln -s $dir web
  wwwnewcount -n courses/$course$term
  cp ~/.www/cgi-bin/{grades,vote,hwsubmit}.cgi $dir
 # chmod u+s $dir/{grades,vote}.cgi
  if (! -e hwsubmit) ln -s $submitdir hwsubmit
  if (! -e $dir/hwsubmit) ln -s $submitdir $dir/hwsubmit
  mkdir -p $dir/grades
  chmod go-r $dir/grades
  chmod go+x $dir/grades
  if (! -e grades) ln -s web/grades

  foreach script (grades vote)
    set cgilogdir = ~rig/.www/cgi-write
    if (! -e $cgilogdir/${script}cgilog/$course$term) touch $cgilogdir/${script}cgilog/$course$term
    chmod go-r $cgilogdir/${script}cgilog/$course$term
    ln -sf $cgilogdir/${script}cgilog/$course$term ${script}cgilog
  end
  if (! -e web/mailarchive) touch web/mailarchive
  ln -sf $dir/mailarchive ~/mail/arch-$course$term
  mkdir -p $submitdir/.LOG
 # chmod o+wx $submitdir/.LOG
  grep -v $group $groupfile > $tmpgroup
  echo -n ${group}: >> $tmpgroup
  (egrep 'NAME|EMAIL' grades.txt | excol EADDR NUMBER | tail -n +2; \
   grep -v '^old' grades.access | awk '{print $2, $3}' ) # All but old items get password access. \
      | awk '$1 != "" && $2 != "" {print "htpasswd -b '$passwdfile'", $1, $2 "; echo -n \" " $1 "\" >> '$tmpgroup'"}' | /bin/csh -f
  cat grades.txt | stripgradestats | excol EADDR | tail -n +2 \
      | awk '$1 != "" {print "mkdir -p '$submitdir'/core/"$1"; if (! -e '$submitdir'/"$1") ln -s core/"$1" '$submitdir'/"$1}' | /bin/csh -f
 # chmod o+w '$submitdir'/core/"$1
 # Above line removed just before right curly.

  mkdir -p $submitdir/core/$EADDR
  if (! -e $submitdir/$EADDR) ln -s core/$EADDR $submitdir/$EADDR
 # chmod o+w $submitdir/core/$EADDR
  foreach hwdir ($submitdir/core/*)
    if (-d $hwdir && ! -e $hwdir/.graded) then
      mkdir $hwdir/.graded
      echo "AuthUserFile $passwdfile\
AuthName $hwdir:t\
AuthType Basic\
\
<Limit GET>\
require user $hwdir:t\
</Limit>" > $hwdir/.htaccess
      # cp ~/.www/cgi-bin/index.cgi $hwdir # Not needed in new setup on rig.cs.luc.edu
      # cp ~/.www/cgi-bin/index.cgi $hwdir/.graded # Not needed in new web setup
    endif
  end
  grep altemail grades.access | awk '{print "if (! -e '$submitdir'/"$2") ln -s core/"$4,"'$submitdir'/"$2}' | /bin/csh -f
  echo " $EADDR" >> $tmpgroup
  if (`cat $tmpgroup | wc -l` < `cat $groupfile | wc -l`) then
    echo "wwwac: $tmpgroup not moved to $groupfile due to reduction in number of lines."
    exit (2)
  else
    mv $tmpgroup $groupfile
  endif
  mkdir -p web/lectures
  chmod go+rx web/lectures
  # cp ~/.www/cgi-bin/index.cgi web/lectures # Not needed in new setup on rig.cs.luc.edu
  if (! -e web/lectures/README) then
    cp ~/bin/courses/README-lectures web/lectures/README
  endif
  mkdir -p web/inclass
  chmod go+rx web/inclass
  # cp ~/.www/cgi-bin/index.cgi web/inclass # Not needed in new setup on rig.cs.luc.edu
  mkdir -p web/grader # To post things privately to grader
  cp grades.access web/grader
  pushd web/grader
  wwwac grader$course$term rig `sed -n 's+[ 	]*grader[ 	]*\([^ 	]*\).*+\1+p' grades.access`
  \rm -f grades.access
  mv .htaccess $tempfile
  sed 's+/.wwwauths/.htpasswd+/.wwwauths/lucstudents.htpasswd+' $tempfile > .htaccess
  # cp ~/.www/cgi-bin/index.cgi . # Not needed in new setup on rig.cs.luc.edu
  popd
endif

chmod go+rx $dir

echo "AuthUserFile $passwdfile\
AuthGroupFile $groupfile\
AuthName $group\
AuthType Basic\
\
<Limit GET>\
require group $group\
</Limit>" > $tempfile

mv $tempfile $dir/.htaccess
