From e2872589a4ad42ec06bb881e3ed0e01ff176653d Mon Sep 17 00:00:00 2001
From: partisan <none@noone.no>
Date: Sat, 3 Aug 2024 20:11:24 +0200
Subject: [PATCH] upload script

---
 .gitignore            |   3 +-
 sourceforge_config.sh |   5 ++
 upload.sh             | 142 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 149 insertions(+), 1 deletion(-)
 create mode 100755 sourceforge_config.sh
 create mode 100755 upload.sh

diff --git a/.gitignore b/.gitignore
index c9c6deb..67f1e59 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,3 @@
 /mozilla-central
-/patches
\ No newline at end of file
+/patches
+/packages.json
\ No newline at end of file
diff --git a/sourceforge_config.sh b/sourceforge_config.sh
new file mode 100755
index 0000000..b2e6301
--- /dev/null
+++ b/sourceforge_config.sh
@@ -0,0 +1,5 @@
+# sourceforge_config.sh
+SF_USER="internet-addict"
+SF_PROJECT="spitfire-browser"
+SF_HOST="frs.sourceforge.net"
+SF_KEY_PATH="$HOME/.ssh/id_rsa"  # Path to your SSH private key for SourceForge
diff --git a/upload.sh b/upload.sh
new file mode 100755
index 0000000..8bf95aa
--- /dev/null
+++ b/upload.sh
@@ -0,0 +1,142 @@
+#!/bin/bash
+
+# Load SourceForge configuration
+source sourceforge_config.sh
+
+# Function to print usage instructions
+print_help() {
+    echo "Usage: ./upload.sh -p=<path-to-build> -t=<target> [-c|--compress] [-v|--version=<version>]"
+    echo ""
+    echo "Options:"
+    echo "  -p, --path      : Path to the build directory"
+    echo "  -t, --target    : Target location format: component-arch-release-platform"
+    echo "  -c, --compress  : Compress the build directory into a tar.gz file before uploading"
+    echo "  -v, --version   : Specify version for the package. For nightly, use current date if not specified. For stable, increment version if not specified."
+    echo "  -h, --help      : Display this help message"
+    echo ""
+    echo "Example use:"
+    echo "  # Without compression"
+    echo "  ./upload.sh -p=./mozilla-central/obj-x86_64-pc-linux-gnu/dist/bin -t=browser-x86_64-stable-linux -v=1.0"
+    echo ""
+    echo "  # With compression"
+    echo "  ./upload.sh -p=./mozilla-central/obj-x86_64-pc-linux-gnu/dist/bin -t=browser-x86_64-stable-linux -c -v=1.0"
+    echo ""
+    echo "  # Nightly build without specifying version"
+    echo "  ./upload.sh -p=./mozilla-central/obj-x86_64-pc-linux-gnu/dist/bin -t=browser-x86_64-nightly-linux -c"
+
+    exit 0
+}
+
+COMPRESS=false
+VERSION=""
+
+# Parse command line arguments
+while [[ $# -gt 0 ]]; do
+    case $1 in
+        -p=*|--path=*)
+            BUILD_PATH="${1#*=}"
+            shift
+            ;;
+        -t=*|--target=*)
+            TARGET="${1#*=}"
+            shift
+            ;;
+        -c|--compress)
+            COMPRESS=true
+            shift
+            ;;
+        -v=*|--version=*)
+            VERSION="${1#*=}"
+            shift
+            ;;
+        -h|--help)
+            print_help
+            ;;
+        *)
+            echo "Invalid option: $1"
+            print_help
+            ;;
+    esac
+done
+
+# Check if both required arguments are provided
+if [ -z "$BUILD_PATH" ] || [ -z "$TARGET" ]; then
+    echo "Error: Both path and target must be specified."
+    print_help
+fi
+
+# Split the target into its components
+IFS='-' read -r COMPONENT ARCH RELEASE PLATFORM <<< "$TARGET"
+
+# Determine the upload directory based on the target
+UPLOAD_DIR="$COMPONENT/$ARCH/$RELEASE/$PLATFORM"
+
+# Construct the remote directory path
+REMOTE_DIR="/home/frs/project/$SF_PROJECT/$UPLOAD_DIR"
+
+# Handle versioning
+if [ -z "$VERSION" ]; then
+    if [[ "$RELEASE" == "nightly" ]]; then
+        VERSION=$(date +"%Y%m%d")
+    elif [[ "$RELEASE" == "stable" ]]; then
+        CURRENT_VERSION=$(grep -oP '(?<="'"$COMPONENT"'": ")([^"]*)' packages.json)
+        if [ -n "$CURRENT_VERSION" ]; then
+            MAJOR_VERSION=$(echo $CURRENT_VERSION | cut -d. -f1)
+            MINOR_VERSION=$(echo $CURRENT_VERSION | cut -d. -f2)
+            MINOR_VERSION=$((MINOR_VERSION + 1))
+            VERSION="${MAJOR_VERSION}.${MINOR_VERSION}"
+        else
+            VERSION="1.0"
+        fi
+    fi
+fi
+
+# Update packages.json
+if [ ! -f packages.json ]; then
+    echo "{}" > packages.json
+fi
+
+jq --arg comp "$COMPONENT" --arg ver "$VERSION" '.[$comp] = $ver' packages.json > packages_temp.json && mv packages_temp.json packages.json
+
+# Handle compression if specified
+if [ "$COMPRESS" = true ]; then
+    COMPRESSED_FILE="/tmp/${TARGET}.tar.gz"
+    echo "Compressing $BUILD_PATH into $COMPRESSED_FILE..."
+    tar -czf "$COMPRESSED_FILE" -C "$BUILD_PATH" .
+    BUILD_PATH="$COMPRESSED_FILE"
+fi
+
+# Upload the files to SourceForge
+echo "Uploading files from $BUILD_PATH to $REMOTE_DIR on SourceForge..."
+scp -i "$SF_KEY_PATH" "$BUILD_PATH" "$SF_USER@$SF_HOST:$REMOTE_DIR/" 2>/dev/null
+UPLOAD_STATUS=$?
+
+if [ $UPLOAD_STATUS -ne 0 ]; then
+    echo "Failed to upload files directly. Creating local directory structure and uploading..."
+
+    # Create the local directory structure
+    TEMP_DIR=$(mktemp -d)
+    mkdir -p "$TEMP_DIR/$UPLOAD_DIR"
+
+    # Upload the directory structure
+    rsync -av --omit-dir-times --no-perms -e "ssh -i $SF_KEY_PATH" "$TEMP_DIR/" "$SF_USER@$SF_HOST:/home/frs/project/$SF_PROJECT/" || { echo "Failed to upload directory structure. Exiting."; rm -rf "$TEMP_DIR"; exit 1; }
+    
+    # Clean up the temporary directory
+    rm -rf "$TEMP_DIR"
+
+    # Retry uploading the files
+    scp -i "$SF_KEY_PATH" "$BUILD_PATH" "$SF_USER@$SF_HOST:$REMOTE_DIR/" || { echo "Failed to upload files after creating directory structure. Exiting."; exit 1; }
+fi
+
+# Upload the updated packages.json to the root directory
+echo "Uploading packages.json to the root directory on SourceForge..."
+scp -i "$SF_KEY_PATH" packages.json "$SF_USER@$SF_HOST:/home/frs/project/$SF_PROJECT/" || { echo "Failed to upload packages.json. Exiting."; exit 1; }
+
+echo "Upload completed successfully."
+
+# Clean up compressed file if it was created
+if [ "$COMPRESS" = true ]; then
+    rm "$COMPRESSED_FILE"
+fi
+
+exit 0