#!/bin/sh
#
# $RCSfile$
#

if [ ! -d "adm" ]; then
     echo "You must be running this script from the topdir directory."
     exit 1;
fi

# Package for simple binary release.
VERSION=`adm/get_version`
echo "Making binary release version $VERSION"

XXDIFF=xxdiff

PLAT=`adm/config.guess`
case $PLAT in
        *sparc*)
        PEXT=sparc ;;

        *mips*)
        PEXT=mips ;;

        *linux*)
        PEXT=i386 ;;

        *apple-darwin*)
        PEXT=osx 
        BUNDLE=xxdiff.app
        XXDIFF=$BUNDLE/Contents/MacOS/xxdiff
        ;;
esac
    
PDIR=xxdiff-$VERSION.$PEXT
ROOT=..

mkdir $ROOT/$PDIR
cp -rf bin/* $ROOT/$PDIR

if [ $PEXT = osx ]; then
    rm -f $ROOT/$PDIR/*.dmg # the one created by make deploy
    if [ ! -e $ROOT/$PDIR/$BUNDLE/Contents/Resources/qt.conf ]; then
        # Copy all required Qt libs in the bundle
        macdeployqt $ROOT/$PDIR/$BUNDLE
        # Strip 32bit part of the libraries, to just keep the x86_64 part
        for lib in `find $ROOT/$PDIR/$BUNDLE -type f -name '*.dylib'; find $ROOT/$PDIR/$BUNDLE/Contents/Frameworks -type f -name 'Qt*'`; do
            if [ `lipo $lib -info | grep -c 'Non-fat'` != "1" ]; then
                lipo $lib -thin x86_64 -output $lib
            fi
        done
    fi
fi

strip $ROOT/$PDIR/$XXDIFF

cp src/xxdiff.1 $ROOT/$PDIR
cp README $ROOT/$PDIR
cp CHANGES $ROOT/$PDIR

$ROOT/$PDIR/$XXDIFF --help-html > $ROOT/$PDIR/xxdiff-doc.html

cd ..
if [ $PEXT != osx ]; then
    tar cvf - $PDIR | gzip > $PDIR.tar.gz
else
    hdiutil create -ov -fs HFS+ -srcfolder $PDIR -volname "xxdiff $VERSION" $PDIR.dmg
fi

rm -rf $PDIR
