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

#---------------------------------------------------------------------
## @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.
##
#---------------------------------------------------------------------
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()  {

  OMIT="^$INSTALL_ROOT/dev\|^$INSTALL_ROOT/tmp\|^$INSTALL_ROOT/usr/src"
  OMIT_IN="     rename\|        symlink\|       unlink"

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

}


#---------------------------------------------------------------------
##
## Creates the install log containing all files installed by the spell.
##
#---------------------------------------------------------------------
function track()  {

  debug  "libsorcery" "Running track() on $SPELL"

  message  "${MESSAGE_COLOR}Creating install log ${FILE_COLOR}${INST_LOG}${DEFAULT_COLOR}"

  parse_iw                   |
  sort                       |
  uniq                       |
  filter  "$EXCLUDED"        |
  filter  "$LOCAL_EXCLUDED"  |
  exists                     >  $TMP_LOG

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

  MISOWNED_SYMLINKS=`syms_not_owned  $TMP_LOG
                     echo  -n  "/dev/null"`

  if    [  "$MISOWNED_SYMLINKS"  ==  "/dev/null"  ]
  then  cp  $TMP_LOG  $INST_LOG
  else  grep  -v  "$MISOWNED_SYMLINKS"  $TMP_LOG  >  $INST_LOG
  fi

  rm  -f  $IW_LOG  $TMP_LOG

}

# this may not be the best place for these, but it seems the most relavent
#---------------------------------------------------------------------
## Makes a list of files with the md5sum
#---------------------------------------------------------------------
function md5list() {
  local INPUT=$1
  local OUTPUT=$2
  message  "${MESSAGE_COLOR}Creating MD5 log ${FILE_COLOR}${OUTPUT}${DEFAULT_COLOR}"
  debug  "libsorcery" "Running md5list() on $SPELL"
  while read LINE ; do
                debug "archive()" "Checking file $LINE"
    if  [ !  -p "$LINE" ] && [ ! -d "$LINE" ] && [ -e "$LINE" ] ; then
                        debug "archive()" "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
## every file installed by a spell.
##
#---------------------------------------------------------------------
function archive()  {

  debug  "libsorcery" "Running archive() on $SPELL"

  if    [  "$ARCHIVE"  ==  "off"  ]; then
    debug "libsorcery" "archive() - ARCHIVE=$ARCHIVE, aborting archival."
    return
  fi

  debug "libsorcery" "archive() - ARCHIVE=$ARCHIVE, archiving."

  message  "${MESSAGE_COLOR}Creating cache file ${FILE_COLOR}${CACHE_COMP}${DEFAULT_COLOR}"

  if  [  "$COMPRESSBIN" == "tar"  ]; then
    tar --no-recursion -cPf "$CACHE_COMP" -T $INST_LOG
  else
    tar --no-recursion --"$COMPRESSBIN" -cPf "$CACHE_COMP" -T $INST_LOG
  fi

}




#---------------------------------------------------------------------
## @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
##
#---------------------------------------------------------------------
