#!/bin/csh -f
set pax = /bin/pax
set mkbom = /usr/bin/mkbom
set lsbom = /usr/bin/lsbom

if ( $#argv != 2 && $#argv != 3 ) then
    echo "Usage: makepkg <version> <machine-type> [ <release> ]"
    echo "  e.g.: makepkg 1.0a ppcosx 2"
    echo "  <release> defaults to 1"
    exit 1
endif

if ( `id -u` != 0 ) then
    echo "makepkg must be run as root (via su)"
    exit 1
endif

set version = $1
set machine = $2

if ( "$3" == "" ) then
   set release = 1
else
   set release = $3
endif

set pkgname = swl${version}-${machine}-${release}

set ROOT = `pwd`/ROOT
if ( -e ${ROOT} ) then
   echo -n "ROOT exists, remove? (y/n): "
   if ( "$<" == "y" ) then
      rm -rf ${ROOT}
   else
      exit 1
   endif
endif
/bin/mkdir -m 755 ${ROOT}
( cd .. ; make install InstallGroup=admin TempRoot=${ROOT} )

# Replace swl shell script with one that opens swl.app so that we avoid
# the "SetFrontProcess failed,-606" bug.
cat << EOF > ${ROOT}/usr/bin/swl
#!/bin/sh
echo cd '"'\`pwd\`'"' > \${HOME}/.swlsourceme
echo ARGS='"'\$*'"' >> \${HOME}/.swlsourceme
open -a /Applications/swl.app
EOF
chmod 555 ${ROOT}/usr/bin/swl

/bin/mkdir -p -m 755 ${ROOT}/Applications/swl.app/Contents/{MacOS,Resources}

cat << EOF > ${ROOT}/Applications/swl.app/Contents/Info.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//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>CFBundleDocumentTypes</key>
        <array>
            <dict>
                <key>CFBundleTypeExtensions</key>
                <array>
                    <string>ss</string>
                    <string>scm</string>
                </array>
                <key>CFBundleTypeIconFile</key>
                <string>cs.icns</string>
                <key>CFBundleTypeName</key>
                <string>SCHEME Document</string>
                <key>CFBundleTypeOSTypes</key>
                <array>
                    <string>SCHEME</string>
                </array>
                <key>CFBundleTypeRole</key>
                <string>Viewer</string>
            </dict>
        </array>
        <key>CFBundleExecutable</key>
        <string>swl</string>
        <key>CFBundleGetInfoString</key>
        <string>SWL Version ${version}</string>
        <key>CFBundleIconFile</key>
            <string>cs.icns</string>
        <key>CFBundleIdentifier</key>
        <string>com.scheme.swl</string>
        <key>CFBundleInfoDictionaryVersion</key>
        <string>6.0</string>
        <key>CFBundleName</key>
        <string>SWiL</string>
        <key>CFBundlePackageType</key>
        <string>APPL</string>
        <key>CFBundleShortVersionString</key>
        <string>${version}</string>
        <key>CFBundleSignature</key>
        <string>SWiL</string>
        <key>CFBundleVersion</key>
        <string>${version}</string>
    </dict>
</plist>
EOF
chmod 444 ${ROOT}/Applications/swl.app/Contents/Info.plist

cat << EOF > ${ROOT}/Applications/swl.app/Contents/MacOS/swl
#!/bin/sh
TCL_LIBRARY=/Library/Frameworks/Tcl.framework/Resources/Scripts/
TK_LIBRARY=/Library/Frameworks/Tk.framework/Resources/Scripts/
SWL_ROOT=/usr/lib/swl${version}/lib
SWL_LIBRARY=/usr/lib/swl${version}/${machine}
export TCL_LIBRARY; export TK_LIBRARY; export SWL_LIBRARY; export SWL_ROOT
ARGS=""
if [ -f \${HOME}/.swlsourceme ]; then
   . \${HOME}/.swlsourceme
   rm \${HOME}/.swlsourceme
fi
# echo "" provides a nonempty file so that tk won't provide unwanted console
cd \${HOME}/Desktop
if [ -f /usr/bin/scheme ]; then
  echo "" | exec /usr/bin/scheme -b /usr/lib/swl${version}/${machine}/swl.boot \$ARGS
else
  exec "" | /usr/bin/petite -b /usr/lib/swl${version}/${machine}/swl.boot \$ARGS
fi
EOF
chmod 555 ${ROOT}/Applications/swl.app/Contents/MacOS/swl

echo -n "APPLSWiL" > ${ROOT}/Applications/swl.app/Contents/PkgInfo
chmod 444 ${ROOT}/Applications/swl.app/Contents/PkgInfo
cp cs.icns ${ROOT}/Applications/swl.app/Contents/Resources/cs.icns
chmod 444 ${ROOT}/Applications/swl.app/Contents/Resources/cs.icns

# Build the package
rm -rf ${pkgname}.pkg
/bin/mkdir -p ${pkgname}.pkg/Contents/Resources/English.lproj
echo pmkrpkg1 > ${pkgname}.pkg/Contents/PkgInfo
(cd ${ROOT}; ${pax} -w -z -x cpio .) > ${pkgname}.pkg/Contents/Resources/${pkgname}.pax.gz

cp -p Resources/* ${pkgname}.pkg/Contents/Resources/

cat << EOF > ${pkgname}.pkg/Contents/Resources/English.lproj/${pkgname}.info
Title Scheme Widget Library
Version ${version}
Description This package installs SWL version ${version} for Mac OS X.
DefaultLocation /
DeleteWarning
NeedsAuthorization YES
Relocatable NO
RequiresReboot NO
UseUserMask NO
OverwritePermissions NO
InstallFat NO
rootVolumeOnly YES
DisableStop NO
LongFilenames YES
LibrarySubdirectory Standard
InstallOnly NO
AllowBackRev YES
Required NO
Application NO
EOF

cat << EOF > ${pkgname}.pkg/Contents/Resources/Welcome.html
<html>
<h3>SWL version ${version}</h3>
<p>Copyright (c) 1993-`date +%Y` Oscar Waddell</p>
<p>SWL provides a graphical interface for Chez Scheme and Petite Chez Scheme.</p>
</html>
EOF

${mkbom} ${ROOT} ${pkgname}.pkg/Contents/Resources/${pkgname}.bom
set numFiles = `${lsbom} -s ${pkgname}.pkg/Contents/Resources/${pkgname}.bom | /usr/bin/wc -l`

# compute package size when compressed
set compressedSize = `/usr/bin/du -k -s ${pkgname}.pkg | awk '{print $1}'`
@ compressedSize += 3		# add 1KB each for sizes, location, status files

set infoSize = `/bin/ls -s ${pkgname}.pkg/Contents/Resources/English.lproj/${pkgname}.info | awk '{print $1}'`
set bomSize = `/bin/ls -s ${pkgname}.pkg/Contents/Resources/${pkgname}.bom | awk '{print $1}'`
if (-e ${pkgname}.pkg/Contents/Resources/background.jpg) then
   set jpgSize = `/bin/ls -s ${pkgname}.pkg/Contents/Resources/background.jpg | awk '{print $1}'`
else
   set jpgSize = 0
endif 

set installedSize = `/usr/bin/du -k -s ${ROOT} | awk '{print $1}'`
@ installedSize += ${infoSize} + ${bomSize} + ${jpgSize} + 3

# echo size parameters to sizes file
cat << EOF >  ${pkgname}.pkg/Contents/Resources/${pkgname}.sizes
NumFiles ${numFiles}
InstalledSize ${installedSize}
CompressedSize ${compressedSize}
EOF

chown -R root:admin ${pkgname}.pkg
find ${pkgname}.pkg -type d | xargs chmod 755
find ${pkgname}.pkg -type f | xargs chmod 444

tar czf ${pkgname}.pkg.tar.gz ${pkgname}.pkg
chown ${SUDO_USER} ${pkgname}.pkg.tar.gz

/bin/rm -rf ${ROOT} ${pkgname}.pkg
