#!/bin/ash
# rustic `file` (by M. Andreoli)
# [ with dd
# (2/28/00 provincial gentrification by A. Costa)

#Syntax

opt=$1

case Z$opt in
Z-d) set -x;shift;;	# debug mode...
Z-h|Z) echo "Usage (mu-file): file [files]" ; exit ;;
*)
esac


# Functions

# compare with string

TestString()
{
f=$1
offset=$2; n=$3 ; string1="$4" 

string2=`dd if=$f skip=$offset bs=1c count=$n 2>/dev/null`
test "$string1" = "$string2"
}

# compare with octal \\0x \\0y \\0z ...

TestOctal()
{
f=$1
offset=$2; n=$3
shift 3

for code in $@
do
    p=.`echo -e $code`		# note the leading period to tame the nulls
    m=.`dd if=$f skip=$offset bs=1c count=1 2>/dev/null`
    test "$m" != "$p" && return 1
    offset=`expr $offset + 1`
done
}


for f in "$@"
do

# special
[ -d "$f" ] && echo "$f: directory" && continue
[ -L "$f" ] && echo "$f: symbolic link" && continue
#[ ! -f "$f" ] && echo "$f: not existent" && continue

# script
if TestString $f 0 1 '#'	# be lazy
then
    TestString $f 0  9 '#!/bin/sh' && echo "$f: Bourne shell script text" && continue
    TestString $f 0 10 '#!/bin/ash' && echo "$f: ash script text" && continue
    TestString $f 0 11 '#!/bin/bash' \
    && echo "$f: Bash shell script text" && continue
    TestString $f 0 10 '#!/bin/csh' && echo "$f: C shell script text" && continue
    TestString $f 0 10 '#!/bin/ksh' && echo "$f: Korn shell script text" && continue
    TestString $f 0 11 '#!/bin/perl' && echo "$f: perl command text" && continue
    TestString $f 0  7 '#!/bin/' && echo "$f: script text" && continue
fi

# Linux 

TestString $f 1 3 ELF \
&& echo -e "$f: ELF executable" && continue

TestString $f 1080 1 'S' \
&& echo "$f: Linux/i386 ext2 filesystem [probable :(]" && continue

TestString $f 4086 10 'SWAP-SPACE' \
&& echo "$f: Linux/i386 swap file" && continue

# compressed

TestString $f 257 5 ustar && echo "$f: TAR archive" && continue
TestOctal $f 0 2  \\037 \\0213 && echo "$f: gzip compress data" && continue
TestString $f 0 2 BZ && echo "$f: bzip compressed data" && continue
TestString $f 0 2 PK && echo "$f: Zip archive data" && continue
TestString $f 0 4 'Rar!' && echo "$f: RAR archive data" && continue

# text
TestString $f 0 5 '%PDF-' && echo "$f: PDF document" && continue

TestString $f 0 2 '%!' \
&& echo "$f: PostScript document text" && continue

TestOctal $f 0 2 \\0367 \002 && echo "$f: TeX DVI file" && continue

# Audio
TestString $f 0 4 MThd  && echo "$f: Standard MIDI data" && continue

if TestString $f 0 4 RIFF 
then
    echo -n "$f: Microsoft RIFF"
    if TestString $f 8 4 WAVE 
    then
	echo ", WAVE audio data" 
    else
	echo
    fi
    continue
fi


# image
TestOctal $f 0 2 \\0377 \\0330 && echo "$f: JPEG image data" && continue 
TestString $f 0 4 GIF8 && echo "$f: GIF image data" && continue
TestString $f 0 2 BM && echo "$f: PC bitmap data" && continue

TestOctal $f 0 4 \\0115 \\0115 \\0 \\052 \
&& echo "$f: TIFF image data, big-endian" && continue

TestOctal $f 0 4 \\0111 \\0111 \\052 \\0 \
&& echo "$f: TIFF image data, little-endian" && continue


# binary
TestString $f 0 2 'MZ' && echo "$f: MS-DOS executable (EXE)" && continue

# HP48
TestString $f 0 7 'HPHP48-' && echo "$f: HP48 binary" && continue
TestString $f 0 5 '%%HP:' && echo "$f: HP48 text" && continue


echo "$f: ASCII text or data"
done
