#!/bin/sh
# Set the following before running to force a specific signing identity and profile:
# CFA_PROVISIONING_PROFILE=
# CFA_SIGNING_IDENTITY=
#
# To get a list of available signing identities, you can run:
# security find-identity -v -p codesigning

CURDIR=`dirname "$(readlink -f "$0")"`

#!/bin/bash

# Default directories for newer and older XCode version
PROVISIONING_PROFILE_DIRS=(
    "$HOME/Library/Developer/Xcode/UserData/Provisioning Profiles"
    "$HOME/Library/MobileDevice/Provisioning Profiles"
)

HAS_MULTIPLE_PROFILES=0
SKIP=

IDENTITIES=$(security find-identity -v -p codesigning | grep -oE '^[ ]*[0-9]+\) [A-F0-9]+ ".*"' | sed -E 's/^[ ]*[0-9]+) +//')
# Count the number of identities
IDENTITY_COUNT=$(echo "$IDENTITIES" | wc -l | tr -d ' ')

if [ -z "$CFA_SIGNING_IDENTITY" ]; then
    if [ "$IDENTITY_COUNT" -eq 1 ]; then
        CFA_SIGNING_IDENTITY=$(echo "$IDENTITIES" | awk '{print $1}')
        IDENTITY_NAME=$(echo "$IDENTITIES" | sed -E 's/^[A-F0-9]+ "//' | sed 's/"$//')
        echo "Signing as: $IDENTITY_NAME"
    else
        echo "Unique signing identity not found. Set CFA_SIGNING_IDENTITY to the signing identity you wish to use:"
        SKIP=1
        security find-identity -v -p codesigning
        echo ""
    fi
fi

if [ -z "$CFA_PROVISIONING_PROFILE" ]; then
    for dir in "${PROVISIONING_PROFILE_DIRS[@]}"; do
        # Ensure directory exists
        if [ -d "$dir" ]; then
            # Find .mobileprovision files
            profiles=("$dir"/*.mobileprovision)

            # Check if any files were found
            if [ -e "${profiles[0]}" ]; then
                if [ ${#profiles[@]} -eq 1 ] && [ "$HAS_MULTIPLE_PROFILES" -eq 0 ]; then
                    # Exactly one profile found, use it
                    export CFA_PROVISIONING_PROFILE="${profiles[0]}"
                    echo "Using mobile provisioning profile:"
                    echo "    $CFA_PROVISIONING_PROFILE"
                    break
                else
                    if [ "$HAS_MULTIPLE_PROFILES" -eq 0 ]; then
                        echo "Multiple mobile provisioning profiles installed. Please set CFA_PROVISIONING_PROFILE to the profile you wish to use:"
                    fi
                    SKIP=1
                    HAS_MULTIPLE_PROFILES=1
                    find "$dir" -type f -name "*.mobileprovision" -exec sh -c 'echo "    \"{}\"" ' \;
                fi
            fi
        fi
    done
fi

if [ -z "$CFA_PROVISIONING_PROFILE" ] && [ "$HAS_MULTIPLE_PROFILES" -ne 1 ]; then
    echo "No mobile provisioning files found in standard XCode paths. Please ensure XCode"
    echo "is installed, connected to your Apple Developer account, and that you have successfully"
    echo "deployed an app to your device."
    echo ""
    echo "Or, set 'CFA_PROVISIONING_PROFILE' to a valid .mobileprovision file and run again."
    SKIP=1
fi

if [ -n "$SKIP" ]; then
    exit 1
fi

# Run "security unlock" first if necessary (for example, running via ssh)
if ! security show-keychain-info ~/Library/Keychains/login.keychain-db >/dev/null 2>&1; then
    echo "Enter your password to unlock keychain before signing..."
    security unlock-keychain  ~/Library/Keychains/login.keychain-db
fi

for app in "$@"
do
    $CURDIR/sign.macos.pl -m "$CFA_PROVISIONING_PROFILE" -s "$CFA_SIGNING_IDENTITY" -a $app
done
