#!/bin/sh
#
# Copyright (c) 1997,2003 Silicon Graphics, Inc.  All Rights Reserved.
# 
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
# 
# This program is distributed in the hope that it would be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# 
# Further, this software is distributed without any warranty that it is
# free of the rightful claim of any third person regarding infringement
# or the like.  Any license provided herein, whether implied or
# otherwise, applies only to this software file.  Patent licenses, if
# any, provided herein do not apply to combinations of this program with
# other software, or any other product whatsoever.
# 
# You should have received a copy of the GNU General Public License along
# with this program; if not, write the Free Software Foundation, Inc., 59
# Temple Place - Suite 330, Boston MA 02111-1307, USA.
# 
# Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
# Mountain View, CA  94043, or:
# 
# http://www.sgi.com 
# 
# For further information regarding this notice, see: 
# 
# http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
#
# Use the file "magic" in this directory to update the system magic file
#
# $Id: update-magic,v 1.5 2003/06/11 10:19:02 tleask Exp $

# source the PCP configuration environment variables
. /etc/pcp.env

status=1	# presume failure
tmp=/tmp/$$
trap "rm -f $tmp.*; exit \$status" 0 1 2 3 15

MAGICFILE=$PCP_MAGIC_FILE
[ ! -f "$MAGICFILE" ] && MAGICFILE=/usr/lib/magic
[ ! -f "$MAGICFILE" ] && exit 0 # give up

MAGICINPUT=$PCP_VAR_DIR/config/magic/magic

if [ ! -f $MAGICFILE ]
then
    echo "$0: Error: cannot find \"$MAGICFILE\": No such file or directory"
    exit
fi

if [ ! -f $MAGICINPUT ]
then
    echo "$0: Error: cannot find \"$MAGICINPUT\": No such file or directory"
    exit
fi

if [ -x /sbin/suattr ]
then
    if /sbin/suattr -C CAP_MAC_WRITE,CAP_DAC_WRITE+ep -c "test ! -w $MAGICFILE"
    then
        echo "$0: Error: \"$MAGICFILE\" is not writeable (1)"
        exit
    fi
else
    if [ ! -w $MAGICFILE ]
    then
        echo "$0: Error: \"$MAGICFILE\" is not writeable (1)"
        exit
    fi
fi

# build awk script to cull old stuff from $MAGICFILE
#
rm -f $tmp.cut
echo "# script for culling entries from $MAGICFILE" >$tmp.cut
echo "# made by $0 on `date`" >>$tmp.cut
grep '^#!cut' $MAGICINPUT \
| $PCP_AWK_PROG '{ pat=$3
	  for (i=4; i<=NF; i++) pat = pat " " $i
	  printf "/%s/\t{ skip = 1; next }\n",pat
	}' >>$tmp.cut
echo >>$tmp.cut '
skip == 1 && /^[^>]/	{ skip = 0 }
skip == 0		{ print }'

# cut
#
$PCP_AWK_PROG -f $tmp.cut $MAGICFILE >$tmp.magic

# paste
#
case "$PCP_PLATFORM"
in
    irix)
	grep -v '^#!' $MAGICINPUT >>$tmp.magic
	;;
    linux)
	#
	# put PCP stuff before C++ rules for bugs #818381 and #815326
	#
	$PCP_AWK_PROG '/^# c-lang/ { exit } { print }' $tmp.magic >$tmp.newmagic
	grep -v '^#!' $MAGICINPUT \
	| sed -e 's/long/belong/g' -e 's/addr	x/string	>\\0/g' >>$tmp.newmagic
	$PCP_AWK_PROG '/^# c-lang/ { started=1 } started { print }' $tmp.magic >>$tmp.newmagic
	mv $tmp.newmagic $tmp.magic
    	;;
    solaris)
        #
        # note: L109 the >\0 null check fails on solaris so no string is returned
        #
	$PCP_AWK_PROG '/^# c-lang/ { exit } { print }' $tmp.magic >$tmp.newmagic
	grep -v '^#!' $MAGICINPUT \
	| sed -e 's/addr	x/string	>\\0/g' >>$tmp.newmagic
	$PCP_AWK_PROG '/^# c-lang/ { started=1 } started { print }' $tmp.magic >>$tmp.newmagic
	mv $tmp.newmagic $tmp.magic
    	;;
    *)
	echo "$0: Error: unknown host platform \"$PCP_PLATFORM\""
	exit 1
	;;
esac

# changes?
#
if cmp -s $MAGICFILE $tmp.magic
then
    :
else
    if [ -x /sbin/suattr ]
    then
        if /sbin/suattr -C CAP_MAC_WRITE,CAP_DAC_WRITE+ep -c "test ! -w $MAGICFILE"
        then
  	    echo "$0: Error: cannot update \"$MAGICFILE\": Permission denied"
	    exit
        fi
        # install new $MAGICFILE
        #
        /sbin/suattr -C CAP_MAC_WRITE,CAP_DAC_WRITE+ep -c "cp $tmp.magic $MAGICFILE"
    else
        if [ ! -w $MAGICFILE ]
	then
  	    echo "$0: Error: cannot update \"$MAGICFILE\": Permission denied"
	    exit
        fi
        # install new $MAGICFILE
        #
        cp $tmp.magic $MAGICFILE	
    fi

    # recompile magic file on linux, ignore any errors
    [ "$PCP_PLATFORM" = "linux" ] && file -C >/dev/null 2>&1
fi

status=0
