#!/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 tempsource
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
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
           set the bounds of container window to {400, 100, 885, 430}
           set theViewOptions to the icon view options of container window
           set arrangement of theViewOptions to not arranged
           set icon size of theViewOptions to 72
           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"}
           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
hdiutil detach /Volumes/PETSc
