#!/bin/sh

echo "Starting dist process..."

if [ -a QuickWho.dmg ]
 then
    rm QuickWho.dmg
fi

if [ -a dist ]
	then
	rm -rf dist
fi

if [ -a build ]
then
    rm -rf build
fi


cat << EOT > Info.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>CFBundleDevelopmentRegion</key>
	<string>English</string>
	<key>CFBundleDisplayName</key>
	<string>QuickWho</string>
	<key>CFBundleExecutable</key>
	<string>_quickWho</string>
	<key>CFBundleGetInfoString</key>
	<string>QuickWho $1</string>
	<key>CFBundleIconFile</key>
	<string>QuickWho.icns</string>
	<key>CFBundleIdentifier</key>
	<string>com.codebykevin.quickwho</string>
	<key>CFBundleInfoDictionaryVersion</key>
	<string>6.0</string>
	<key>CFBundleName</key>
	<string>QuickWho</string>
	<key>CFBundlePackageType</key>
	<string>APPL</string>
	<key>CFBundleShortVersionString</key>
	<string>$1</string>
	<key>CFBundleSignature</key>
	<string>????</string>
	<key>CFBundleVersion</key>
	<string>$1</string>
	<key>LSApplicationCategoryType</key>
	<string>public.app-category.utilities</string>
	<key>LSArchitecturePriority</key>
	<string>x86_64</string>
	<key>LSHasLocalizedDisplayName</key>
	<false/>
	<key>LSMinimumSystemVersion</key>
	<string>10.13</string>
	<key>LSMinimumSystemVersionByArchitecture</key>
	<dict>
		<key>x86_64</key>
		<string>10.13</string>
	</dict>
	<key>NSAppleScriptEnabled</key>
	<string>YES</string>
	<key>NSHighResolutionCapable</key>
	<string>YES</string>
	<key>NSHumanReadableCopyright</key>
	<string>Copyright (c) 2018 WordTech Communications LLC</string>
	<key>NSMainNibFile</key>
	<string>MainMenu</string>
	<key>NSPrincipalClass</key>
	<string>NSApplication</string>
	<key>NSServices</key>
	<array>
		<dict>
			<key>NSMenuItem</key>
			<dict>
				<key>default</key>
				<string>QuickWho: Display Domain Information</string>
			</dict>
			<key>NSMessage</key>
			<string>provideService</string>
			<key>NSPortName</key>
			<string>QuickWho</string>
			<key>NSSendTypes</key>
			<array>
				<string>NSStringPboardType</string>
			</array>
		</dict>
	</array>
	<key>OSAScriptingDefinition</key>
	<string>QuickWho.sdef</string>
</dict>
</plist>
EOT

gcc main.m -o _quickwho -framework Carbon -framework Cocoa

mkdir dist
mkdir dist/QuickWho.app
mkdir dist/QuickWho.app/Contents
mkdir dist/QuickWho.app/Contents/MacOS
mkdir dist/QuickWho.app/Contents/Resources
mkdir dist/QuickWho.app/Contents/Frameworks

cp Info.plist dist/QuickWho.app/Contents/Info.plist
cp _quickwho dist/QuickWho.app/Contents/MacOS/_quickwho
cp launcher dist/QuickWho.app/Contents/Resources/launcher
cp QuickWho.icns dist/QuickWho.app/Contents/Resources/QuickWho.icns
cp QuickWho.sdef dist/QuickWho.app/Contents/Resources/QuickWho.sdef
cp QuickWho.py  dist/QuickWho.app/Contents/Resources/QuickWho.py
cp ToolTip.py  dist/QuickWho.app/Contents/Resources/ToolTip.py

