
#*************************************************************************
# This file contains functions and aliases which can be cut out and put
# in your .skshrc file.  They are not included there by default, since
# many people will not want them, or will want modified versions.
# These commands should operate with SKsh version 1.3 or later.
# They are also simple skeletons which can be expanded to add features.
#
# Also, please note that these commands have not been tested extensively.
#*************************************************************************


#*************************************************************************
# This is a "man" function which can be used to retrieve documentation
# files stored in a MAN: directory.  These files (which are often
# included with public domain software as "readme" files) should be
# copied to a name such as "MAN:prog.MAN".  Then, "man prog" can be used
# to retrieve the files with the $PAGER command.
#*************************************************************************

function man {
   if [ -z "$PAGER" ] then PAGER='more'; fi

   if [ ! -f "MAN:$1.MAN" ]
   then
      echo "No manual entry for $1"
   else
      $PAGER "MAN:$1.MAN"
   fi
}


#*************************************************************************
#  This function accepts a number of parameters, and prompts the user
#  for whether these files should be deleted.  For example, "qrm *.o"
#  would prompt the user at each .o file.  Answering "Y" or "y" causes
#  the file to be removed; otherwise, it is ignored.  A useful extention
#  would look at only the first character of the response, thus allowing
#  "yes" to be used in place of "y".
#*************************************************************************

function qrm {    # query rm
   local ans

   for file in $*
   do
      echo -n "Remove $file ? "
      read ans

      if [ "$ans" = 'y' -o "$ans" = 'Y' ]
      then
         rm -v "$file"
      fi
   done
}


#*************************************************************************
# This alias will remove emacs backup files by using the "qrm" function
# defined above.  It could be extended to remove ".o" files, or anything
# else.
#*************************************************************************

alias clean='qrm *~'


#*************************************************************************
# The following functions implement directory push and pop operations.
# The first, pushd, accepts one argument.  It cd's to that directory, but
# pushes the current working directory onto a stack, first, so that it
# can later be retrieved with a popd.
#*************************************************************************

function pushd {
   if [ $# -ne 1 ]
   then
      echo "Usage: $0 new_dir"
   else
      DIRSTACK="$PWD,$DIRSTACK"
      export DIRSTACK
      cd "$1"
   fi
}

#*************************************************************************
# This function implements the popd operation.  It accepts no arguments,
# and simply pops a directory off the stack and returns to that point.
# it complains if the directory stack is empty.
#*************************************************************************

function popd {
   local comma_pos new_dir

   if [ $# -ne 0 ]
   then
      echo "Usage: $0"
      return
   elif [ -z "$DIRSTACK" ]
   then
      echo 'The directory stack is empty'
      return
   fi
      
   comma_pos = $(expr index "$DIRSTACK" ',')
   new_dir   = $(expr substr "$DIRSTACK" 1 $(expr $comma_pos - 1))
   DIRSTACK  = $(expr substr "$DIRSTACK" $(expr $comma_pos + 1) 32000)
   export DIRSTACK
   cd "$new_dir"
}


#*************************************************************************
# These aliases clear and print the directory stack, respectively.  The
# directory stack will be printed with a trailing comma, which is how it
# is stored.  This could be cleaned up quite simply.
#*************************************************************************

alias cleard='DIRSTACK=""'
alias dstack='echo "$DIRSTACK"'

#*************************************************************************
# Ths function insert all files in the current directory except .o
# files into the current complist.  It also inserts the contents of the
# COMPLIST variable.
#*************************************************************************

function cget {
   complist -e
   complist -a !*.o
   complist -a $COMPLIST
}

#*************************************************************************
# Aliases and functions to make zoo files a bit easier to manipulate.
# The "zmore" function
#*************************************************************************

alias zp='zoo -print'
alias zl='zoo -list'
alias zx='zoo -extract'
alias za='zoo -add'

function zmore {

  local zoofile tmpfile

  if [ $# -lt 2 ]
  then
     echo "Usage: $0 zoofile file1..."
     return;
  fi

  if [ -f "$1" ] then zoofile="$1"; else zoofile="$1.zoo"; fi

  if [ ! -f "$zoofile" ]
  then
     echo "$0: $zoofile not found"
     return
  fi

  shift

  tmpfile="pipe:tmp_$CLINUM.$CMDNUM"

  if [ -z "$PAGER" ] then PAGER='more'; fi

  srun -o "$tmpfile" $(which zoo) -print "$zoofile" $*
  $PAGER "$tmpfile"
}


#*************************************************************************
# This function insert all files from a 'zoo' archive into the current
# complist.  They also insert the contents of the COMPLIST variable.
#*************************************************************************

function _zadd {
   shift 2    # get rid of "Archive blah.zoo"
   complist -a $*
}

function zget {

   local zoofile zlist

   if [ -f "$1" ] then zoofile="$1"; else zoofile="$1.zoo"; fi

   if [ ! -f "$zoofile" ]
   then
      echo "$0: $zoofile not found"
      return
   fi

   complist -e

   zlist=$(zoo lf1 "$zoofile")
   
   _zadd $zlist
   complist -a $COMPLIST
}


#*************************************************************************
# This function extracts a zoo file in its own sub-directory, then
# deletes the original
#*************************************************************************

function zxdir {
   local file dname zooname

   for file in $*
   do
      dname=$(extname -v "$file")
      zooname="$dname.zoo"
      if [ ! -d "$dname" ] then mkdir "$dname"; fi

      mv "$zooname" "$dname"; cd "$dname"

      if zoo -extract "$zooname" >nil:
      then
         rm "$zooname"
      else
         echo "Extract Failed!"
      fi
      cd ..
   done
}

