#!/bin/bash

# updater_shlip
#
# helper script to define utility functions
#
# part of apt-notifier package
#
# this bash scripted is meant to be "sourced"
# at /usr/lib/apt-notifier/lib/updater_shlib


press_any_key_stop_auto_close() {

    local default_timout=10
    if [ -z "${AUTO_CLOSE_TIMEOUT}" ]; then
        AUTO_CLOSE_TIMEOUT=$default_timout
    fi
    if [ -n "${AUTO_CLOSE_TIMEOUT//[0-9]/}" ]; then
        AUTO_CLOSE_TIMEOUT=$default_timout
    fi

    local count_timeout=${AUTO_CLOSE_TIMEOUT}

    local length_timeout=${#count_timeout}
    local count_str='<'
    local count_len=24
    local cols=$(/usr/bin/tput cols)
    local count_line=$(printf "$count_str%.0s" $(eval echo {1..$count_len}));
    local space_line=$(printf '%*s' $cols)
    local done_line=$(printf '_''%.0s' $(eval echo {1..$cols}))

    local anykey_to_stop=$(mygettext -d apt-notifier "press any key to stop automatic closing")
    local anykey_to_close=$(mygettext -d apt-notifier "press any key to close")

    anykey_to_stop="${anykey_to_stop^}..."
    anykey_to_close="${anykey_to_close^}..."

    local count_down=$count_timeout
    local cntd_line
    local cntd_space_line
    for i in $(eval echo {1..$((count_timeout+1))}); do

        (( count_str_len = count_len - ( (i-1) * count_len) /count_timeout ))
        (( spc_len = count_len - count_str_len ))
        cntd_line="${count_line:0:$count_str_len}";
        cntd_done_line="${done_line:0:$spc_len}"
        printf $'\r'"%${length_timeout}i ${cntd_line}${cntd_done_line}: $anykey_to_stop" $count_down;
        (( count_down = count_timeout-i))
        read -sr -t1 -n1 && break;
    done && printf $'\r'"$space_line" || return 0
    printf $'\r'"$space_line"
    return 1
}

press_any_key_to_close() {

    local cls_msg_en="press any key to close"
    local cls_msg=$(mygettext -d apt-notifier "press any key to close")
    # capitilize
    cls_msg=${cls_msg^}
    # add ellipses
    cls_msg="${cls_msg^}..."
    read -rsn 1 -p $'\r'"$cls_msg" -t 999999999
    return 0
}


translate() {
    local text="$1"
    local domain="MX"

    #text=$(gettext -d apt-notifier "$text")
    text=$(mygettext -d apt-notifier "$text")
    # check DOMAIN is set - but could be  empty
    [ -z "${DOMAIN+set}" ] && echo "$text" && return
    # check text contains $domain
    [ "${text}" == "${text/$domain}" ] && echo $text && return
    text="${text//$domain /$DOMAIN }"
    text="${text// $domain/ $DOMAIN}"
    text="${text/  / }"
    echo "$text"
}

updater_show() {

    local msg="$1"
    local cmd="$2"
    local title="$3"
    local icon="$4"
    local icon_kde="$5"

    local T="$title"
    local I="$icon"
    # xdo
    read DW DH < <(xdotool getdisplaygeometry)
    if [ -z "$DW" ]; then
        sleep 2
        read DW DH < <(xdotool getdisplaygeometry)
    fi
    TH=$((DH*2/3))  # desired terminal hight
    if ((DW <= 1440)); then
		TW=$((DW*4/5))  # desired terminal width
    else
		TW=$((DW*2/3))  # desired terminal width
    fi

    read -r XOFF YOFF < <(xrandr | sed -n -r '/connected primary/{/.*[0-9]+x[0-9]+\+([0-9]+)\+([0-9]+).*/{s::\1 \2:p;q}}')

    : ${XOFF:=0}
    : ${YOFF:=0}

    TX=$(((DW-TW)/2 + XOFF))
    TY=$(((DH-TH)/2 + YOFF))

    
    XSEARCH="xdotool sleep 0.2 search --onlyvisible --name '$T'"
    XSIZE="windowsize $TW $TH"
    XMOVE="windowmove $TX $TY"
    CLASS_NAME='apt-notifier'
    XCLASS="set_window --classname ${CLASS_NAME} --class ${CLASS_NAME}"

    XDO="$XSEARCH $XSIZE $XMOVE $XCLASS"


    CW=10 # char width - rough default
    CH=20 # char hight - rough default

    #G="--geometry=$(($TW/$CW))x$(($TH/$CH))+$TX+$TY"
    G="$(($TW/$CW))x$(($TH/$CH))+$TX+$TY"

    SLEEP=0.3 # sleep

    C='bash -c "echo '"$msg"'; sleep 0.5;'" $XDO;"' '"$RUN"'"'
    K='bash -c "echo '"$msg"'; sleep 1; '"$RUN"'"'


    case $(readlink -e /usr/bin/x-terminal-emulator) in

      *gnome-terminal.wrapper)
            gnome-terminal.wrapper $G -T "$T" -e "$C"
            ;;
      *konsole)
            if pgrep -x plasmashell >/dev/null; then
               konsole --nofork --hide-menubar -qwindowgeometry "${TW}x${TH}+$TX+$TY" -qwindowicon "$icon_kde" -qwindowtitle "$T" -e "$K"
            else
               konsole -e "$C"
               sleep 5
            fi
            ;;
      *roxterm)
            roxterm --hide-menubar --separate "--geometry=$G" -T "$T" -e "$C" 2>/dev/null
            ;;
      *urxvt)
            urxvt  -geometry "$G" -icon "$icon" -title "$T"  -e sh -c "echo '$msg'; sleep 1; $XDO; $RUN"
            ;;
      *xfce4-terminal.wrapper | *xfce4-terminal)

            xfce4-terminal --disable-server --hide-menubar  "--geometry=$G"  "--icon=$icon"  -T "$T" -e "$C" 2>&1 | grep -v 'SESSION_MANAGER environment variable not defined'
            ;;
      *xterm)
            xterm  -fa monaco -fs 12 -bg black -fg white  -xrm 'XTerm.vt100.allowTitleOps: false' -T "$T"  -e "$C"
            ;;
      *) x-terminal-emulator -T "$T" -e "$C"
            ;;
    esac
}

mygettext() {
	gettext "$@"
}
