#!/bin/bash
# Author  : Gaston Gonzalez
# Date    : 14 January 2024
# Purpose : Back up select files from the user's home directory.
#
# The backups are limited to the following:
# 1. EmComm Tools configuration
# 2. Winlink email

TODAY=$(date +"%Y%m%d")
BACKUP_FILE="${HOME}/etc-user-backup-${HOSTNAME}-${TODAY}.tar.gz"

# All paths are relative to $HOME.
BACKUP_DIRS=(
  ".config/emcomm-tools"
  ".local/share/emcomm-tools"
  ".local/share/pat"
)

ESC="\033"
CYAN="$ESC[36m"
GREEN="$ESC[0;32m"
RED="$ESC[0;31m"
WHITE="$ESC[1;37m"
YELLOW="$ESC[1;33m"
NC="$ESC[0m"

tar -czf ${BACKUP_FILE} \
   --transform 's|^.*\.local|.local|' \
   -C "${HOME}" "${BACKUP_DIRS[@]}"

[ $? -ne 0 ] && et-log "Error creating backup" && exit 1

# Test that we can read if after we create it.
TAR_OUT=$(tar -tzf ${BACKUP_FILE})
[ $? -ne 0 ] && et-log "Can't read backup file: ${BACKUP_FILE}. Error creating backup." && exit 1

echo -e "${GREEN}Backup created: ${WHITE}${BACKUP_FILE}\n"

echo -e "${CYAN}Note: Only the following directories were backed up:"
for dir in "${BACKUP_DIRS[@]}"; do
  echo "* $dir"
done

echo -e "\n${YELLOW}If you need files outside of these directories, please back them up manually.${NC}"
