#!/bin/bash

function signAppArray {
    array=($@)
    for i in "${array[@]}"
    do
	signAppFile "$i"
    done
}

function signAppFile {
    # sandboxing is triggered and controlled by engauge.entitlements file, which was created in 
    # code for a dummy project
    file="$1"
    echo "$file"
    codesign \
	-s "3rd Party Mac Developer Application: Mark Mitchell" \
	--entitlements dev/osx/engauge.entitlements \
	"Engauge Digitizer.app/$file"
}

function signAppFramework {
    vc=/Contents/Frameworks/$1.framework/Versions/Current/$1
    signAppFile "$vc"
}

# Make sure we are in the correct directory
if [ ! -e dev ]; then
   echo "This must be run from the root directory of Engauge (dev/osx/build_installable_package)"
   exit
fi

# Make sure environment variables are correctly set
exe=`which macdeployqt`
if [ -z "$exe" ]; then
    echo "'source dev/osx/macosx_setup' must be run before this script. Quitting"
    exit
fi

# Remove old build. Need root privileges. Note that this step implicitly removes any leftover 
# engauge.log file, which would other break the sandbox
sudo rm -rf Engauge\ Digitizer.app 2>/dev/null

# Make into Engauge Digitizer.app
qmake engauge.pro
make clean
make 

# Help
cd help
./build
cd ..
cp -r bin/documentation/engauge.* Engauge\ Digitizer.app/Contents/Resources

# Translations
lrelease engauge.pro
mkdir -p Engauge\ Digitizer.app/Contents/Resources/translations
mv translations/*.qm Engauge\ Digitizer.app/Contents/Resources/translations

# Icons
cp src/img/digitizer.icns Engauge\ Digitizer.app/Contents/Resources/

# License
cp LICENSE Engauge\ Digitizer.app/Contents/Resources/

# Debug symbols are just dropped for now
dsymutil Engauge\ Digitizer.app/Contents/MacOS/Engauge\ Digitizer -o Engauge\ Digitizer.app.dSYM

# Manually insert version numbering
cp dev/osx/Info_valid.plist Engauge\ Digitizer.app/Contents/Info.plist
egrep '>\d+\.\d' Engauge\ Digitizer.app/Contents/Info.plist
echo "If submitting to app store, then in another window..."
echo '1. <editor> Engauge\ Digitizer.app/Contents/Info.plist'
echo '2. append/add .1 to version number)'
echo '3. save the file and exit)'
read -p "Press any key to continue"
echo 'Ignore error about /usr/lib/log4cpp... in the next line'

# Insert Qt libraries. Log4cpp library does not get processed so we manually copy first. The
# error 'ERROR No file at /usr/lib/log4cpp.1.dylib' is a bug in macdeployqt that is claimed to 
# not cause any problems, but copying a library to /usr/lib or creating symlink does not work
# due to "System Integrity Protection" which prevents write access to /usr/lib. So we manually
# copy the file just after macdeployqt
macdeployqt Engauge\ Digitizer.app -appstore-compliant -dmg
cp ${LOG4CPP_HOME}/lib/liblog4cpp.1.dylib Engauge\ Digitizer.app/Contents/Frameworks

# Signing
frameworks=(QtCLucene QtCore QtGui QtHelp QtNetwork QtPrintSupport QtSql QtWidgets QtXml)
for i in "${frameworks[@]}"
do
    signAppFramework $i
done

signAppArray `find Engauge\ Digitizer.app | grep '/Frameworks/lib'           | sed 's/Engauge\ Digitizer\.app//g'`
signAppArray `find Engauge\ Digitizer.app | grep '/engauge_'                 | sed 's/Engauge\ Digitizer\.app//g'`
signAppArray `find Engauge\ Digitizer.app | grep '/PlugIns/bearer/lib'       | sed 's/Engauge\ Digitizer\.app//g'`
signAppArray `find Engauge\ Digitizer.app | grep '/PlugIns/imageformats/lib' | sed 's/Engauge\ Digitizer\.app//g'`
signAppArray `find Engauge\ Digitizer.app | grep '/PlugIns/platforms/lib'    | sed 's/Engauge\ Digitizer\.app//g'`
signAppArray `find Engauge\ Digitizer.app | grep '/PlugIns/printsupport/lib' | sed 's/Engauge\ Digitizer\.app//g'`
signAppArray `find Engauge\ Digitizer.app | grep '/PlugIns/sqldrivers/lib'   | sed 's/Engauge\ Digitizer\.app//g'`
signAppFile /Contents/MacOS/Engauge\ Digitizer

echo "******************************************************************************"
echo "* If previous line says 'code object is not signed at all' then stop and fix *"
echo "******************************************************************************"

# Build package
productbuild --component Engauge\ Digitizer.app /Applications Engauge\ Digitizer.pkg

# Sign package
productsign --sign "3rd Party Mac Developer Installer: Mark Mitchell" Engauge\ Digitizer.pkg Engauge\ Digitizer.pkg.signed
mv Engauge\ Digitizer.pkg.signed Engauge\ Digitizer.pkg

# Code signing checks. Note that spctl seems to give false alarms, while codesign seems to be happy
#spctl --assess --type execute Engauge\ Digitizer.app 2>&1 | grep -i rejected
#spctl --assess --type install Engauge\ Digitizer.app 2>&1 | grep -i rejected
echo "******************** codesign validations ************************"
codesign --verbose --deep --strict Engauge\ Digitizer.app
echo "******************** codesign entitlements ***********************"
codesign --display --verbose=4 --entitlements - Engauge\ Digitizer.app
echo "******************************************************************"

# Test the built package
echo "Testing the built package. Output should have lines like 0% complete, 20% complete, ..., 100% complete"
sudo installer -store -pkg Engauge\ Digitizer.pkg -target /

echo "Assuming the test results show '0% complete, ..., 100% complete', open Xcode and then select Xcode / Open Developer Tools / Application Loader to upload Engauge Digitizer.pkg"

