diff --git a/gitea-blurple.sh b/gitea-blurple.sh index 60967b9..a690c84 100644 --- a/gitea-blurple.sh +++ b/gitea-blurple.sh @@ -1,22 +1,83 @@ #!/bin/bash -git clone https://git.oldgate.org/OGS/Gitea-Blurple.git -sudo systemctl stop gitea -rm -rf /var/lib/gitea/custom/public -cp -r Gitea-Blurple/public/ /var/lib/gitea/custom/ -sudo chown -R git:git /var/lib/gitea/custom -sudo chmod -R 750 /var/lib/gitea/custom -if grep -Fxq "[ui]" /etc/gitea/app.ini -then -rm -rf Gitea-Blurple -sudo systemctl restart gitea -else -cp /etc/gitea/app.ini /etc/gitea/app.ini.bak -cat Gitea-Blurple/templates/app.ini >> /etc/gitea/app.ini -sudo chown root:git /etc/gitea -sudo chmod 770 /etc/gitea -rm -rf Gitea-Blurple -sudo systemctl restart gitea -fi -echo -e "==================================================================" -echo "Clear your cache for the changes to appear" -echo -e "==================================================================" + +log_message() { + local level=$1 + local message=$2 + local timestamp=$(date +"%Y-%m-%d %H:%M:%S") + echo "[$timestamp] [$level] $message" >> /var/log/gitea_blurple.log +} + +handle_error() { + local message=$1 + local level=${2:-"ERROR"} + log_message "$level" "$message" + echo "Error: $message" >&2 + exit 1 +} + +clone_repository() { + local retries=3 + local attempt=1 + until git clone https://git.oldgate.org/OGS/Gitea-Blurple.git; do + ((attempt++)) + if [ $attempt -gt $retries ]; then + handle_error "Failed to clone repository after $retries attempts" + fi + log_message "WARNING" "Failed to clone repository, retrying... (Attempt $attempt)" + sleep 5 + done +} + +backup_file() { + local file=$1 + if [ -e "$file" ]; then + cp "$file" "$file.bak" || handle_error "Failed to backup $file" + log_message "INFO" "Backup created for $file" + else + log_message "WARNING" "File $file does not exist, skipping backup" + fi +} + +append_to_file() { + local content=$1 + local file=$2 + echo "$content" >> "$file" || handle_error "Failed to append content to $file" +} + +main() { + local repository_dir="Gitea-Blurple" + local gitea_service="gitea" + local gitea_config="/etc/gitea/app.ini" + local custom_dir="/var/lib/gitea/custom" + local custom_public_dir="$custom_dir/public" + + cd /tmp || handle_error "Failed to change directory to /tmp" + clone_repository || handle_error "Failed to clone repository" + + sudo systemctl stop "$gitea_service" || handle_error "Failed to stop Gitea service" + + backup_file "$custom_public_dir" + + sudo cp -r "$repository_dir/public/" "$custom_public_dir" || handle_error "Failed to copy new custom/public directory" + + sudo chown -R git:git "$custom_dir" || handle_error "Failed to set ownership" + sudo chmod -R 750 "$custom_dir" || handle_error "Failed to set permissions" + + if grep -q "\[ui\]" "$gitea_config"; then + sudo systemctl restart "$gitea_service" || handle_error "Failed to restart Gitea service" + else + backup_file "$gitea_config" + append_to_file "$(cat "$repository_dir/templates/app.ini")" "$gitea_config" + sudo chown root:git "$gitea_config" || handle_error "Failed to set ownership for app.ini" + sudo chmod 770 "$gitea_config" || handle_error "Failed to set permissions for app.ini" + sudo systemctl restart "$gitea_service" || handle_error "Failed to restart Gitea service" + fi + + echo -e "==================================================================" + echo "Clear your cache for the changes to appear" + echo -e "==================================================================" + + log_message "INFO" "Script execution completed successfully" +} + +main