#!/bin/sh
#
# This script makes a Apple Mac OS X dmg from the PETSc framework and docset, see ./makeall
#
# Make sure that "Enable access for assistive devices" is checked in System Preferences>>Universal Access. It is required for the AppleScript to work.
#
echo "Creating ${PETSC_DIR}/${PETSC_ARCH}/PETSc.dmg from ${PETSC_DIR}/${PETSC_ARCH}/PETSc.framework and ${PETSC_DIR}/${PETSC_ARCH}/PETSc.docset"
rm -rf pack.temp.dmg tempsource ${PETSC_DIR}/${PETSC_ARCH}/PETSc.dmg
mkdir tempsource
cp -r ${PETSC_DIR}/${PETSC_ARCH}/PETSc.framework ${PETSC_DIR}/${PETSC_ARCH}/PETSc.docset ${PETSC_DIR}/systems/Apple/OSX/examples tempsource
cp ${PETSC_DIR}/src/snes/examples/tutorials/ex19.c tempsource/PETSc\ command\ line\ example/examples/PETSc\ command\ line\ example
cp ${PETSC_DIR}/src/snes/examples/tutorials/ex19.c tempsource/PETSc\ cocoa\ example/examples/PETSc\ cocoa\ example

let size=`du  -c tempsource | tail -1 | cut -f1`
let size=2*$size

hdiutil create -srcfolder tempsource -volname PETSc -fs HFS+ -fsargs "-c c=64,a=16,e=16" -format UDRW -size ${size}k pack.temp.dmg
sleep 5
device=`hdiutil attach -readwrite -noverify -noautoopen "pack.temp.dmg" | head -1 | cut -f1`
echo Created device $device
sleep 5
echo '
   tell application "Finder"
     tell disk "PETSc"
           open
           set current view of container window to icon view
           set toolbar visible of container window to false
           set statusbar visible of container window to false
           make new alias file at container window to POSIX file "/Library/Frameworks" with properties {name:"Copy in PETSc.frameworks"}
           make new alias file at container window to POSIX file "/Applications/Xcode.app/Contents/Developer/Documentation/DocSets" with properties {name:"Copy in PETSc.docset"}
           make new alias file at container window to POSIX file "/Users/barrysmith/" with properties {name:"Copy in examples"}
           eject
     end tell
   end tell
' | osascript
sleep 8
sync
sync
echo 'Completed osascript'
#hdiutil detach ${device}
hdiutil convert "pack.temp.dmg" -format UDZO -imagekey zlib-level=9 -o ${PETSC_DIR}/${PETSC_ARCH}/PETSc.dmg
rm -rf pack.temp.dmg tempsource
sync
sleep 2
hdiutil detach /Volumes/PETSc

#           set the bounds of container window to {400, 100, 885, 630}
#           set theViewOptions to the icon view options of container window
#           set icon size of theViewOptions to 72
#           set arrangement of theViewOptions to not arranged
