#!/bin/sh
# gfscheck -- check what files are installed by a package.

# Copyright (C) 2004 Henrik S. Hansen

# Author: Henrik S. Hansen <hsh@freecode.dk>
# $Date: 2004/10/24 00:06:32 $
# $Revision: 1.3 $

# This file is part of gfs.

# gfs 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.

# gfs 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 gfs; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA


# Synopsis: gfscheck package.

# List files installed by package.  Searches for the log file in
# /usr/share/gfs.  You can just use the start of the package name, but
# this must then be unique.  If package name is only partial and not
# unique, complain and exit.  Files that are installed are listed,
# files installed but subsequently deleted are preceded by '# '.


if [ $# -ne 1 -o -z "$1" ] ; then
    echo "Usage: $0 package"
    exit 1
fi

cd /usr/share/gfs

# Find unique logfile
matched=$(mktemp)
find * -name "$1"'*' ! -name '*.dellog' > $matched
if [ $(cat $matched | wc -l) -gt 1 ] ; then
    echo 'Log file not unique.'
    exit 1
elif [ $(cat $matched | wc -l) -lt 1 ] ; then
    echo 'Log file not found.'
    exit 1
else
    log=$(cat $matched)         # Log file
    pkgname=${log%.*}           # Full name of package
    
    if [ -f "$pkgname.addlog" ] ; then
        log="$pkgname.addlog"
    elif [ -f "$pkgname.deleted" ] ; then
        log="$pkgname.deleted"
    else
        echo 'Log file not found.'
        exit 2
    fi
fi

echo "Logfile used: $log" >&2

for f in $(grep -v '^#' $log) ; do
  if [ -f $f -o -h $f -o -d $f ] ; then
      echo "$f"
  else
      echo "# $f"
  fi
done
