# Determine Mac system architecture
ARCH=$(uname -m)
echo "Detected $ARCH architecture (raw)"

IS_TRANSLATED=0
TRANSLATED=$(sysctl -n sysctl.proc_translated)
if [ -z "$TRANSLATED" ]; then
    echo "Not running with Rosetta translation - translation not found"
elif [ "$TRANSLATED" = 0 ]; then
    echo "Not running with Rosetta translation"
elif [ "$TRANSLATED" = 1 ]; then
    IS_TRANSLATED=1
    echo "Running with Rosetta translation"
else
    echo "Unknown Rosetta translation value: $TRANSLATED"
fi

echo "Detected architecture: ${ARCH}, Rosetta translation status: ${IS_TRANSLATED}"

# If the detected architecture is x86_64, but the script is running under Rosetta translation, then the calculated
# architecture will be arm64. The assumption is one cannot run arm64 under Rosetta translation.
if [ "$ARCH" == "x86_64" ] && [ $IS_TRANSLATED == 1 ]; then
    ARCH="arm64"
fi

echo "Calculated architecture: ${ARCH}"

if [ "$ARCH" == "x86_64" ]; then
    exit 1
elif [ "$ARCH" == "arm64" ]; then
    exit 2
else
    exit 0
fi