#!/bin/sh
#
# Copyright 1997,2003 Silicon Graphics, Inc.
# ALL RIGHTS RESERVED
# 
# UNPUBLISHED -- Rights reserved under the copyright laws of the United
# States.   Use of a copyright notice is precautionary only and does not
# imply publication or disclosure.
# 
# U.S. GOVERNMENT RESTRICTED RIGHTS LEGEND:
# Use, duplication or disclosure by the Government is subject to restrictions
# as set forth in FAR 52.227.19(c)(2) or subparagraph (c)(1)(ii) of the Rights
# in Technical Data and Computer Software clause at DFARS 252.227-7013 and/or
# in similar or successor clauses in the FAR, or the DOD or NASA FAR
# Supplement.  Contractor/manufacturer is Silicon Graphics, Inc.,
# 2011 N. Shoreline Blvd. Mountain View, CA 94039-7311.
# 
# THE CONTENT OF THIS WORK CONTAINS CONFIDENTIAL AND PROPRIETARY
# INFORMATION OF SILICON GRAPHICS, INC. ANY DUPLICATION, MODIFICATION,
# DISTRIBUTION, OR DISCLOSURE IN ANY FORM, IN WHOLE, OR IN PART, IS STRICTLY
# PROHIBITED WITHOUT THE PRIOR EXPRESS WRITTEN PERMISSION OF SILICON
# GRAPHICS, INC.
#
# Use the file "magic" in this directory to update the system magic file
#
# $Id: update-magic,v 1.3 2002/10/17 05:11:47 markgw 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 [ ! -w $MAGICFILE ]
then
    echo "$0: Error: \"$MAGICFILE\" is not writeable"
    exit
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 [ ! -w $MAGICFILE ]
    then
	echo "$0: Error: cannot update \"$MAGICFILE\": Permission denied"
	exit
    fi
    # install new $MAGICFILE
    #
    cp $tmp.magic $MAGICFILE

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

status=0
