#!/bin/bash

Info () {
title="Info"
author="Peter Starfinger"
mail="E-Mail: info@die-starfingers.de"
year="2021"
app="$(basename $0)"
msgid="The Starbuntu application
<i>\$app</i>
was written by"
info="$(gettext "$msgid" | (export PATH `envsubst --variables "$msgid"`; envsubst "$msgid"))"

yad --center --fixed --title="$title" --window-icon="info" --image="info" --borders=20 --text="$info"$'\n'$'\n'"$author"$'\n'"$mail"$'\n'$'\n'"$year"$'\n' --text-align=center --selectable-labels --button="OK" &
wunwa "$title" > /dev/null; xdotool key Home key Down
}

tip () {
    adv=$"instead of"
    alarm=$"Attention while inserting special characters:"
    title=$"Special Characters"
    conj=$"or"

    help1=$'\t'"    $adv nbsp&#9;  \' \' $conj \'&amp;#160;\'"
    help2=$'\t'"    $adv Tab&#9;  \'	\' $conj \'&amp;#9;\'"
    help3=$'\t'"    $adv Return&#9;  \'﻿﻿↵\' $conj \'&amp;#10;\'"
    help="    $alarm    &#10;&#10;$help1&#10;$help2&#10;$help3&#10;"
    yad --center --fixed --title="$title" --window-icon="/usr/share/icons/gnome/48x48/status/dialog-warning.png" --image="/usr/share/icons/gnome/48x48/status/dialog-warning.png" --image-on-top --borders=20 --text="$help" --selectable-labels --button="OK" &
    wunwa "$title" > /dev/null; xdotool key Home key Down
}

cut () {
  local clip
  title=$"Truncate History Files"
  ask=$"&#9;Truncate history files&#10;&#9;down to number entries:&#10;"
  button=$"Cancel"
  n=$(yad --center --title="$title" --window-icon="/usr/share/icons/gnome/48x48/status/dialog-warning.png" --image="/usr/share/icons/gnome/48x48/status/dialog-warning.png" --borders=20 --text="$ask" --entry --button="$button":1 --button="OK":0)
  knopf=$?
  [[ ! $n =~ ^[0-9]+$ ]] && knopf=1
  if [ $knopf -eq 0 ] && [[ $n =~ ^[0-9]+$ ]]; then
    until [[ -z "$@" ]]; do
	clip="$(cat "$1")"
	l=$(echo "$clip" | tr "\n" " " | sed 's/‖/\n/g' | wc -l); ((l++))
	for ((k=0; k<l-n; k++)); do clip="${clip%‖*}"; done
	echo "$clip" > "$1"
	shift
    done
  fi
}

