#!/bin/bash

set -u;
declare _source_dir _manifest_str _sync_list _src _tgt
_source_dir="$HOME/26.04LTS/kubuntu-settings-desktop.wip";
_source_dir='';

cat << EOF
This is a helper script to be run at the top level directory to help copy all
files from a development system into the package. This is challenging because
the files do not map neatly to the filesystem, but instead are often nested
within each other.

The _source_dir is the directory that holds the updated and tested files.
|${_source_dir}|

If the _source_dir is incorrect, or you are NOT running from the package root,
exit now and fix either issue.
EOF
read -rp 'Press return to continue, or CTRL+C to exit > ';

_manifest_str="$(cat << 'EOF'
# These are tightend up from desktop/etc below to avoid silly labor
# To find this, use meld on desktop/etc, then use File Filters >
#   [x] Same, [x] # Modified (disable [x] New)
desktop/etc/X11/Xsession.d/*            etc/X11/Xsession.d/
desktop/etc/kubuntu-default-settings/*  etc/kubuntu-default-settings/
desktop/etc/sddm.conf.d/*               etc/sddm.conf.d/
desktop/etc/skel/*                      etc/skel/
desktop/etc/xdg/*                       etc/xdg/
# Tightned up from desktop-icons
desktop/icons/breeze-dark/*             usr/share/icons/breeze-dark/
desktop/icons/breeze/*                  usr/share/icons/breeze/
desktop/icons/hicolor/*                 usr/share/icons/hicolor/

# This is copied from kubuntu-settings-desktop.install verbatim
# desktop/etc/*                 etc/X11/ #Disabled: too broad, see above
desktop/kf5-settings/*          usr/share/kubuntu-default-settings/kf5-settings/
desktop/konsole/*               usr/share/konsole/
desktop/pixmaps/*               usr/share/pixmaps/
desktop/usr-share/*             usr/share/kubuntu-default-settings/
desktop/theme/*                 usr/share/plasma/
desktop/sddm/*                  usr/share/sddm/
desktop/kcm/*                   usr/share/plasma/systemsettings/externalmodules/
# desktop/icons/*               usr/share/icons/ #Disabled: too broad, see above
desktop/live/usr-share/*        usr/share/
desktop/bin/*                   usr/bin/
fonts/*                         usr/share/fonts/truetype/
desktop/plasma/plasma-welcome/extra-pages/* usr/share/plasma/plasma-welcome/extra-pages/
desktop/applications/*          usr/share/applications/

# This is copied directly from plymouth-theme-kubuntu-logo.install
plymouth/kubuntu-logo/* usr/share/plymouth/themes/kubuntu-logo/

# This is copied directly from plymouth-theme-kubuntu-text.install
plymouth/kubuntu-text/* usr/share/plymouth/themes/kubuntu-text/
EOF
)";

_manifest_str="$(
  sed -e 's?/\* ? ?g' -e 's/\s\+/|/g' -e 's?/$??g' <<< "${_manifest_str}"
)";
_manifest_str="$(grep -v '^\(#\|\s*$\)' <<< "${_manifest_str}")";
mapfile -t _sync_list <<< "${_manifest_str}";

for _line in "${_sync_list[@]}"; do
  IFS='|' read -r -a _bit_list <<< "${_line}";
  _src="${_source_dir}/${_bit_list[1]}";
  _tgt="./${_bit_list[0]}";

  read -rp "Consider |${_src}| diff to |${_tgt}|";
  if diff -r --brief "${_src}" "${_tgt}" >/dev/null; then
    echo "Paths match, continuing...";
  else
    meld "${_src}" "${_tgt}" ;
  fi
done

read -rp 'Press enter to get a list of symlinks > ';
echo '== BEGIN links ==============================';
find ./ -type l -printf '%l %P\n';
echo '== . END links ==============================';
echo;
cat <<EOF
Make sure the number of links and all paths are accounted for in the
debian/<package_name>.links file. As with this listing, the source (a real
file) should be shown first, followed by the symbolic link target.

UNLIKE THIS LISTING, all paths should be fully qualified in the links file but
with no leading slash.
EOF
echo;

read -rp 'This helper is finished. Press ENTER to exit > ';
exit;


