#!/bin/sh

. /etc/profile
. /etc/sysconfig/security

test "$UID" = 0 -o ! -z "$RPM_BUILD_ROOT" || {
    echo Check does not work for normal users.
    exit 1
}

do_gzip() {
	while read input; do
		if test -f "$input.gz"; then
			#echo "Remove old file $input.gz"
			rm -f "$input.gz"
		fi
		if test -L "$input"; then
			link=`perl -e 'print readlink($ARGV[0]);' -- "$input"`
			test -d "$link" && continue
			test -d "$RPM_BUILD_ROOT/$link" && continue
			rm -f "$input"
			ln -sf "$link.gz" "$input.gz"
		else
			gzip -nf9 "$input"
		fi
		ls -l "$input.gz"
	done
	return
}

DIRS="/usr/info /usr/share/info /opt/gnome/info /usr/X11R6/lib/X11/fonts/*"

OLD_IFS="$IFS"
IFS="$IFS:"

for MANDIR in $MANPATH $manpath /usr/openwin/man /opt/gnome/man ; do
    DIRS="$DIRS $MANDIR"
done

IFS="$OLD_IFS"

if test -n "$RPM_BUILD_ROOT" ; then
    TDIRS=""
    for DIR in $DIRS ; do
        test "$UID" = 0 && TDIRS="$TDIRS $DIR"
        TDIRS="$TDIRS ${RPM_BUILD_ROOT%/}/$DIR"
    done
    DIRS="$TDIRS"
fi


for DIR in $DIRS ; do
    test -d $DIR && find $DIR -name "*.Z" -type f | \
        xargs --no-run-if-empty gunzip
done

for DIR in $DIRS ; do
    case "$DIR" in
	*/usr/X11R6/lib/X11/fonts/CID) continue ;;
	*/usr/X11R6/lib/X11/fonts/truetype) continue ;;
	*/usr/X11R6/lib/X11/fonts/TTF) continue ;;
    esac
    test -d $DIR && find $DIR \! -name "*.gz" \! -name "*.Z" \! -name "*.bz2" \
        \! -name dir \! -name dir.old \! -name localdir \
        \! -name whatis \! -name fonts.alias \! -name fonts.dir \
        \! -name "fonts.*" \! -name ".fonts-*" \
	\! -name "*.spd" \! -name "*.pfa" \! -name "*.pfb" \
	\! -name "*.pfm" \! -name "*.phont" \! -name ".*.gz" \
        \! -name ".*.Z" \! -name ".*.spd" \! -name ".*.pfa" \
        \! -name ".*.pfb" \! -name ".*.phont" \! -name URW \
        \! -name "*.ttf" \! -name "*.TTF" \! -name "encodings.dir" \
        \! -name "XftConfig" \! -name "*.TXT" \! -name "*.rpmorig" \
        \! -name "map-*-*" \! -name "*.afm" \! -name "*.exe" \
	\! -name "*.EXE" \! -name "XftCache" \! -path "*/CVS/*" \
	\! -path "*/RCS/*" \! -path "*/.svn/*" \! -path "*/SCCS/*" \
        \! -type d | do_gzip
done


TMPFILE=`mktemp /tmp/Check.perms.XXXXXX`

if test -n "$CHECK_PERMISSIONS" -a -e /etc/permissions -a -x /usr/bin/chkstat ; then
    if test "$UID" = 0 -o -n "$RPM_BUILD_ROOT" ; then
	cat /etc/permissions > $TMPFILE
	for PERMEXT in $PERMISSION_SECURITY ; do
	    test -e /etc/permissions."$PERMEXT" &&\
		cat /etc/permissions."$PERMEXT" >> $TMPFILE
	done
	# run below RPM_BUILD_ROOT regardless of UID
	if test -n "$RPM_BUILD_ROOT" ; then
	    if test "$CHECK_PERMISSIONS" = "set" ; then
		/usr/bin/chkstat -root $RPM_BUILD_ROOT -set $TMPFILE
	    elif test "$CHECK_PERMISSIONS" = "warn" ; then
		/usr/bin/chkstat -root $RPM_BUILD_ROOT $TMPFILE
	    fi
	fi
	# run for / only if UID = 0
	if test "$UID" = 0 ; then
	    if test "$CHECK_PERMISSIONS" = "set" ; then
		/usr/bin/chkstat -set $TMPFILE
	    elif test "$CHECK_PERMISSIONS" = "warn" ; then
		/usr/bin/chkstat $TMPFILE
	    fi
	fi
    fi
fi

rm -rf $TMPFILE
