#!/bin/bash

# Program to determine if one binary file is a subset of another.  Actually,
# this script will only tell you if one file is just a truncated version
# of the other.
# 
# Written by:  Chris Studholme
# Last Update: 8-Jan-1999
# Copyright:   GPL (http://www.fsf.org/copyleft/gpl.html)

if [ ! \( -r "$1" -a -r "$2" \) ]; then
	printf "Usage:\n  %s file1 file2\n" `basename $0`
	exit 1
fi

size1=`wc -c "$1"|awk '{print $1}'`
size2=`wc -c "$2"|awk '{print $1}'`

if [ $size1 -eq $size2 ]; then
	exec diff "$1" "$2"
else
	if [ $size1 -lt $size2 ]; then
		if dd if="$2" bs=1 count=$size1 2>/dev/null |diff - "$1">/dev/null; then
			echo "$1" is a subset of "$2"
		else
			echo Binary files "$1" and "$2" differ
		fi
	else
		if dd if="$1" bs=1 count=$size2 2>/dev/null |diff - "$2">/dev/null; then
			echo "$2" is a subset of "$1"
		else
			echo Binary files "$1" and "$2" differ
		fi
	fi
fi

