#!/bin/bash
# 
# Create a disk image containing an installer package of gnue-common
#
# Usage: create-build

if [ $UID -ne 0 ];
then
  echo "Please run this script as admin (or sudo)"
  exit 1
fi

packagedir="../.."
if [ ! -d "$packagedir" ];
then
  echo "Usage: $0"
  exit 1
fi


if [ ! -f "$packagedir/setup.py" ];
then
  echo "$packagedir does not contain a setup.py file"
  exit 1
fi

pkgmaker=/Developer/Tools/packagemaker
if [ ! -x $pkgmaker ];
then
  pkgmaker=/Developer/usr/bin/packagemaker
fi

if [ ! -x $pkgmaker ];
then
  echo "No packagemaker binary found"
  exit 1
fi

# ---------------------------------------------------------------------------
# Get the package-name and version from the PKG-INFO file
# ---------------------------------------------------------------------------

pyver=`python -c "import sys; print sys.version[:3].replace('.', '')"`

volname="gnue-forms-0.6.3-py$pyver"
package="gnue-forms-py$pyver.pkg"


# ---------------------------------------------------------------------------
# Populate the package environment
# ---------------------------------------------------------------------------

curdir=`pwd`
destdir="$curdir/build-env/pkg-root"
resdir="$curdir/build-env/Resources"
pkgres="$curdir/build-env/pkg-resources"

test -d "$destdir" || mkdir "$destdir"

cd "$packagedir"
python setup.py install --root=$destdir --no-compile


# ---------------------------------------------------------------------------
# Build the Application bundle for GNUe Forms
# ---------------------------------------------------------------------------

cd $curdir
test -d "build-pyapp" && rm -rf "build-pyapp"
python setup.py py2app
test -d "build-pyapp" && rm -rf "build-pyapp"


# ---------------------------------------------------------------------------
# Apply the proper permissions to all files of the package
# ---------------------------------------------------------------------------
cd "$destdir"

echo "Applying permissions ..."

find . -print | while read filename
do
  real="/$filename"
  if [ -e "$real" ];
  then
    data=`stat -f "%u %g %p" "$real"`

    uid=`echo $data | cut -f1 -d" "`
    gid=`echo $data | cut -f2 -d" "`
    mod=`echo $data | cut -f3 -d" "`

    chmod $mod "$filename"
    chgrp $gid "$filename"
    chown $uid "$filename"
  else
    chown root "$filename"
    chgrp admin "$filename"

    if [ -d "$filename" ];
    then
      chmod 775 "$filename"
    else
      if [ -x "$filename" ]; then
        chmod 775 "$filename"
      else
        chmod 664 "$filename"
      fi
    fi
  fi
done



# ---------------------------------------------------------------------------
# Build the package
# ---------------------------------------------------------------------------
cd $curdir

echo "Building the package ..."
$pkgmaker -build -p $package -f $destdir -ds -r $resdir -i Info.plist -d Description.plist


# ---------------------------------------------------------------------------
# Get rid of the obsolte directories
# ---------------------------------------------------------------------------

rm -rf "$destdir"


# ---------------------------------------------------------------------------
# Create a disk image
# ---------------------------------------------------------------------------

cd $curdir
echo "Creating disk image ..."
rm -f temp.dmg "$volname.dmg"

# Prepare the directory tree for the disk image.  This contains the previously
# created installer-package as well as everything from build-env/pkg-resources
rm -rf "build-dmg"
mkdir "build-dmg"
cp -R "$pkgres/" "build-dmg"
mv $package "build-dmg"

hdiutil create -srcfolder "build-dmg" -volname $volname temp.dmg

# Convert the image
echo "Converting disk image ..."
hdiutil convert temp.dmg -format UDZO -o $volname.dmg

# Clean up the working directory
rm -f temp.dmg
rm -rf "build-dmg"