#copy the frameworks
cp -R -f /Library/Frameworks/Tcl.framework  dist/QuickWho.app/Contents/Frameworks/Tcl.framework
cp -R -f /Library/Frameworks/Tk.framework  dist/QuickWho.app/Contents/Frameworks/Tk.framework
cp -R -f /Library/Frameworks/Python.framework  dist/QuickWho.app/Contents/Frameworks/Python.framework
cp -f dist/QuickWho.app/Contents/Frameworks/Python.framework/Versions/3.6/Resources/Python.app/Contents/MacOS/Python dist/QuickWho.app/Contents/Resources/QuickWho
cp -f /usr/local/lib/libcrypto.42.dylib  dist/QuickWho.app/Contents/Frameworks/libcrypto.42.dylib
cp -f /usr/local/lib/libssl.44.dylib  dist/QuickWho.app/Contents/Frameworks/libssl.44.dylib 


#copy the supporting libraries
for lib in scriptlibs/*
do
  echo "Copying $lib library..."
  cp -R -f $lib dist/QuickWho.app/Contents/Frameworks/Tcl.framework/Versions/8.6/Resources/Scripts
done

#copy the supporting libraries
for lib in maclibs/*
do
  echo "Copying $lib library..."
  cp -R -f $lib dist/QuickWho.app/Contents/Frameworks/Tcl.framework/Versions/8.6/Resources/Scripts
done



find dist/QuickWho.app  -name "*.a"  -exec rm -rf {} \;
find dist/QuickWho.app  -name "*debug"  -exec rm -rf {} \;
find dist/QuickWho.app  -name "*.sh"  -exec rm -rf {} \; 

find dist/QuickWho.app -type f -name "*.so"  -exec strip -x {} \;
find dist/QuickWho.app -type f -name "*.so"  -exec codesign --verbose --signature-size 9400 -f -s "Developer ID Application: Kevin Walzer" {} \;
find dist/QuickWho.app -type f -name "*.dylib"  -exec strip -x  {} \;
find dist/QuickWho.app -type f -name "*.dylib"  -exec codesign --verbose --signature-size 9400 -f -s "Developer ID Application: Kevin Walzer" {} \; 

codesign  --verbose --signature-size 9400 -f -s "Developer ID Application: Kevin Walzer" dist/QuickWho.app/Contents/Frameworks/Tk.framework/Versions/8.6/Resources/Wish.app/Contents/MacOS/Wish
codesign  --verbose --signature-size 9400 -f -s "Developer ID Application: Kevin Walzer" dist/QuickWho.app/Contents/Frameworks/Tk.framework/Versions/Current
codesign --verbose --signature-size 9400 -f -s "Developer ID Application: Kevin Walzer" dist/QuickWho.app/Contents/Frameworks/Tcl.framework/Versions/Current
codesign --verbose --signature-size 9400 -f -s "Developer ID Application: Kevin Walzer" dist/QuickWho.app/Contents/Frameworks/Python.framework/Versions/Current
codesign  --verbose --signature-size 9400 -f -s "Developer ID Application: Kevin Walzer" dist/QuickWho.app/Contents/MacOS/_quickwho
xattr -cr dist/QuickWho.app

codesign  --signature-size 9400 -f -s "Developer ID Application: Kevin Walzer" --verbose=2  dist/QuickWho.app

cp readme.txt dist/readme.txt
cd dist
ln -s /Applications Applications
cd ../
 

echo "Creating and signing DMG file..."

hdiutil create  -srcfolder dist -fs HFS+ -volname QuickWho QuickWho.dmg

codesign  --signature-size 9400 -f -s "Developer ID Application: Kevin Walzer" --verbose=2  QuickWho.dmg

echo "Uploading DMG..."

cp -f QuickWho.dmg /Users/kevin/share/kevin/Sites/codebykevin/updates/QuickWho.dmg



cp -f quickwho-changes.tcl /Users/kevin/share/kevin/Sites/codebykevin/quickwho-changes.tcl
cp -f quickwho-version.tcl /Users/kevin/share/kevin/Sites/codebykevin/quickwho-version.tcl


cd ../

#create archive of entire source tree
tar cvfz QuickWho-$1.tgz QuickWho

echo "Done."
