#!/bin/bash

TMPDIR=/tmp/pkg.$$

mkdir $TMPDIR
cp -R pkg/Templates $TMPDIR
cp -R pkg/Specifications $TMPDIR
cp -R pkg/Plug-ins $TMPDIR

PROJECTS=/tmp/projects.$$
cat > $PROJECTS << EOF
cda:orca:ORCAC Classic Desk Accessory:0:0:0
cda:orca:ORCAM Classic Desk Accessory:0:0:0
cda:merlin:Merlin Classic Desk Accessory:0:0:0
cdev:orca:ORCAC Control Panel:0:0:0
cdev:orca:ORCAM Control Panel:0:0:0
cdev:merlin:Merlin Control Panel:0:0:0
desktop:orca:ORCAC Desktop Application:1:1:0
desktop:orca:ORCAM Desktop Application:1:0:0
desktop:merlin:Merlin Desktop Application:1:0:0
shell:gno:ORCAC GNO Shell Command:0:0:0
nda:orca:ORCAC New Desk Accessory:0:0:0
nda:orca:ORCAM New Desk Accessory:0:0:0
nda:merlin:Merlin New Desk Accessory:0:0:0
shell:orca:ORCAC Shell Command:0:0:0
shell:orca:ORCAM Shell Command:0:0:0
init:orca:ORCAC Init:0:0:1
init:orca:ORCAM Init:0:0:1
init:merlin:Merlin Init:0:0:1
EOF

while read PROJECT
do
    PROJECT_TYPE=`echo $PROJECT | awk -F: '{print $1}'`
    SHELL_TYPE=`echo $PROJECT | awk -F: '{print $2}'`
    PROJECT_NAME=`echo $PROJECT | awk -F: '{print $3}'`
    SUPPORTS_RES_MODE=`echo $PROJECT | awk -F: '{print $4}'`
    SUPPORTS_MESSAGE_CENTER=`echo $PROJECT | awk -F: '{print $5}'`
    SUPPORTS_INIT_TYPE=`echo $PROJECT | awk -F: '{print $6}'`

    cp -R make "${TMPDIR}/Templates/Apple IIgs/${PROJECT_NAME}.xctemplate/"

    if [ $SHELL_TYPE = merlin ]
    then
        sed "
            /^# TARGETTYPE=${PROJECT_TYPE}/s/^# //" Makefile.merlin > "${TMPDIR}/Templates/Apple IIgs/${PROJECT_NAME}.xctemplate/Makefile"
        rm -f "${TMPDIR}/Templates/Apple IIgs/${PROJECT_NAME}.xctemplate/make/orca-asm"
        rm -f "${TMPDIR}/Templates/Apple IIgs/${PROJECT_NAME}.xctemplate/make/orca-cc"
    else
        sed "
            /^# TARGETTYPE=${PROJECT_TYPE}/s/^# //
            /^# export ORCA=.*\/${SHELL_TYPE}$/s/^# //" Makefile > "${TMPDIR}/Templates/Apple IIgs/${PROJECT_NAME}.xctemplate/Makefile"
        rm -f "${TMPDIR}/Templates/Apple IIgs/${PROJECT_NAME}.xctemplate/make/merlin-asm"
    fi

    if [ "$SUPPORTS_RES_MODE" -eq 1 ]
    then
	sed -i "" 's/^# DESKTOP_RES_MODE=.*$/DESKTOP_RES_MODE=___VARIABLE_RESOLUTIONMODE___/' "${TMPDIR}/Templates/Apple IIgs/${PROJECT_NAME}.xctemplate/Makefile"
    fi

    if [ "$SUPPORTS_MESSAGE_CENTER" -eq 1 ]
    then
	sed -i "" 's/^# MESSAGE_CENTER=.*$/MESSAGE_CENTER=___VARIABLE_MESSAGECENTER___/' "${TMPDIR}/Templates/Apple IIgs/${PROJECT_NAME}.xctemplate/Makefile"
    fi

    if [ "$SUPPORTS_INIT_TYPE" -eq 1 ]
    then
	sed -i "" 's/^# INIT_TYPE=.*$/INIT_TYPE=___VARIABLE_INITTYPE___/' "${TMPDIR}/Templates/Apple IIgs/${PROJECT_NAME}.xctemplate/Makefile"
    fi

done < $PROJECTS

rm -f $PROJECTS

pkgbuild --root $TMPDIR --version 3.2 --identifier com.halcyontouch.Apple2gsTemplate.pkg --install-location /tmp/com.halcyontouch.Apple2gsTemplate.pkg/ --scripts pkg/scripts/ Apple2GSXcodeTemplate.pkg
productbuild --distribution pkg/Distribution.xml --resource ./pkg temp.pkg
rm Apple2GSXcodeTemplate.pkg
productsign --sign "Developer ID Installer: Halcyon Touch Software" temp.pkg Apple2GSXcodeTemplate.pkg
rm temp.pkg

rm -rf $TMPDIR
