#!/bin/bash

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

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'
# This is copied from kubuntu-settings-desktop.install verbatim
desktop/etc/*                   etc/
desktop/kf5-settings/*          usr/share/kubuntu-default-settings/kf5-settings/
desktop/konsole/*               usr/share/konsole
desktop/kubuntu-logo.png        usr/share/pixmaps/kubuntu-logo.png
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/
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 > ';
find "${_source_dir}" -type l -printf '%l %p\n';

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 is show
first, followed by the target. UNLIKE this listing, all paths should be fully
qualified.
EOF

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


