#!/bin/bash
#---------------------------------------------------------------------
## @Synopsis Functions for dealing with tracking of files, and other installwatch related things.
## @Copyright Copyright (C) 2004 The Source Mage Team <http://www.sourcemage.org>
## Functions for dealing with tracking of files, and other installwatch related things.
#---------------------------------------------------------------------

#---------------------------------------------------------------------
## @Stdin list of files
## @Stdout list of files
## Reads a list of files from standard in, and returns a list of the
## files that exist.
##
#---------------------------------------------------------------------
function exists()  {
  while  read  ITEM;  do  [  -e  "$ITEM"  ]  &&  echo  $ITEM;  done;
}


#---------------------------------------------------------------------
##
## Given a list of files it will notify installwatch of them.
## Usfull for spells whos components are not dynamicly linked
## to glibc. Uses simple hack of touching files while
## installwatch is running.
##
#---------------------------------------------------------------------
function track_manual()  {

  if  [[  -z  "$INSTALLWATCHFILE"  ]];  then
    echo "Can't tell installwatch to manually track... installwatch isn't running."
    return 1
  fi

  local  i
  for i in $* ; do
    [ -e $i ] && touch $i
  done

  return 0

}


#---------------------------------------------------------------------
##
## Sets up installwatch.
##
#---------------------------------------------------------------------
function invoke_installwatch()  {

  if  [  -e  /usr/lib/installwatch.so  ];  then
    export  INSTALLWATCHFILE=$IW_LOG
    export  LD_PRELOAD=/usr/lib/installwatch.so
  fi

}


#---------------------------------------------------------------------
##
## Stops using installwatch
##
#---------------------------------------------------------------------
function devoke_installwatch()  {

  unset  LD_PRELOAD
  unset  INSTALLWATCHFILE

}



#---------------------------------------------------------------------
## @param filename
##
## First argument is a file containing a list of files.  Checks for
## broken symbolic links.
##
## This function is depreciated....
##
#---------------------------------------------------------------------
function syms_not_owned()  {

  cat  $1  |
  while  read  ITEM;  do

    if  [  -h  "$ITEM"  ]   &&
        [  -f  "$ITEM"  ];  then

      DEST=$(  basename  $(  ls   -la  "$ITEM"  |
                             cut  -d  '>'  -f2  |
                             cut  -c  2-
                          )
            )

      if  !  grep  -q  "$DEST"  "$1"
      then   echo  -n  "$ITEM\|"
             echo      "$ITEM"  >>  /tmp/$SPELL.rejected.symlinks
      fi

    fi

  done

}


#---------------------------------------------------------------------
##
## Parses the installwatch log for files installed by a spell.
##
#---------------------------------------------------------------------
function parse_iw()  {

  # it is EXTREMELY IMPORTANT that this variable contains an actual
  # tab character and not some number of spaces. Otherwise BAD THINGS
  # will happen.
  local TAB="	"
  OMIT_IN="${TAB}rename\|${TAB}symlink\|${TAB}unlink"

  grep -v "$OMIT_IN" $IW_LOG | cut -f3 | grep "^/"
  cat                $IW_LOG | cut -f4 | grep "^/"

}


#---------------------------------------------------------------------
##
## Creates the install log containing all files installed by the spell.
##
#---------------------------------------------------------------------
function track()  {
  debug  "libtrack" "Running track() on $SPELL"
  local TMP_LOG=$1

  parse_iw                           |
  sort                               |
  uniq                               |
  install_log_filter $INSTALL_ROOT "" |
  grep -v -x ""                      |
  filter  "$EXCLUDED"                |
  filter  "$LOCAL_EXCLUDED"          |
  install_log_filter "" $INSTALL_ROOT |
  exists                             >  $TMP_LOG

  echo  "$C_LOG_COMP"               >>  $TMP_LOG
  echo  "$MD5_LOG"                  >>  $TMP_LOG
  echo  "$INST_LOG"                 >>  $TMP_LOG

}

#---------------------------------------------------------------------
## Makes a list of files with the md5sum
#---------------------------------------------------------------------
function md5list() {
  local INPUT=$1
  local OUTPUT=$2
  debug  "libtrack" "Running md5list() on $SPELL"
  while read LINE ; do
    debug "libtrack" "Checking file $LINE"
    if  [ !  -p "$LINE" ] && [ ! -d "$LINE" ] && [ -e "$LINE" ] ; then
      debug "libtrack" "Running md5 on $LINE"
      md5sum "$LINE"
    fi
  done < $INPUT 2>/dev/null > $OUTPUT
}

#---------------------------------------------------------------------
##
## Creates a bzip/gzip'ed tar file containing an archived backup of
## file specified on standard input
## @stdin files, one per line, to put into the archive
## @param directory input files are relative to
##
#---------------------------------------------------------------------
function archive()  {

  debug  "libtrack" "Running archive() on $SPELL"
  if    [  "$ARCHIVE"  ==  "off"  ]; then
    debug "libtrack" "archive() - ARCHIVE=$ARCHIVE, aborting archival."
    return
  fi
  debug "libtrack" "archive() - ARCHIVE=$ARCHIVE, archiving."

  message  "${MESSAGE_COLOR}Creating cache file" \
           "${FILE_COLOR}${CACHE_COMP}${DEFAULT_COLOR}"
  pushd $1 &>/dev/null
  if  [  "$COMPRESSBIN" == "tar"  ]; then
    tar --no-recursion -cPf "$CACHE_COMP" -T -
  else
    tar --no-recursion --"$COMPRESSBIN" -cPf "$CACHE_COMP" -T -
  fi
  popd &>/dev/null

}

# this is to filter the install log from one form to another
# for install_root/track_root conversions
function install_log_filter() {
  sed "s:^$1:$2:"
}
function md5_log_filter() {
  sed "s:  $1:  $2:"
}



#---------------------------------------------------------------------
## @License
##
## This software is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This software is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this software; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
##
#---------------------------------------------------------------------
