#!/usr/bin/env bash
# wizard.sh: a backup wizard made with dialog. Pick share folders in a checklist, confirm, watch a gauge while
# rsync -n checks each folder, and read in a message box what a real backup would copy. It copies nothing.
# Run it: bash wizard.sh [DESTINATION]   (default /share/Backup; SMB_CONF may point at a sample smb.conf)
set -uo pipefail

conf=${SMB_CONF:-/etc/config/smb.conf}
dest=${1:-/share/Backup}
title="NAS backup wizard"
export NCURSES_NO_UTF8_ACS=1 # frames drawn with Unicode lines instead of the terminal's line set
DIALOGRC=$(mktemp) && export DIALOGRC
result=$(mktemp)
trap 'rm -f "$DIALOGRC" "$result"; clear' EXIT
cat >"$DIALOGRC" <<'EOF'
use_shadow = OFF
screen_color = (WHITE,BLACK,OFF)
dialog_color = (WHITE,BLACK,OFF)
title_color = (YELLOW,BLACK,ON)
border_color = (YELLOW,BLACK,OFF)
border2_color = (YELLOW,BLACK,OFF)
menubox_border_color = (YELLOW,BLACK,OFF)
menubox_border2_color = (YELLOW,BLACK,OFF)
button_active_color = (BLACK,YELLOW,ON)
button_inactive_color = (WHITE,BLACK,OFF)
button_key_active_color = (BLACK,YELLOW,ON)
button_key_inactive_color = (YELLOW,BLACK,ON)
button_label_active_color = (BLACK,YELLOW,ON)
button_label_inactive_color = (WHITE,BLACK,ON)
menubox_color = (WHITE,BLACK,OFF)
item_color = (WHITE,BLACK,OFF)
item_selected_color = (BLACK,YELLOW,ON)
check_color = (WHITE,BLACK,OFF)
tag_color = (YELLOW,BLACK,ON)
tag_selected_color = (BLACK,YELLOW,ON)
tag_key_color = (YELLOW,BLACK,ON)
tag_key_selected_color = (BLACK,YELLOW,ON)
check_selected_color = (BLACK,YELLOW,ON)
gauge_color = (YELLOW,BLACK,ON)
EOF

# the share folders: each [Name] section of smb.conf with a path to a folder that exists
names=() paths=() section=''
while IFS= read -r line; do
  case $line in
    \[*\]) section=${line#[} section=${section%]} ;;
    path\ =\ *)
      path=${line#path = }
      [[ $section != global && -d $path ]] && names+=("$section") && paths+=("$path")
      ;;
  esac
done <"$conf"

items=()
for i in "${!names[@]}"; do items+=("${names[i]}" "${paths[i]}" off); done
picked=$(dialog --stdout --separate-output --backtitle "$title" --title " Share folders " \
  --checklist "Space ticks a folder, Enter goes on.\nThe backup goes to $dest." 14 64 6 "${items[@]}") || exit 0
[[ -n $picked ]] || exit 0
mapfile -t picked <<<"$picked" # one folder a line, so names may hold spaces

dialog --backtitle "$title" --title " Confirm " --yes-label "Check" --no-label "Cancel" \
  --no-collapse --yesno "Check what a backup of these folders would copy?\n\n  ${picked[*]}\n\nto $dest\n\nThis is a dry run (rsync -n): nothing is copied." 13 64 || exit 0

# the gauge reads numbers from the loop; between XXX lines it also gets a new text
for i in "${!picked[@]}"; do
  name=${picked[i]}
  for j in "${!names[@]}"; do [[ ${names[j]} == "$name" ]] && path=${paths[j]}; done
  printf 'XXX\n%d\nChecking %s ...\nXXX\n' $((i * 100 / ${#picked[@]})) "$name"
  files=0 bytes=0
  while read -r size file; do # rsync -n prints "SIZE NAME" for each file it would copy; folders end in /
    [[ $file == */ ]] && continue
    files=$((files + 1)) bytes=$((bytes + size))
  done < <(rsync -a -n --out-format='%l %n' "$path/" "$dest/$name/" 2>/dev/null)
  printf '  %-14s %5d %9d KiB\n' "$name" "$files" $(((bytes + 1023) / 1024)) >>"$result"
  sleep 0.4 # long enough to read the gauge
done | dialog --backtitle "$title" --title " Checking " --gauge "Starting ..." 8 64 0

table=$(<"$result")
dialog --backtitle "$title" --title " Dry run done " --no-collapse --msgbox \
  "rsync -n would copy to $dest:\n\n  Folder         Files      Size\n${table//$'\n'/\\n}\n\nNothing was copied. To copy, remove -n in the rsync line." 14 64
