#!/bin/bash

# Check if root
if [ "$EUID" -ne 0 ]; then 
    echo "  Please run as root"
    exit 1
fi

date=$(date '+%Y-%m-%d_%H:%M:%S')
tmpdir=$(mktemp -d)


filename=r${1}_logs_${date}.zip

cd "$tmpdir" || exit 1

# Copy matching .zip logs to temp dir
cp /etc/salicru/storage/logs/r${1}*.zip . 2>/dev/null

# Only create the archive if files were found
if compgen -G "r${1}*.zip" > /dev/null; then
    zip -rq "$filename" r${1}*.zip
    if [ $? -eq 0 ]; then
        echo "$tmpdir/$filename"
    else
        echo "ZIP creation failed."
        exit 1
    fi
else
    echo "No log files found matching pattern: r${1}*.zip"
    exit 1
fi
