#!/bin/ksh
# -----------------------------------------------------------------------
# UseVote Unix shell script   (c)1993 Ron Dippold All Rights Reserved
# UseVote 2.0a
#
# ( Note - there's a few parameters to set below this box. )
#
# This is for those of you running UseVote under Unix - this shell script
# really helps automate the UseVote process, as if it wasn't automated
# enough already.  It's somewhat dependent on my setting, but yours is
# probably somewhat similar, and you can always modify the script.
#
# uvshell assumes a directory under the directory you gather your votes for
# a specific group in called "done" - it saves results files and packed votes
# files here.  If you're running uvsplit, there's one done/ directory for each
# group.  If it can't find done/ it will create it.
#
# uvshell will name your raw votes and results files like this:
#   votes0000 results0000
#   votes0001 results0001
#   votes0002 results0002  (etc.)
# This gives you up to 10,000 invocations.  Sheesh!
#
# It will look through the done/ directory to see what's next - if the last
# votes file is votes0004 then votes0005 is next.  If this file already exists
# in the current directory it will use it.  Otherwise, it will attempt to
# rename votes to votes0005.  If it can't do either, it quits.
#
# It runs votes0005 through uvvote to produce results0005.  If you run uvshell
# without any parameters it stops here - this allows you to correct any
# wierdnesses manually if you want to.	Then when you run 'uvshell all',
# it does the above again (using votes0005 again), then continues:
#
# Then it packs votes0005 to create votes0005.gz, changes the privledges of
# both votes0005.gz and results0005 to 400 (read only by you), moves them both
# to done/, then catenates all the results files so far into results.all and
# does a uvcount on it.
#
# finally, it starts domail running in the background.
#
# is this fully automated or what?  Again, until you're comfortable with this,
# BACKUP YOUR STUFF before running it.
# ----------------------------------------------------------------------------
#
# Name of raw votes file
vfile="votes"

# extension of files created by packer... votes1 becomes votes1.gz
# if you don't have gzip, but only compress, the correct line is
#   ext=".Z"
# Don't forget to go down inside the file and change the packing
# command if you don't have gzip - it's currently set up for gzip
ext=".gz"

# command to run run uvvote.  Note the -i for interactive; remove this
# if you won't be supervising the running.
uvcmd="/home/ikluft/src/usevote-2.0/usevote/uvvote"

# trace file for uvvote to use, if any.  If not, comment this out.
#tfile="trace"

	# Permission for processed votes and results files - 400 is read only
	# by you - helps against accidental delete
	perm="400"

	# name of domail file - if you don't want it executed when done with
	# processing, then comment it out
	mfile="domail"

#---------------------- start of shell script -----------------------------

# Test for subdirectory "done", create if necessary
if ( test -d done ) then
  test
else
  echo  done/ not found, creating
  mkdir done
fi

# Now find the right name for the new voting file
for i1 in 0 1 2 3 4 5 6 7 8 9; do
  for i2 in 0 1 2 3 4 5 6 7 8 9; do
    for i3 in 0 1 2 3 4 5 6 7 8 9; do
      for i4 in 0 1 2 3 4 5 6 7 8 9; do
        i=${i1}${i2}${i3}${i4}
        file=votes${i}
        rfile=results${i}
        if (test -f done/${file}${ext-} ) then
          test
        else
          break 4
        fi
      done
    done
  done
done

if ( test $i = "9999" ) then
  echo Ran out of numbers 0000-9999 to use!  Wow...  aborting.
  exit
else
  echo File to use is $file \\c
fi

# See if it already exists - rename if necessary
if ( test -f $file ) then
  echo " which already exists"
else
  if( test -f $vfile) then
     echo " - renaming $vfile to $file"
     mv $vfile $file || exit
  else
     echo " but neither $vfile or $file exist!"
     exit
  fi
fi
vfile=$file

# delete old results? file
if ( test -f $rfile ) then
#  mv -f $rfile ${rfile}.old || exit
   rm -f $rfile || exit
fi

#do uvvote!
echo Runnning uvvote
$uvcmd $vfile $rfile ${tfile-}

if ( test "${1}" != "all" ) then
  echo "Exiting for cleanup - run as "
  echo "  $0 all"
  echo "to do all cleanup, mailing, and packing"
  exit
fi

# generate the packed file name.
pfile=${vfile}${ext-}

# Here's the pack command!  Use ${vfile} for the name of the votes file and
# ${pfile} for the name of the packed votes file.  If you are using something
# like zip, which requires the packed file name and unpacked file name, use
#   zip -m ${pfile} ${vfile}
# You should move (-m) the file to the archive, not just add it
# for compress, which should be on your system for sure, use
#   compress ${vfile}

if( test "${ext-}" = "" ) then
  echo "No extension given, not packing"
else
  echo "Packing ${vfile} to ${vfile}${ext-}"
# The line right below this one is the actual pack command
  gzip -9 ${vfile}
  if( test $? -ne 0 ) then
    echo Error while trying to pack ${vfile} -  Aborting.
    exit
  fi
fi

echo Protecting and moving $pfile and $rfile

# change the protection on the files
chmod $perm $pfile $rfile

# if you wanted to copy the file elsewhere, this would be a good place
# to do it
# cp ${pfile} ../mybackup/.

# move the files to done/
mv -f $pfile done/${pfile}
mv -f $rfile done/${rfile}

# and create a file with all results in it
cat done/results* > results.all

# now launch domail in the background
if( test "${mfile-}" = "" ) then
  test
else
  nohup sh $mfile > /dev/null &
  echo Running $mfile with PID of $!, output to ${mfile}.out
fi

# and count results
uvcount results.all

