#!/bin/sh

#if [ $# != 3 ]; then
# echo "usage: mkdmg appname version"
# exit 0
#fi

APPNAME="$1"
VERSION="$2"
FILES="$3"

APPNAME="QCTools"
VERSION="1.4"
SIGNATURE="MediaArea.net"

DMG="tmp-${APPNAME}.dmg"
FILES="tmp-${APPNAME}"

# Create the package
rm -f "{FILES}"
mkdir "${FILES}"
mkdir "${FILES}/${APPNAME}.app"
cp -R "../QtCreator/${APPNAME}.app" "${FILES}/"
sed -i '' -e "s/VERSION/${VERSION}/g" "${FILES}/${APPNAME}.app/Contents/Info.plist"

macdeployqt "${FILES}/${APPNAME}.app"

# Qt 5.3 doesn’t handle the new version of Apple
# signatures (Mac 10.9+), so we must determin which Qt
# frameworks is used by MediaConch and sign them manually.
for FRAMEWORK in `ls "${FILES}/${APPNAME}.app"/Contents/Frameworks |grep framework | sed "s/\.framework//"` ; do
    pushd "${FILES}/${APPNAME}.app/Contents/Frameworks/${FRAMEWORK}.framework"
    # Despite their misleading names, these directories
    # generated by macdeployqt must be deleted, or codesign
    # will fail.
    rm -fr _CodeSignature
    rm -fr Versions/Current/_CodeSignature
    # The trailing slash saga continues… codesign will
    # fail with "ln -s 5/ Current".
    ln -s 5 Versions/Current
    cp "${HOME}/Qt/5.3/clang_64/lib/${FRAMEWORK}.framework/Contents/Info.plist" Resources
    mv Resources Versions/Current
    ln -s Versions/Current/${FRAMEWORK}
    ln -s Versions/Current/Resources Resources
    if [ "$FRAMEWORK" = "QtPrintSupport" ] ; then
        sed -i '' 's/_debug//g' Resources/Info.plist
    fi
    popd
    codesign -f -s "Developer ID Application: ${SIGNATURE}" --verbose "${FILES}/${APPNAME}.app/Contents/Frameworks/${FRAMEWORK}.framework"
done

find "${FILES}/${APPNAME}.app/Contents/PlugIns" "${FILES}/${APPNAME}.app/Contents/Ressources" -name "*.dylib" -exec codesign -f -s "Developer ID Application: ${SIGNATURE}" --verbose "{}" \;

codesign -f -s "Developer ID Application: ${SIGNATURE}" --verbose "${FILES}/${APPNAME}.app/Contents/MacOS/${APPNAME}"
codesign -f -s "Developer ID Application: ${SIGNATURE}" --verbose "${FILES}/${APPNAME}.app"

cp ../../License.html "${FILES}/License.html"
cp ../../History.txt "${FILES}/History.txt"
mkdir "${FILES}/.background"
cp Logo_White.icns "${FILES}/.background/Logo_White.icns"

# pkg creation
productbuild --component "${FILES}/${APPNAME}.app" /Applications --sign "3rd Party Mac Developer Installer: ${SIGNATURE}" --product "${FILES}/${APPNAME}.app/Contents/Info.plist" "${APPNAME}.pkg"


# Create the disk image
hdiutil create "$DMG" -ov -format UDRW -volname "${APPNAME}" -srcfolder "${FILES}"
DEVICE=$(hdiutil attach -readwrite -noverify "${DMG}" | egrep '^/dev/' | sed 1q | awk '{print $1}')
echo $DEVICE
sleep 2
pushd /Volumes/"${APPNAME}"
ln -s /Applications
rm .DS_Store
popd
echo '
   tell application "Finder"
     tell disk "'${APPNAME}'"
           open
           set current view of container window to icon view
           set toolbar visible of container window to false
           set the bounds of container window to {400, 100, 962, 521}
           set viewOptions to the icon view options of container window
           set arrangement of viewOptions to not arranged
           set icon size of viewOptions to 72
           set background picture of viewOptions to file ".background:Logo_White.icns"
           set position of item "QCTools.app" of container window to {125, 175}
           set position of item "Applications" of container window to {275, 200}
           set position of item "License.html" of container window to {450, 175}
           set position of item "History.txt" of container window to {450, 275}
           close
     end tell
   end tell
' | osascript
hdiutil detach "${DEVICE}"
sleep 2

# convert to compressed image, delete temp image
rm -f "${APPNAME}_${KIND}.dmg"
hdiutil convert "$DMG" -format UDBZ -o "${APPNAME}.dmg"
codesign -f -s "Developer ID Application: ${SIGNATURE}" --verbose "${APPNAME}.dmg"
rm -f "{FILES}"
rm -f "$DMG"
