#!/usr/bin/env bash
set -Eeuo pipefail
export TEXTDOMAIN=${TEXTDOMAIN:-eczos-platform-tools}
export TEXTDOMAINDIR=${TEXTDOMAINDIR:-/usr/share/locale}
_() { gettext "$1"; }

if [[ $# -eq 0 && -n ${DISPLAY:-}${WAYLAND_DISPLAY:-} ]]; then
    exec /usr/bin/eczos-ui migration
fi

if [[ $(id -u) -eq 0 ]]; then
    printf '%s\n' "$(_ 'Run migration as the signed-in user, not as root.')" >&2
    exit 2
fi

mode=dry-run
source_profile=''
gui=true
while (( $# )); do
    case $1 in
        --dry-run) mode=dry-run ;;
        --apply) mode=apply ;;
        --source) shift; source_profile=${1:-} ;;
        --no-gui) gui=false ;;
        -h|--help) printf '%s\n' "$(_ 'Usage: eczos-migrate [--dry-run|--apply] [--source WINDOWS-PROFILE] [--no-gui]')"; exit 0 ;;
        *) printf "$(_ 'Unknown option: %s')\n" "$1" >&2; exit 2 ;;
    esac
    shift
done

if [[ -z "$source_profile" && $gui == true && -n ${DISPLAY:-}${WAYLAND_DISPLAY:-} ]]; then
    source_profile=$(kdialog --getexistingdirectory "$HOME" --title "$(_ 'Select the Windows user folder')" || true)
fi
[[ -n "$source_profile" ]] || { printf '%s\n' "$(_ 'No Windows profile selected.')" >&2; exit 2; }
source_profile=$(realpath -e -- "$source_profile")
[[ -d "$source_profile" && "$source_profile" != / && "$source_profile" != "$HOME" ]] || {
    printf '%s\n' "$(_ 'Invalid source folder.')" >&2; exit 2;
}

folders=(Desktop Documents Downloads Music Pictures Videos Favorites)
found=false
for folder in "${folders[@]}"; do [[ -d "$source_profile/$folder" ]] && found=true; done
[[ $found == true ]] || { printf '%s\n' "$(_ 'No known Windows user folders were found in this folder.')" >&2; exit 1; }

state_dir=${XDG_STATE_HOME:-"$HOME/.local/state"}/eczos/migration
mkdir -p "$state_dir"
log="$state_dir/$(date +%Y%m%d-%H%M%S).log"
rsync_options=(-a --safe-links --ignore-existing --exclude=desktop.ini --exclude=Thumbs.db)
[[ $mode == dry-run ]] && rsync_options+=(--dry-run --itemize-changes)

if [[ $mode == apply && $gui == true && -n ${DISPLAY:-}${WAYLAND_DISPLAY:-} ]]; then
    kdialog --yesno "$(printf "$(_ 'Existing files will not be overwritten. Transfer files from:\n%s?')" "$source_profile")" || exit 0
fi

for folder in "${folders[@]}"; do
    [[ -d "$source_profile/$folder" ]] || continue
    case $folder in
        Desktop) destination=$(xdg-user-dir DESKTOP) ;;
        Documents|Favorites) destination=$(xdg-user-dir DOCUMENTS) ;;
        Downloads) destination=$(xdg-user-dir DOWNLOAD) ;;
        Music) destination=$(xdg-user-dir MUSIC) ;;
        Pictures) destination=$(xdg-user-dir PICTURES) ;;
        Videos) destination=$(xdg-user-dir VIDEOS) ;;
    esac
    [[ -n "$destination" ]] || destination="$HOME/$folder"
    [[ $folder == Favorites ]] && destination="$destination/Windows-favorieten"
    mkdir -p "$destination"
    rsync "${rsync_options[@]}" "$source_profile/$folder/" "$destination/" | tee -a "$log"
done

if [[ $mode == dry-run ]]; then
    message=$(printf "$(_ 'Preview complete; nothing has been copied yet. Log: %s')" "$log")
else
    message=$(printf "$(_ 'Transfer complete. Existing files were preserved. Log: %s')" "$log")
fi
printf '%s\n' "$message"
if [[ $gui == true && -n ${DISPLAY:-}${WAYLAND_DISPLAY:-} ]]; then kdialog --msgbox "$message" --title "$(_ 'Move from Windows')"; fi
