summarylogtreecommitdiffstats
path: root/url-script
blob: 7a9626fd9cafcb7aea6919810261c934b5e01ae2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/bin/bash

REPO_OWNER="expo"
REPO_NAME="orbit"
PKGBUILD_FILE="PKGBUILD"

# Function to fetch the latest release information from the GitHub API and extract the .rpm URL
get_latest_rpm_info() {
  local API_URL="https://api.github.com/repos/$REPO_OWNER/$REPO_NAME/releases/latest"

  # Use curl to fetch the API data
  API_RESPONSE=$(curl -s "$API_URL")

  # Check if the curl command was successful
  if [ $? -ne 0 ]; then
    echo "Error: Failed to fetch release information from the GitHub API."
    return 1
  fi

  # Extract the tag name and assets array from the JSON response using jq
  LATEST_TAG=$(echo "$API_RESPONSE" | jq -r '.tag_name')
  ASSETS=$(echo "$API_RESPONSE" | jq -r '.assets')

  # Check if assets were found
  if [ -z "$ASSETS" ]; then
    echo "Error: No assets found in the latest release."
    return 1
  fi

  # Iterate through the assets to find the .rpm file
  for ASSET in $(echo "$ASSETS" | jq -r '.[] | @base64'); do
    ASSET_DECODED=$(echo "$ASSET" | base64 --decode)
    ASSET_NAME=$(echo "$ASSET_DECODED" | jq -r '.name')
    ASSET_DOWNLOAD_URL=$(echo "$ASSET_DECODED" | jq -r '.browser_download_url')

    # Check if the asset is an .rpm file
    if [[ "$ASSET_NAME" == *.rpm ]]; then
      echo "RPM_URL=$ASSET_DOWNLOAD_URL"

      # Extract version number from the tag, removing 'expo-orbit-v' prefix
      VERSION=$(echo "$LATEST_TAG" | sed 's/^expo-orbit-v//' | sed 's/^v//')
      echo "VERSION=$VERSION"
      echo "ASSET_NAME=$ASSET_NAME" # Added debug output
      return 0                      # Exit function after finding the first .rpm
    fi
  done

  echo "Error: No .rpm asset found in the latest release."
  return 1
}

# Get the latest .rpm URL and version
get_latest_rpm_info
if [ $? -ne 0 ]; then
  echo "Error: Failed to get RPM information."
  exit 1
fi

# Extract the variables from the output of get_latest_rpm_info
eval $(get_latest_rpm_info)

# Update pkgver in PKGBUILD
sed -i "s/^pkgver=.*/pkgver=${VERSION}/" "$PKGBUILD_FILE"

# Increment pkgrel (TODO Update to only increment on same version and revert to 1 on new)
# This simply increments the number.  If pkgrel isn't a number, this will break.
OLD_PKGREL=$(grep '^pkgrel=' "$PKGBUILD_FILE" | awk -F= '{print $2}')
NEW_PKGREL=$((OLD_PKGREL + 1))
sed -i "s/^pkgrel=.*/pkgrel=${NEW_PKGREL}/" "$PKGBUILD_FILE"

# Update source URL in PKGBUILD
sed -i "s#^source=(.*)#source=(${RPM_URL})#" "$PKGBUILD_FILE"

# Calculate SHA256 checksum (download to a temperary file)
TEMP_FILE=$(mktemp)
curl -L -H "Content-Type: application/octet-stream" -A "User-Agent: MyArchLinuxPackageBuilder" "$RPM_URL" -o "$TEMP_FILE"
CURL_EXIT_CODE=$?
if [ $CURL_EXIT_CODE -ne 0 ]; then
  echo "Error: curl failed to download the RPM file. Exit code: $CURL_EXIT_CODE"
  rm -f "$TEMP_FILE"
  exit 1
fi
SHA256SUM=$(sha256sum "$TEMP_FILE" | awk '{print $1}')
rm -f "$TEMP_FILE"

# Update sha256sums in PKGBUILD
sed -i "s/^sha256sums=.*/sha256sums=('${SHA256SUM}')/" "$PKGBUILD_FILE"

# Print a message indicating the PKGBUILD has been updated
echo "Updated $PKGBUILD_FILE with:"
echo "  pkgver = ${VERSION}"
echo "  pkgrel = ${NEW_PKGREL}"
echo "  source = ${RPM_URL}"
echo "  sha256sums = ('${SHA256SUM}')"