#!/bin/sh

echo "Starting build process..."



if [ -a TextSweep.app ]
	then
	rm -rf TextSweep.app
fi


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

	
if [ -a build/TextSweep.app ]
	then
	rm -rf build/TextSweep.app
fi

#generate new plist file
echo "Generating plist file..."



cat << EOT > Info-cbk.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>CFBundleExecutable</key>
	<string>TextSweep</string>
	<key>CFBundleGetInfoString</key>
	<string>TextSweep $1 (c) 2018 WordTech Communications LLC</string>
        <key>NSPrincipalClass</key>
        <string>NSApplication</string>
<key>NSHighResolutionCapable</key>
	<true/>
	<key>NSAppleScriptEnabled</key>
	<true/>
	<key>OSAScriptingDefinition</key>
	<string>TextSweep.sdef</string>
	<key>LSMinimumSystemVersionByArchitecture</key>
	<dict>
	<key>i386</key>
	<string>10.13</string>
	<key>x86_64</key>
	<string>10.13</string>
	</dict>
	<key>LSArchitecturePriority</key>
<array>
<string>x86_64</string>
<string>i386</string>
</array>
	<key>LSApplicationCategoryType</key>
	<string>public.app-category.utilities</string>
	<key>NSHumanReadableCopyright</key>
	<string>(c) 2018 WordTech Communications LLC</string>
	<key>CFBundleIconFile</key>
	<string>textsweep.icns</string>
	<key>CFBundleInfoDictionaryVersion</key>
	<string>6.0</string>
	<key>CFBundleDocumentTypes</key>
	<array>
		<dict>
 <key>CFBundleTypeName</key>
			<string>File name</string>
			<key>CFBundleTypeRole</key>
			<string>Editor</string>
			<key>LSItemContentTypes</key>
			<array>
				<string>public.directory</string>
				<string>com.apple.bundle</string>
				<string>com.apple.resolvable</string>
			</array> 
		</dict>
	</array>
	<key>CFBundleName</key>
	<string>TextSweep</string>
	<key>CFBundleDisplayName</key>
	<string>TextSweep</string>
	<key>CFBundleIdentifier</key>
	<string>com.codebykevin.com.textsweep-cbk</string>
	<key>CFBundlePackageType</key>
	<string>APPL</string>
	<key>CFBundleShortVersionString</key>
	<string>$1</string>
	<key>CFBundleVersion</key>
	<string>$1</string>
	<key>LSMinimumSystemVersion</key>
	<string>10.13</string>
	<key>LSRequiresCarbon</key>
	<true/>	
	<key>NSSupportsAutomaticTermination</key>
	<string>YES</string>
	<key>NSSupportsSuddenTermination</key>
	<string>YES</string>
  <key>SUFeedURL</key>
	<string>http://www.codebykevin.com/textsweep.xml</string>
</dict>
</plist>
EOT



#create app bundle shell from standalone Wish app
cp -R -f Wish.app TextSweep.app

mv TextSweep.app/Contents/MacOS/Wish TextSweep.app/Contents/MacOS/TextSweep

#copy additional files, resources to app bundle

cp -f textsweep.icns TextSweep.app/Contents/Resources/textsweep.icns
mkdir TextSweep.app/Contents/lib
cp -R -f Scripts TextSweep.app/Contents/Resources/Scripts


#copy the supporting libraries
for lib in scriptlibs/*
do
  echo "Copying $lib library..."
  cp -R -f $lib TextSweep.app/Contents/lib
done

#copy the supporting libraries
for lib in maclibs/*
do
  echo "Copying $lib library..."
  cp -R -f $lib TextSweep.app/Contents/lib
done


cp -f TextSweep.sdef TextSweep.app/Contents/Resources/TextSweep.sdef

#make website build and appstore build
echo "Making website build..."
ditto --rsrc  --arch x86_64 TextSweep.app build/TextSweep.app
cp -f Info-cbk.plist build/TextSweep.app/Contents/Info.plist


find build/TextSweep.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" build/TextSweep.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" build/TextSweep.app/Contents/Frameworks/Tk.framework/Versions/Current
codesign --verbose --signature-size 9400 -f -s "Developer ID Application: Kevin Walzer" build/TextSweep.app/Contents/Frameworks/Tcl.framework/Versions/Current
codesign  --signature-size 9400 -f -s "Developer ID Application: Kevin Walzer" --verbose=2  build/TextSweep.app


xattr -cr build/TextSweep.app

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

echo "Creating and signing DMG file..."

hdiutil create  -srcfolder build -fs HFS+ -volname TextSweep TextSweep.dmg

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

echo "Uploading DMG..."

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



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

cd ../

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



echo "Done."



