#!/bin/bash

Info () {
local app
title="Info"
author="Peter Starfinger"
mail="E-Mail: info@die-starfingers.de"
year="2026"
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
}


param="$@"
[ "$1" = "info" ] && Info && exit 0

title="Epub-Reader"
if [ -z "$param" ] || [ "$param" = '%f' ]; then
    text='\n'"$(echo $"Which EPUB e-book shall be opened?")"'\n'
    field="$(echo "E-Book"): "
    button1=$"Cancel"
    cd
    entries=`yad --center --title="$title" --width=600 --height=200 --window-icon="$HOME/.icons/epub.png" --borders=10 --text="<b>$text</b>" --text-align=center --form --item-separator=" " --field="$field":MFL --button="$button1":1 --button="Info":"$0 info" --button="OK":0`
    [ $? -eq 0 ] && $0 "${entries::-1}" &
    exit 0
fi

ebook="$(basename "${param/.epub}")"
# ebook="$(echo "$ebook" | sed 's/ /_/g')" # for opener="browser 1"
opener="yelp"
# opener="browser 1"
wm_name=$"Help"
# wm_name="Google Chrome"
mkdir "/tmp/$ebook"
cd "/tmp/$ebook"
unzip "$param" -d .
for f in $(find -type f -name "*.xhtml"); do mv "$f" "${f/.xhtml/.html}"; done
grep -lr '\.xhtml' * | xargs sed -i 's/\.xhtml/.html/g'
IFS=$'\n'
opf="$(find -name content.opf)"
path="$(dirname "$opf")"
cd "$path"
sed -i 's|/><|/>\n<|g' content.opf
content="$(cat content.opf)"
itemref=("$(grep 'itemref' <<< "$content")")
index=()
for i in ${itemref[@]}; do
    ref="$(echo "$i" | cut -d\" -f2)"
    href="$(grep "id=\"$ref\"" <<< "$content")"
    href="$(echo "${href#*href=}" | cut -d\" -f2)"
    index=(${index[@]} "$href")
done
n_index=${#index[@]}
dir_0="$(dirname "${index[0]}")"
pre_0="/tmp/$ebook/$path/$dir_0/"
dir="$(dirname "$href")"
pre="/tmp/$ebook/$path/$dir/"
sed -i "/<\/body>/ i <p\ align=\"center\"><big><b><a\ href=\"$pre$(basename "${index[1]}")\">$(gettext "Next")</a></b></big></p>" "${index[0]}"
for ((k=1; k<n_index-1; k++)); do
    sed -i -E "s|(<body[^>]*>)|\1\n|; s|(</body>)|\n\1|" "${index[$k]}"
    [ $k -gt 1 ] && pre_0=""
    sed -i "/<body/ a <p\ align=\"center\"><big><b><a\ href=\"$pre_0$(basename "${index[$((k-1))]}")\">$(gettext "Previous")</a></b></big></p>" "${index[$k]}"
    sed -i "/<\/body>/ i <p\ align=\"center\"><big><b><a\ href=\"$(basename "${index[$((k+1))]}")\">$(gettext "Next")</a></b></big></p>" "${index[$k]}"
done
[ $n_index -eq 2 ] && pre_0="/tmp/$ebook/$path/$dir_0/" || pre_0=""
sed -i -E "s|(<body[^>]*>)|\1\n|" "${index[$((n_index-1))]}"
sed -i "/<body/ a <p\ align=\"center\"><big><b><a\ href=\"$pre_0$(basename "${index[$((n_index-2))]}")\">$(gettext "Previous")</a></b></big></p>" "${index[$((n_index-1))]}"
eval "$opener ${index[0]} &"
id=$(wunwa "$wm_name") && sh -c "xseticon -id $id ~/.icons/epub.png; orient-act-win 8" && place-window $id 1 0 800 1000 $((($(dims | head -1 | cut -d'|' -f1)-800)/2)) 0
while [[ "$(wmctrl -l)" =~ "$wm_name" ]]; do sleep 1; done
cd /tmp
rm -r "$ebook"