trim () {
  local clip clp d_clip
  until [[ -z "$@" ]]; do
    cont=$(cat "$1")
    while [[ "$cont" =~ "‖‖" ]]; do cont=$(sed 's/‖‖/‖/g' <<< $cont); done
    [ "${cont::1}" = "‖" ] && cont="${cont: 1}"
    clip=($cont)
    [ "$1" = "$prim_file" ] && prim="${clip[0]}"
    clp=()
    for c in ${clip[@]}; do [[ $c ]] && clp+=("$c"); done
    clip=""; for c in ${clp[@]}; do clip="$clip‖$c"; done
    clip="${clip: 1}"
    echo "$clip"  > "$1"
    d_clip=""
    for c in ${clp[@]}; do
	if [ -n "$c" ]; then
	    [ ${#c} -le 80 ] && d_clip="$d_clip‖$c" || d_clip="$d_clip‖${c::40} ... ${c: -40}"
#	    d_clip="$(echo "$d_clip" | sed 's/</\&#60;/g; s/>/\&#62;/g')"
	fi
    done
    d_clip="${d_clip: 1}"
    clipb+=("$d_clip")
    shift
  done
}

param="$@"
[ "$param" = "info" ] && Info && exit 0
[ "$param" = "tip" ] && tip && exit 0
prim_file=$HOME/.local/share/clipboard/primary
sec_file=$HOME/.local/share/clipboard/secondary
tert_file=$HOME/.local/share/clipboard/tertiary

label[0]=$"&#10;Prim. Selection: &#10;"
label[1]=$"&#10;Sec. Selection:  &#10;"
label[2]="&#10;"$"Clipboard"":   &#10;"
title=$"Current Clipboard Entries With History"
text=$"&#10;The entries are editable!&#10;&#10;&#10;"
button1=$"Cancel"
button2=$"Truncate"
button3=$"Hints"
button4=$"Help"
lang_dir=$"C"

IFS='‖'; clipb=()
trim $prim_file $sec_file $tert_file
IFS=$'\v'
sleep 0.1

for ((k=0; k<3; k++)); do
    [ -z "${clipb[$k]}" ] && clipb[$k]=" "
    clipb[$k]="${clipb[$k]//\\/\\\\}"
done

bash -c 'title=$"Current Clipboard Entries With History"; wunwa "$title" > /dev/null; xdotool key End' &

clips=`yad --center --title="$title" --height=400 --width=1000 --borders=20 --text="$text" --text-align=center --window-icon=$HOME/.icons/clipit-trayicon.png --image=$HOME/.icons/clipit-trayicon.png --separator="¦" --form --item-separator="‖" --field="${label[0]}":CBE " ${clipb[0]}" --field="${label[1]}":CBE " ${clipb[1]}" --field="${label[2]}":CBE " ${clipb[2]}" --button="$button1":1 --button="$button2":3 --button="$button3":"$0 tip" --button="$button4":"yelp /usr/share/help/$lang_dir/Starbuntu/starbuntu-apps-details.page#prim-sec-clip" --button="Info":"$0 info" --button="Update":2 --button="OK":0`

button=$?
if [ $button -eq 0 ]; then

  prim_change=false
  clips="$(echo "$clips" | sed 's/^ //; s/¦ /¦/g; s/\&\#9;/\t/g; s/\&\#10;/\n/g; s/↵/\n/g; s/\&\#160;/ /g; s/\\\\/\\/g')"	
  if [ -n "$clips" ]; then
    postclipb=()
    while [ -n "$clips" ]; do
	postclipb=(${postclipb[@]} ${clips%%¦*}) && clips="${clips#*¦}"
    done

    IFS='‖'	
    for ((k=0; k<3; k++)); do
	inflate () {
	    case $k in
		0) clp=($(cat $prim_file)) ;;
		1) clp=($(cat $sec_file)) ;;
		2) clp=($(cat $tert_file)) ;;
	    esac
	    for ((c=0; c<${#clp[@]}; c++)); do
		[[ ${clp[$c]} =~ ^"${post::40}".*"${post: -40}"$ ]] && break #  && [[ ! ${clp[$c]} =~ " ... " ]] && break
	    done
	    
	    post="${clp[$c]}"
	}
	
	post="${postclipb[$k]}"
	[[ "$post" =~ " ... " ]] && inflate "$post"

	if [[ "${clipb[$k]%%‖*}" != "${postclipb[$k]}" ]]; then
	    case $k in
		0) clip_file=$prim_file
		   prim_change=true ;;
		1) clip_file=$sec_file ;;
		2) clip_file=$tert_file ;;
	    esac
	    file_cont="$(cat $clip_file)"
	    trimmed_cont="${file_cont//‖${post//\\/\\\\}/}"
	    echo -n "$post‖$trimmed_cont" > $clip_file
	fi
	post="${post//&#x2016;/‖}"
	case $k in
	    0)  echo -n "$post" | xsel -ip ;;
	    1)  echo -n "$post" | xsel -is ;;
	    2)  echo -n "$post" | xsel -ib ;;
	esac
	
    done
  fi

  ! $prim_change && echo -n "$prim" | xsel -ip

elif [ $button -eq 3 ]; then
  cut $prim_file $sec_file $tert_file
  button=2
fi

[ $button -eq 2 ] && $0 &

exit 0
