#!/bin/sh
UNAME=`uname -m`
case $UNAME in
  i*86) MCH='0000000 0.00';;
  *)    MCH='0000000  000';;
esac

case $# in
0)
  echo Usage: demac [srcfile] [destfile]
  echo        demac [srcfile] > [destfile]
  echo        If src has MacBinHeader exit 0 else exit 1
  exit 1;;
1)
  HEAD="`dd ibs=2 count=1 if=$1 2> /dev/null | od -x`"
  MC=`expr "$HEAD" : "$MCH"`
  if [ ! $MC = 12 ] ; then
    echo $1 is not MacBinary. >&2
    cat $1
    exit 1
  else
    echo $1 is MacBinary. >&2
    dd ibs=128 skip=1 if=$1 2> /dev/null
  fi;;
2)
  HEAD="`dd ibs=2 count=1 if=$1 2> /dev/null | od -x `"
  MC=`expr "$HEAD" : "$MCH"`
  if [ ! $MC = 12 ] ; then
    echo $1 is not MacBinary. >&2
    exit 1
  else
    echo $1 is MacBinary. >&2
    dd ibs=128 skip=1 if=$1 of=$2 2> /dev/null
    exit 0
  fi;;
esac
