
VERSION=$(./gpsbabel -V | awk '/\./ {print $3}')
NAME=GPSBabel+-${VERSION}
UDMG=${NAME}-uncompressed.dmg
DMG=${NAME}.dmg
WORKDIR=/tmp/${NAME}-$$
QUIET=-quiet


checkgpsbabel() {
#  file $1 | grep -q "2 architectures" || {
#    echo $1 is not a universal binary.  Aborting
#    exit 1
#  }
  otool -L $1 | grep expat && {
    echo $1 does not staticly link expat.  Use tools/mac-config
    exit 1
  }
}


makedmg() {
  mkdir -p $WORKDIR
  rm -f ${UDMG} ${DMG}

  trap 'rm -fr $WORKDIR' 0 1 15

  # Create the image uncompressed based on our preloaded copy.
  hdiutil create ${QUIET} -srcfolder mac/dmg-contents -scrub -size 30m ${UDMG} -format UDRW -volname ${NAME}
  hdiutil  attach ${QUIET} ${UDMG} -noautoopen -mountpoint ${WORKDIR}

  # Since CVS loses the resource forkes, we've stored a ditto image in the 
  # repository.  Unwind that now and turn the *.app back into a "real" 
  # executable.
  (
    cd ${WORKDIR}
    bzcat GPSBabel+.app.dittoed | cpio -ivd
    rm GPSBabel+.app.dittoed
    # Clean this out of taste.
    rm -fr CVS
  )

  # Copy in the new executable.
  cp -p ./gpsbabel ${WORKDIR}
  rm -fr ${WORKDIR}/CVS
  hdiutil detach ${QUIET} ${WORKDIR}

  hdiutil convert ${QUIET} ${UDMG} -format UDZO -imagekey zlib-level=9 -o ${DMG}
}

checkgpsbabel ./gpsbabel
makedmg
