#!/var/jb/usr/bin/bash

# Define the path to the status file
status_file="/var/jb/Library/dpkg/status"

# Function to update file_contents variable
update_file_contents() {
    file_contents=$(<"$status_file")
}

# Store the contents of the status file in a variable
file_contents=$(<"$status_file")

# Check if the status file exists
if [ -f "$status_file" ]; then
    echo
    echo "[•] Scanning packages..."
    echo

    while IFS= read -r line; do
        if [[ $line == "Package: "* ]]; then
            package=$(echo "$line" | cut -d' ' -f2)
        elif [[ $line == "Maintainer: "* ]]; then
            maintainer=$(echo "$line" | cut -d' ' -f2-)
        elif [[ $line == "Description: "* ]]; then
            description=$(echo "$line" | cut -d' ' -f2-)
        elif [[ $line == "" ]]; then
            if [ -z "$maintainer" ]; then
                random_maintainer="Your mom"
                sed -i "/Package: $package/a Maintainer: $random_maintainer" "$status_file"
                echo "[*] $package: Added Maintainer"
                update_file_contents
                echo
            fi
            if [ -z "$description" ]; then
                random_description="Your mom forgot a description so I added one for her"
                sed -i "/Package: $package/a Description: $random_description" "$status_file"
                echo "[*] $package: Added Description"
                update_file_contents
                echo
            fi
            package=""
            maintainer=""
            description=""

            pcount=$(grep -c "^Package:" <<< "$file_contents")
            mcount=$(grep -c "^Maintainer:" <<< "$file_contents")
            dcount=$(grep -c "^Description:" <<< "$file_contents")

            if [[ $pcount == $mcount ]] && [[ $mcount == $dcount ]]; then
                echo "[✓] Completed!"
                echo
                exit 0
            fi
        fi
    done < "$status_file"

    echo "[✓] Completed!"
    echo
else
    echo "[x] Error: Status file not found."
fi
