#!/bin/bash

# Define the repository path
REPO_PATH="/opt/apps/elab-manage"

# Function to update the git repository
update_repo() {
    echo "Updating the git repository..."
    cd "$REPO_PATH" || {
        echo "Error: Unable to find the repository at $REPO_PATH";
        exit 1;
         }
    git pull -r || {
        echo "Error: Unable to update the repository";
        exit 1;
        }
}



# Function to update symbolic links
update_symlinks() {
    echo "Updating symbolic links for cron.daily..."
    # Update or add new symbolic links for cron.daily and cron.hourly
    for dir in "cron."{d,daily,hourly,monthly,weekly}; do
        find -L /etc/$dir -maxdepth 1 -type l -ilname $REPO_PATH/etc/$dir\* -delete
        ## test if the directory is empty or exist to avoid creating empty links
        if compgen -G "$REPO_PATH/etc/$dir/*" > /dev/null; then
            ln -sf "$REPO_PATH/etc/$dir/"* "/etc/$dir"
        fi
    done

    echo "Symbolic links have been successfully updated."
}

# Update the git repository and symbolic links
update_repo
update_symlinks

echo "Elabore Update complete."
