#!/bin/sh

# checkpkg
# Copyright (C) 2006, 2007, 2008, 2011 Joseph Rosevear

# This file is part of an application of SAM for GNU/Linux Slackware
# known as SAM-GLS.

# SAM-GLS 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 3 of the License, or (at
# your option) any later version.

# SAM-GLS 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 program.  If not, see <http://www.gnu.org/licenses/>.

# (Slackware is a registered trademark of Patrick Volkerding and
# Slackware Linux, Inc.)

# JHR 110109 I changed the code to use ~/scratch instead of
# $env_scratch.  To make this robust I also added a check to see if
# ~/scratch was an existing, writable dir.

# JHR 110114 I changed the code back to using $env_scratch.  Sorry for
# the flip flop.


#Runs Volkerding's installpkg with the -warn on $1.  When it does this
#it writes a list of files to $env_scratch. Then it does some things to
#check if any of those files already exist.  This determines if there
#would be any collisions.

# Does $env_scratch exist and is it writable?
if ! [ \( -d $env_scratch \) -a \( -w $env_scratch \) ]; then

   echo Error:  \$env_scratch must be a writable, existing dir
   exit
fi

#Was a package (or something) given?
if [ "$1" = "" ]; then

   echo
   echo "Usage: checkpkg <package to be checked>"
   exit
fi

#Tell what we are doing
echo
echo Checking package $1.
pause

#Does target list exist?  If it does, delete it.
if [ -e $env_scratch/checkpkg.dat ]; then

   rm $env_scratch/checkpkg.dat
fi

#Make the package list
installpkg -warn $1 | skip - "important before you install this package:" > $env_scratch/checkpkg.dat

#Show user the package list
echo
echo Displaying list of what is in the package.
pause
echo
cat $env_scratch/checkpkg.dat

#Use the package list to check for collisions
echo
echo Checking for collisions.
pause
echo
for file in `cat $env_scratch/checkpkg.dat | grab 6`; do

   if [ -e /$file ]; then

      echo Collision: $file

   else

      echo OK: $file

   fi
done
