#!/bin/sh
#
# install a PCP *.dmg package from the command line ...
# run from the build/mac directory in a PCP build tree after Makepkgs
#
tmp=`mktemp -d /var/tmp/pcp.XXXXXXXXX` || exit 1
sts=1
trap "rm -rf $tmp; exit \$sts" 0 1 2 3 15

disk_image=`echo pcp-*.dmg | sed -e 's/$/ /' -e 's/[^ ]*\.tmp\.dmg / /' -e 's/ $//'`
if [ "$disk_image" = "pcp-*.dmg" ]
then
    echo "Error: no .dmg file?"
    exit
fi

n=`echo "$disk_image" | wc -w | sed -e 's/ //'`
if [ "$n" -gt 1 ]
then
    echo "Error: more than one .dmg file: $disk_image"
    exit
fi

base=`echo $disk_image | sed -e 's/\.dmg$//'`
if hdiutil attach $disk_image >$tmp/out 2>&1
then
    :
else
    cat $tmp/out
    echo "Error: hdiutil attach failed"
    exit
fi

if [ -d /Volumes/$base/$base.pkg ]
then
    if sudo installer -verbose -pkg /Volumes/$base/$base.pkg -target / >$tmp/out 2>&1
    then
	sts=0
    else
	cat $tmp/out
	echo "Error: installer failed"
    fi
else
    ls -l /Volumes/$base/
    echo "Error: no package directory /Volumes/$base/$base.pkg?"
    exit
fi

if hdiutil detach /Volumes/$base >$tmp/out 2>&1
then
    :
else
    cat $tmp/out
    echo "Error: hdiutil detach failed"
    sts=1
    exit
fi
