#!/bin/bash

#	sudo ./dopackage srcapp appname package_name title [identity_app [identity_install]]
#
#	Package the virtual printer for mac
#
#	Put the srcapp (ex nvp.app) in a directory with this file and run the command via sudo from a terminal
#	Xcode must be installed on the target machine
#	If signature is used, certificates must be installed on the target machine
#
#	srcapp: name of the virtual printer bundle (ex.: nvp.app)
#	appname: name of the target application without the .app extension (ex.: mynvp)
#	package_name: name of the final package (ex.: mynvp.pkg)
#	title: title displayed in the installation program (ex.: My nvp by Nirva Software)
#	identity_app (optional): identity of the certificate used to sign the application (ex.: Developer ID Application: NIRVA SOFTWARE) 
#	identity_install (optional): identity of the certificate used to sign the installation package (ex.: Developer ID Installer: NIRVA SOFTWARE) 
#
#	example:
#	sudo ./dopackage nvp.app mynvp mynvp.pkg "My Nvp by Nirva Software" "Developer ID Application: NIRVA SOFTWARE" "Developer ID Installer: NIRVA SOFTWARE"


# copy package
rm -rf $2.app
cp -R -P -p $1 $2.app

# do signature if required
if [ -n "$5" ]
then

echo "signing frameworks"
codesign --force --verify --sign "$5" $2.app/Contents/Frameworks/QtCore.framework/Versions/4
codesign --force --verify --sign "$5" $2.app/Contents/Frameworks/QtGui.framework/Versions/4
codesign --force --verify --sign "$5" $2.app/Contents/Frameworks/QtNetwork.framework/Versions/4

echo "signing plugins"
codesign --force --verify --sign "$5" $2.app/Contents/PlugIns/imageformatss/libqtiff.dylib
codesign --force --verify --sign "$5" $2.app/Contents/PlugIns/imageformatss/libqtga.dylib
codesign --force --verify --sign "$5" $2.app/Contents/PlugIns/imageformatss/libqmng.dylib
codesign --force --verify --sign "$5" $2.app/Contents/PlugIns/imageformatss/libqjpeg.dylib
codesign --force --verify --sign "$5" $2.app/Contents/PlugIns/imageformatss/libqico.dylib
codesign --force --verify --sign "$5" $2.app/Contents/PlugIns/imageformatss/libqgif.dylib

codesign --force --verify --sign "$5" $2.app/Contents/PlugIns/codecs/libqtwcodecs.dylib
codesign --force --verify --sign "$5" $2.app/Contents/PlugIns/codecs/libqkrcodecs.dylib
codesign --force --verify --sign "$5" $2.app/Contents/PlugIns/codecs/libqjpcodecs.dylib
codesign --force --verify --sign "$5" $2.app/Contents/PlugIns/codecs/libqcncodecs.dylib

codesign --force --verify --sign "$5" $2.app/Contents/PlugIns/bearer/libqgenericbearer.dylib
codesign --force --verify --sign "$5" $2.app/Contents/PlugIns/bearer/libqcorewlanbearer.dylib

codesign --force --verify --sign "$5" $2.app/Contents/PlugIns/accessible/libqtaccessiblewidgets.dylib


for file in $2.app/Contents/MacOS/*
do
    if [[ -f $file ]]; then
    	echo "signing file $file"
		codesign --force --verify --verbose=0 --sign "$5" $file 2> /dev/null
    fi
done


echo "signing bundle $2.app"
codesign --force --verify --sign "$5" $2.app


echo "verify application signature for gatekeeper"
# uncomment next line to check some errors
# codesign --verbose=4 --deep --strict $2.app
spctl --assess --verbose=4 --type execute $2.app

fi


# Copy app to root directory
mkdir ./root
rm -rf ./root/*
cp -R $2.app ./root

# set postinstall file
mkdir ./scripts
rm -rf ./scripts/*
echo '#!/bin/bash' > ./scripts/postinstall
echo "export DYLD_LIBRARY_PATH=\$2/$2.app/Contents/MacOS" >> ./scripts/postinstall
echo "export DYLD_FRAMEWORK_PATH=\$2/$2.app/Contents/Frameworks" >> ./scripts/postinstall
echo "\"\$2/$2.app/Contents/MacOS/nvp\" -m install -s -c \"\$2/$2.app/Contents/MacOS/config.txt\" -d \"\$2/$2.app/Contents/MacOS\" -l /tmp/nvpinstall.log" >> ./scripts/postinstall
echo 'exit $?' >> ./scripts/postinstall
chmod ugoa+x ./scripts/postinstall

# set tmp.plist file
echo '<?xml version="1.0" encoding="UTF-8"?>' > ./tmp.plist
echo '<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">' >> ./tmp.plist
echo '<plist version="1.0">' >> ./tmp.plist
echo '<array>' >> ./tmp.plist
echo '	<dict>' >> ./tmp.plist
echo '		<key>BundleHasStrictIdentifier</key>' >> ./tmp.plist
echo '		<true/>' >> ./tmp.plist
echo '		<key>BundleIsRelocatable</key>' >> ./tmp.plist
echo '		<false/>' >> ./tmp.plist
echo '		<key>BundleIsVersionChecked</key>' >> ./tmp.plist
echo '		<false/>' >> ./tmp.plist
echo '		<key>BundleOverwriteAction</key>' >> ./tmp.plist
echo '		<string>upgrade</string>' >> ./tmp.plist
echo '		<key>RootRelativeBundlePath</key>' >> ./tmp.plist
echo "		<string>$2.app</string>" >> ./tmp.plist
echo '	</dict>' >> ./tmp.plist
echo '</array>' >> ./tmp.plist
echo '</plist>' >> ./tmp.plist


# build intermediate package
pkgbuild --root ./root --scripts ./scripts --component-plist ./tmp.plist --identifier com.nirva-software.$2 --install-location /Applications --ownership preserve tmp.pkg



# Set distribution file
echo '<?xml version="1.0" encoding="UTF-8"?>' > ./distribution.xml
echo '<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">' >> ./distribution.xml
echo '<installer-gui-script minSpecVersion="1">' >> ./distribution.xml
echo "	<title>$4</title>" >> ./distribution.xml
echo '	<organization>com.nirva-software</organization>' >> ./distribution.xml
echo '	<domains enable_anywhere="true"/>' >> ./distribution.xml
echo '	<os-version min="10.9"/>' >> ./distribution.xml
echo '	<options customize="never" require-scripts="true" hostArchitectures="x86_64" />' >> ./distribution.xml
echo '	<!-- List all component packages -->' >> ./distribution.xml
echo "	<pkg-ref id=\"com.nirva-software.$2\" version=\"0\" auth=\"root\">tmp.pkg</pkg-ref>" >> ./distribution.xml
echo '	<choices-outline>' >> ./distribution.xml
echo "		<line choice=\"com.nirva-software.$2\"/>" >> ./distribution.xml
echo '	</choices-outline>' >> ./distribution.xml
echo '	<!-- Define each choice above -->' >> ./distribution.xml
echo '	<choice' >> ./distribution.xml
echo "		id=\"com.nirva-software.$2\"" >> ./distribution.xml
echo '		start_selected="true">' >> ./distribution.xml
echo "		<pkg-ref id=\"com.nirva-software.$2\"/>" >> ./distribution.xml
echo '	</choice>' >> ./distribution.xml
echo '</installer-gui-script>' >> ./distribution.xml


# Do final package
if [ -n "$6" ]
then
productbuild --distribution distribution.xml --sign "$6" $3
else
productbuild --distribution distribution.xml $3
fi
rm ./tmp.pkg
