#!/bin/bash

# This program is part of the tbackup package,
# (c) 1993,1994,1995,1998 Koen Holtman.
#
# The tbackup package is free software; you can redistribute it and/or
# modify it under the terms of version 2 of the GNU General Public
# License as published by the Free Software Foundation.
# No warranty. See the GNU General Public License for more details.

set -f
userwd=`pwd`
set=/etc/tbackup/tdir.set
bset=tdir.set

function usage
{
 echo 'tdir usage:'
 echo '  tdir [-s setname] -c|-create <description>'
 echo '  tdir [-s setname] <dirname/filename> [...]'
 echo '  tdir [-s setname] not name|ext|dir|fullname <string/"pattern"> [...]'
 echo '  tdir [-s setname] -d|-delete|-e|-edit|-l|-list|-u|-undoadd'
 echo 'man tdir gives more help.'
}

function listset
{ 
 echo Current contents of $set:
 cat -vn $set 
}

function setpresent
{
 if [ ! -e $set ]; then
  echo "Error: directory set $set does not exist."
  usage
  exit 1
 fi
}

#handle -s argument
if [ $# -ge 2 ]; then
 case "$1" in "-s" ) 
   set="/etc/tbackup/$2.set"
   bset=$2.set
   shift 2
 ;;
 esac
fi

if [ $# = 0 ]; then 
  setpresent
  listset
  echo "tdir -h gives help"
  exit 0
fi

case "$1" in
 "-c" | "-create" )
    if [ -e $set ]; then
      echo "Error: the directory set $set already exists."
    else
      shift
      echo "# $*" >>$set
      echo "Created directory set $set."
      listset
    fi
  ;;
 "-d" | "-delete" )
    if rm $set; then
      echo "Set $set deleted."
    fi
  ;;
 "-l" | "-list" )
    setpresent
    listset
  ;;
 "-e" | "-edit" )
    setpresent
    source /etc/tbackup/Config
    if [ "$2" != "" ]; then tdireditor="$2"; fi
    $tdireditor $set
  ;;
 "-u" | "-undoadd" )
    setpresent
    lines="`wc -l <$set`"
    if [ "$lines" -eq 0 ]; then 
      echo "Error: There are no more lines left to remove in $set."
      exit 1
    fi
    echo "Removing last line \"`tail -1 $set`\" from $bset."
    lines=$[$lines -1]
    head -$lines $set >$set.tmp
    mv -f $set.tmp $set
    listset
  ;;
 "not" )
   case "$2" in "name" | "fullname" | "ext" | "dir" )
     setpresent
     echo "$*" >>$set
     echo "Added predicate \"$*\" to $bset."
    ;;
     * )
     echo "Error: Invalid \"not\" predicate."
     usage
   esac
  ;;
 -* )
   usage
   exit 1
  ;;
 * )
  setpresent
  for i in $*; do
   case $i in [!/]* )
     if [ $userwd = "/" ]; then
       i=/$i
     else
       i=$userwd/$i
     fi ;;
   esac 
   if [ ! -e $i ]; then
    echo "Error: file or directory \"$i\" does not exist."
   else
     if [ -d $i ]; then
       name=`cd $i;pwd`
       if [ "$name" != "$i" ]; then
        echo "[Changed \"$i\""
        echo "      to \"$name\".]"
       fi
     else
       dir=`dirname $i`
       dir=`cd $dir;pwd`
       if [ $dir = "/" ]; then dir=""; fi
       name=$dir/`basename $i`
       if [ "$name" != "$i" ]; then
        echo "[Changed \"$i\""
        echo "      to \"$name\".]"
       fi
     fi
     echo $name >>$set
     if [ -f $name ]; then
       echo "Added file \"$name\" to $bset."
     elif [ -d $name ]; then
       echo "Added directory \"$name\" to $bset."
     else
       echo "Warning: \"$name\" is not a regular file or directory."
       echo "Added \"$name\" to $bset."
     fi
   fi
  done
esac


