#!/bin/csh -f
# Maintain an archive of versions of a developing program.
# Archive maintains files in a subdirectory called "archdir".
# File "ArchList.txt" will contain names of files in the project.
# Otherwise archdir contains subdirectories named for the
# date and time of creation and containing the archived files.
# By design, files created in the same hour are overwritten.

# Usage: archive new MyName
#        archive add Myfile1 [MyFile2 ...]
#        archive remove MyfileN
#        archive   (With no arguments: lists files to be archived.)
#        archive save ["comments"]
#        archive show   (This shows previous dates and comments of archiving.)
#        archive help   (Lists command options.)

# default names could be changed
set ARCHDIR = archdir
set ARCHLIST = ArchList.txt

# Assure correct usage
if ($# > 0 && $1 != new && $1 != add && $1 != remove && $1 != show && $1 != save) then
  echo Usage is one of the following:
  echo archive new MyName
  echo archive add Myfile1 [MyFile2 ...]
  echo archive remove MyfileN
  echo archive \(With no arguments: this lists files to be archived.\)
  echo archive save [comments in quotations]
  echo archive show \(shows dates and comments of archived versions\)
  exit
endif

# Create new archive in archdir with ArchList.txt containing project name
if ($1 == new) then
  if (-e $ARCHDIR && -e $ARCHDIR/$ARCHLIST) then 
    echo Sorry, archive already exists: `head -n1 $ARCHDIR/$ARCHLIST`
    exit
  endif
  if ($# != 2) then
    echo Usage: new MyName
    exit
  endif
  echo Creating new archive named Project $2
  if (! -e $ARCHDIR) then
    mkdir $ARCHDIR
  endif
  echo Project $2 > $ARCHDIR/$ARCHLIST
  exit
endif

# Assure archive is setup (by new) before being used
if (! -e $ARCHDIR || ! -e $ARCHDIR/$ARCHLIST) then
  echo You must run \"archive new\" once first.
  exit
endif 

# List files in the archive
if ($# == 0) then
  set rslt = `wc -l  $ARCHDIR/$ARCHLIST`
  if ($rslt[1] == 1) then
    echo Use \"archive add myfilename\" to add files to the archive list
    exit
  endif 
  set line1 = `head -n1 $ARCHDIR/$ARCHLIST`
  echo Files on archive list for $line1\:
  grep -v "$line1" $ARCHDIR/$ARCHLIST
  echo Use \"archive save mycomments\" to archive current state.
  exit
endif

# Show archived versions in the archive
if ($1 == show) then
  echo The following archives are in ${ARCHDIR}:
  cat $ARCHDIR/ArchiveComments.txt
  exit
endif

# Add files to the archive (append to ArchList.txt)
if ($1 == add) then
  if ($# < 2) then
    echo Nothing to add!
    exit
  endif
  shift
  echo Adding $# file\(s\) to `head -n1 $ARCHDIR/$ARCHLIST`
  while ($# > 0)
    echo $1 >> $ARCHDIR/$ARCHLIST
    shift
  end
  exit
endif

# remove one file from archive
if ($1 == remove) then
  if ($# != 2) then
    echo You must remove exactly one file at a time.
    exit
  endif
  set cnt = `cat $ARCHDIR/$ARCHLIST | grep $2 | wc -l`
  if ($cnt == 0) then
    echo $2 was not found on the archive list.
    exit
  endif
  if ($cnt > 1) then
    echo Ambiguous request\; please remove manually.
    emacs $ARCHDIR/$ARCHLIST
    exit
  endif
  set tmpfile = `mktemp -u /tmp/archtemp.XXXXXX`
  cat $ARCHDIR/$ARCHLIST |grep -v $2 > $tmpfile
  cp $tmpfile $ARCHDIR/$ARCHLIST
  rm $tmpfile
  echo Removing file $2 from `head -n1 $ARCHDIR/$ARCHLIST`
  exit
endif

# Do actual archiving
if ($1 != save) then
  echo Error in archive logic
  exit
endif
if ($#>2) then
  echo comments must be within double quotes\; try again
  exit
endif
set cnt = `wc -l $ARCHDIR/$ARCHLIST`
@ cnt -= 1
set date = `date +%h%d%Yat%Hhrs`
if (-e $ARCHDIR/$date) then
  echo -n "Type yes to replace files in $date> "
  set response = $<
  if ($response != yes) then
    echo Not archiving
    exit
  endif
else
  mkdir $ARCHDIR/$date
endif
if (! -e $ARCHDIR/$date) then
  echo Archiving FAILED: Can't create $ARCHDIR/$date
  exit
endif
echo Saving $cnt files to directory $date
set line1 = `head -n1 $ARCHDIR/$ARCHLIST`
set filelist = `grep -v "$line1" $ARCHDIR/$ARCHLIST`
while ( $#filelist > 0 )
  cp $filelist[1] $ARCHDIR/$date/ || \
    (echo Error copying files\; archiving FAILED\!)
  shift filelist
end
if ($# == 2) then
  echo $date $2 >>  $ARCHDIR/ArchiveComments.txt
else
  echo $date  >>  $ARCHDIR/ArchiveComments.txt
endif

