#! /bin/sh

if test $1 = "prefix"; then
	prefix=$2
	shift
	shift
fi

if test -w "$1" -a ! "$1" = ""; then
  MAGIC="$1"
elif test -w ${prefix}/usr/share/magic; then
  MAGIC="${prefix}/usr/share/magic"
elif test -w ${prefix}/usr/local/share/magic; then
  MAGIC="${prefix}/usr/local/share/magic"
elif test -w ${prefix}/etc/magic; then
  MAGIC="${prefix}/etc/magic"
else
  echo "Cannot find magic file to patch."
  exit
fi

if grep "^[^#].*belong" $MAGIC 2>/dev/null >/dev/null; then
  LONG=belong
  SHORT=beshort
else
  LONG=long
  SHORT=short
fi

if grep "\\\\b" $MAGIC 2>/dev/null >/dev/null; then
  BS="\\b"
else
  BS=""
fi

if grep DACT $MAGIC 2>/dev/null >/dev/null; then
	exit
else
echo "

#------------------------------------------------------------------------------
# dact:  file(1) magic for DACT compressed files
#
0	$LONG		0x444354C3	DACT compressed data
>4	byte		>-1		(version %i.
>5	byte		>-1		$BS%i.
>6	byte		>-1		$BS%i)
>7	$LONG		>0		$BS, original size: %i bytes
>15	$LONG		>30		$BS, block size: %i bytes
" >> $MAGIC
fi
