remote_url='https://git.chiasson.cloud/api/packages/Olivier/debian/pool/bookworm/main/upload'
script_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
cd "$script_dir"

# Build list of package directories (those containing DEBIAN/control)
dirs=()
for d in "$script_dir"/*/; do
    [[ -f "$d/DEBIAN/control" ]] && dirs+=("$(basename "$d")")
done

if ((${#dirs[@]} == 0)); then
    echo "No package directories with DEBIAN/control found in $script_dir" >&2
    exit 1
fi

PS3="Choose a package directory: "
select pkg in "${dirs[@]}"; do
    if [[ -n "$pkg" ]]; then
        pkg_path="$pkg"
        break
    fi
    echo "Invalid selection"
done
version=$(awk -F': ' '/^Version:/{print $2; exit}' "$pkg_path/DEBIAN/control")
output_file="${pkg_path}_${version}_all.deb"
output_path="builds/${output_file}"
read -p "Enter your username: " user
read -s -p "Enter your password: " pass
echo

mkdir -p builds
dpkg-deb --build "$pkg_path" "$output_path"
response=$(curl --silent --show-error --user "$user:$pass" --upload-file "$output_path" "$remote_url" 2>&1)
if echo "$response" | grep -q "package file already exists"; then
    echo "Upload skipped: package file already exists on the server. You might have forgot to update the version number in the control file"
elif echo "$response" | grep -q "authGroup.Verify"; then
    echo "Upload failed: authentication failed"
else
    echo "$response"
    echo "Package $output_path uploaded successfully"
fi








