#!/bin/sh

#
# This script is used to help identify whether a given file is a
# PDB file for an HDF5 file. It appends magic lines to an existing
# magic file and uses the combined result in the file command.
#

if test -n "$TMPDIR"; then
    tmpDir=$TMPDIR
else
    if test -d /tmp; then
        tmpDir=/tmp
    elif test -d /usr/tmp; then
        tmpDir=/usr/tmp
    elif test -d /var/tmp; then
        tmpDir=/var/tmp
    else
        tmpDir=.
    fi
fi

tmpMagicFile=$tmpDir/.silofile_magic_$$
rm -f $tmpMagicFile

#
# add some lines to the magic file for detecting PDB and HDF5
# note since the HDF5 line is detecting a 'long' type, we include
# two entries for big and small endian. Tabs in these strings
# are required.
#
echo "0		string		\!<<PDB:II>>\!		Portable Database (PDB) File" >> $tmpMagicFile
echo "0		long		=0x46444889		Hierarchical Data Format (HDF) file" >> $tmpMagicFile
echo "0		long		=0x28484446		Hierarchical Data Format (HDF) file" >> $tmpMagicFile

if test -e /usr/share/magic; then
    cat /usr/share/magic >> $tmpMagicFile
fi


# the file command as a bad habit of printing on stderr a message
# indicating that a different magic file is being used and then
# giving that file's name. We filter it from the output here.
file -m $tmpMagicFile $* 2>&1 | grep -v $tmpMagicFile

rm -f $tmpMagicFile
