#!/usr/bin/env bash

#set -3
clear
#osascript -e 'display notification "Sending request to EuphoriaTools..." with title "A12Liberator"'
osascript -e 'display notification "Checking registration..." with title "A12Liberator"'
#change script activated working directory to current directory
cd "$(dirname "$0")"

#delete existing activation files
#rm ./gold/activation_record.plist
rm -rf ./GENERATED_ACTIVATION/

sleep 1

# Find ideviceinfo in system PATH
IDEVICEINFO_PATH=$(which ideviceinfo 2>/dev/null)
if [ -z "$IDEVICEINFO_PATH" ]; then
    # If not found with 'which', try common paths
    for path in /opt/homebrew/bin/ideviceinfo /usr/local/bin/ideviceinfo /opt/local/bin/ideviceinfo /usr/bin/ideviceinfo; do
        if [ -x "$path" ]; then
            IDEVICEINFO_PATH="$path"
            break
        fi
    done
fi

# Find irecovery in system PATH
IRECOVERY_PATH=$(which irecovery 2>/dev/null)
if [ -z "$IRECOVERY_PATH" ]; then
    # If not found with 'which', try common paths
    for path in /opt/homebrew/bin/irecovery /usr/local/bin/irecovery /opt/local/bin/irecovery /usr/bin/irecovery; do
        if [ -x "$path" ]; then
            IRECOVERY_PATH="$path"
            break
        fi
    done
fi

# Check if ideviceinfo was found
if [ -z "$IDEVICEINFO_PATH" ]; then
    echo "ERROR: ideviceinfo not found in system PATH"
    echo "Please install libimobiledevice:"
    echo "  brew install libimobiledevice"
    osascript -e 'display notification "ideviceinfo not found! Please install libimobiledevice" with title "A12Liberator"'
    exit 1
fi

echo "✓ Found ideviceinfo at: $IDEVICEINFO_PATH"
if [ -n "$IRECOVERY_PATH" ]; then
    echo "✓ Found irecovery at: $IRECOVERY_PATH"
fi

#new for app! 1.7+
dir="$(dirname "$0")"

DeviceInfo(){
     "$IDEVICEINFO_PATH" | grep -w $1 | awk '{printf $NF}';
}

_info() {
    if [ "$1" = 'recovery' ]; then
        if [ -n "$IRECOVERY_PATH" ]; then
            echo $("$IRECOVERY_PATH" -q | grep "$2" | sed "s/$2: //")
        else
            echo "irecovery not found"
        fi
    elif [ "$1" = 'normal' ]; then
        echo $("$IDEVICEINFO_PATH" | grep "$2: " | sed "s/$2: //")
    fi
}

echo '-----'
echo 'Found!'
echo ''
echo 'Model:' $(DeviceInfo ModelNumber)
echo 'iOS:' $(DeviceInfo ProductVersion)
echo 'Build:' $(DeviceInfo BuildVersion)
echo 'Serial:' $(DeviceInfo SerialNumber)
echo 'IMEI:' $(DeviceInfo InternationalMobileEquipmentIdentity)
echo 'UDID:' $(DeviceInfo UniqueDeviceID)
echo 'DeviceClass:' $(DeviceInfo DeviceClass)
echo 'DeviceUCID:' $(DeviceInfo UniqueChipID)

sleep 1

rm -rf GENERATED_ACTIVATION

sleep 1

mkdir -p ./GENERATED_ACTIVATION/

echo "https://euphoriatools.com/a12liberator/server/registration_check.php?sn=$(DeviceInfo SerialNumber)&ucid=$(DeviceInfo UniqueChipID)&udid=$(DeviceInfo UniqueDeviceID)" --output ./GENERATED_ACTIVATION/actstatus.txt

curl -k "https://euphoriatools.com/a12liberator/server/registration_check.php?sn=$(DeviceInfo SerialNumber)&ucid=$(DeviceInfo UniqueChipID)&udid=$(DeviceInfo UniqueDeviceID)" --output ./GENERATED_ACTIVATION/actstatus.txt

sleep 3

#osascript -e 'display notification "Recieved response!" with title "FrostByte"'
#
#sleep 1

filename="GENERATED_ACTIVATION/actstatus.txt"
search_text="This device is NOT registered with A12Liberator!"

if grep -q "$search_text" "$filename"; then
    osascript -e 'display notification "'$(DeviceInfo SerialNumber)' is NOT registered!" with title "A12Liberator"'
    sleep 2
    echo ""
    echo "This device is NOT registered!"
    echo ""
    osascript -e 'display notification "Please visit EuphoriaTools.com to Register!" with title "A12Liberator"'
else
    echo ""
    echo "This device IS registered!"
    echo ""
    osascript -e 'display notification "'$(DeviceInfo SerialNumber)' is registered!" with title "A12Liberator"'
fi

echo 'Racoon Done.'
