#!/usr/bin/bash
# Add at the beginning of your script
# exec > "/tmp/screenshot-log.txt" 2>&1
# set -x  # Enable debugging

# When launching tools like wl-clipboard from window managers or other
# non-terminal environments, explicitly setting up those file descriptors
# (stdin, stdout, and stderr) ensures the tools have a proper environment
# to run in.
 
# Ensure file descriptors are properly set up
exec < /dev/null
exec > /dev/tty
exec 2> /dev/tty

if ! [[ -d ~/Pictures ]];
then
  mkdir ~/Pictures
fi

# maim -sq | xclip -i -selection clipboard -t image/png
slurp | grim -g - - | wl-copy
FNAME="$(date '+%s').png"
# xclip -o -selection clipboard | tee "$HOME/Pictures/${FNAME}" | tesseract stdin stdout | grep -v '^$' | tr '\n' ' ' | xclip -i
LANG=eng
# LANG=$(echo -e 'eng\njpn' | dmenu -i)
# xclip -o -selection clipboard | tee "$HOME/Pictures/${FNAME}" | tesseract -l "${LANG}" stdin stdout | grep -v '^$' | xclip -i
wl-paste | tee "$HOME/Pictures/${FNAME}" | tesseract -l "${LANG}" stdin stdout | grep -v '^$' | wl-copy --primary

# xclip -o > "$HOME/Pictures/.${FNAME}.txt"
wl-paste --primary > "$HOME/Pictures/.${FNAME}.txt"
notify-send -t 10000 -h string:x-canonical-private-synchronous:scanned "OCRed: $(wl-paste --primary)"


# GEOMETRY="813x1007+555+48"
# maim --geometry="${GEOMETRY}" -q | xclip -i -selection clipboard -t image/png
# FNAME="$(date '+%s')"
# FPATH="/tmp/book"
# 
# if ! [[ -d "$FPATH" ]];
# then
#     echo "Create $FPATH first"
# else
#     xclip -o -selection clipboard | tee "${FPATH}/${FNAME}.png" | tesseract stdin stdout > "${FPATH}/${FNAME}.txt" && xdotool click 1
# fi
#
#    maim --geometry="813x1007+555+48" -q -o "$(date '+%s').png" && xdotool click 1


