#!/bin/bash
#
# rsync-backup-rotator
# Version: 1.0.1
#
# Copyright (C) 2023 - 2024 Willem L. Middelkoop (mail@willem.com)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#
# For more information, visit: https://source.willem.com/rsync-backup-rotator/

# Function to show help/usage
show_help() {
    echo "Usage: $0 parent_directory [-w] [-m] [--delete] [--help]"
    echo
    echo "Options:"
    echo "  parent_directory   Mandatory. The parent directory for 'current' backups."
    echo "  -w                 Optional. Enable backups for each weekday."
    echo "  -m                 Optional. Enable backups for the first day of each month."
    echo "  --delete           Optional. Include the --delete option in rsync to remove files in the destination that are no longer in the source."
    echo "  --help             Show this help message and exit."
    echo
    echo "Example:"
    echo "  $0 /path/to/parent -wm --delete"
}

# Function to perform rsync
do_rsync() {
    local source=$1
    local destination=$2
    # Ensure the destination directory exists
    mkdir -p "$destination"
    local rsync_opts="-arvz"
    if [ "$DELETE_ENABLED" = true ]; then
        rsync_opts="$rsync_opts --delete"
    fi
    rsync $rsync_opts "$source" "$destination"
}

# Function to handle daily backups
daily_backup() {
    if [ "$ENABLE_WEEKDAY_BACKUP" = true ]; then
        local weekday=$(date +%A)
        local dest_week="$PARENT_DIR/week/$weekday"
        do_rsync "$SOURCE_PATH" "$dest_week"
    fi
}

# Function to handle monthly backups
monthly_backup() {
    if [ "$ENABLE_MONTHLY_BACKUP" = true ]; then
        local month=$(date +%B)
        local day=$(date +%d)
        if [ "$day" -eq 1 ]; then
            local dest_month="$PARENT_DIR/month/$month"
            do_rsync "$SOURCE_PATH" "$dest_month"
        fi
    fi
}

# Check for --help option first
for arg in "$@"; do
    if [ "$arg" = "--help" ]; then
        show_help
        exit 0
    fi
done

# Check if a parent directory is provided
if [ $# -lt 1 ]; then
    echo "Error: No parent directory provided."
    echo
    show_help
    exit 1
fi

# Parsing command-line arguments
PARENT_DIR=$1
shift # Removing the first argument as it's the parent directory
ENABLE_WEEKDAY_BACKUP=false
ENABLE_MONTHLY_BACKUP=false
DELETE_ENABLED=false
SOURCE_PATH="$PARENT_DIR/current"

while (( "$#" )); do
    case $1 in
        -w) ENABLE_WEEKDAY_BACKUP=true;;
        -m) ENABLE_MONTHLY_BACKUP=true;;
        --delete) DELETE_ENABLED=true;;
        *) echo "Invalid option $1" >&2; exit 1;;
    esac
    shift
done

# Running backup functions
daily_backup
monthly_backup
