#!/bin/sh

# This file is part of BOINC.
# http://boinc.berkeley.edu
# Copyright (C) 2025 University of California
#
# BOINC is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License
# as published by the Free Software Foundation,
# either version 3 of the License, or (at your option) any later version.
#
# BOINC is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with BOINC.  If not, see <http://www.gnu.org/licenses/>.
#
##
# post-Install Script for Macintosh BOINC Manager for OS X revised 3/8/25
##

echo "Starting BOINC postinstall script"
echo "initial directory = $PWD"
echo "argument 1 = $1"

# Copy acct_mgr_url.xml into BOINC Data directory here so it won't be
# deleted if user later runs standard BOINC installer
if [ -f "Contents/Resources/acct_mgr_url.xml" ]; then
    cp -fp Contents/Resources/acct_mgr_url.xml "/Library/Application Support/BOINC Data/acct_mgr_url.xml"
fi

rm -f /tmp/BOINCInstallLog.txt

# PostInstall.app is in the same directory as the package (BOINC.pkg or GridRepublic.pkg, etc.)
pipath=`dirname "$1"`/PostInstall.app/Contents/MacOS/PostInstall
echo "PostInstall.app path = $pipath"

# Run the Postinstall Application
if [ "${COMMAND_LINE_INSTALL}" = "1" ]; then
    "$pipath" -part1 > /tmp/BOINCInstallLog.txt 2>&1
    retval=$?
    if [ $retval -eq 0 ]; then
        sleep 2
        "$pipath" -part2 >> /tmp/BOINCInstallLog.txt 2>&1 &
    fi
else
    "$pipath" -part1 2>&1 | tee /tmp/BOINCInstallLog.txt
    retval=$?
    if [ $retval -eq 0 ]; then
        sleep 2
        # part2 continues to run after Installer finishes,
        # and fails unless we redirect stdout to a file
        "$pipath" -part2 >> /tmp/BOINCInstallLog.txt 2>&1 &
    fi
fi

exit $retval
