#!/bin/sh -e
# Copyright 2006-2008,2015-2016 BitMover, Inc
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#
# The idea is to copy the Mac OS X bundle that Tcl created (Wish.app)
# and just replace the fields we care about (e.g. the icon, name, etc)
# We treat the Wish.app bundle as kind of a black box and just replace
# the bits we care about. So for instance, Info.plist, pkgInfo, etc
# are generated by the Tcl/Tk build and we just whack them.


USAGESTR="Usage: $0 APPNAME VERSION ABBR COMPANY URL WISHAPP"

if [ $# != 6 ]; then
	echo $USAGESTR
	exit 1
fi

appname=$1
version=$2
abbrev=$3
company=$4
url=$5
wishapp=$6

test -z "$appname" -o -z "$version" -o -z "$abbrev" && {
	echo $USAGESTR
	exit 1
}

test -z "$company" -o -z "$url" -o -z "$wishapp" && {
	echo $USAGESTR
	exit 1
}

lowabbrev=`echo "$abbrev" | tr "A-Z" "a-z"`
year=`date +%Y`
copyright="Copyright © $year by $company"
newline="\
"

origdir=`pwd`
destapp=$origdir/bin/$appname.app
rm -rf $destapp

cp -fRPH $wishapp $destapp

cd $destapp/Contents

sed "s/>Wish Shell</>$appname</" Info.plist | \
	sed "s/\(Wish Shell 8.[0-9]\), /$appname $version \
$copyright \
Based on \1/;
	s/Wish.icns/$abbrev.icns/ ; 
	s/com.tcltk.wish/$url/ ;
	s/>Wish</>$appname</ ;
	s/>WiSH</>$abbrev</ ;
	s/>8\.[0-9][0-9abp]*</>"$version"</" > NewInfo.plist
mv NewInfo.plist Info.plist
rm -f development.plist PkgInfo
echo -n "APPL$abbrev" > PkgInfo

cd Resources
rm -f Wish.icns
test -f $origdir/../macosx/data/BK.icns || bk -R get -S macosx/data/BK.icns
cp $origdir/../macosx/data/BK.icns $abbrev.icns

cd ../Frameworks

# remove extra crud
#find . -name "*.a" -exec rm -f {} \;
#find . -name "tclConfig.sh" -exec rm -f {} \;
#find . -name "Headers" -type d -exec rm -rf {} \; -prune
#find . -name "PrivateHeaders" -type d -exec rm -rf {} \; -prune
#find . -name "tcltest*" -type d -exec rm -rf {} \; -prune
#find . -name "*.enc" -size +20 -exec rm -f {} \;
#find . -name "demos" -type d -exec rm -rf {} \; -prune

cd ../MacOS
test -f Wish && mv Wish $appname
exit 0
