#!/bin/sh
#helper library for i3 config actions
export TEXTDOMAINDIR=/usr/share/locale
export TEXTDOMAIN=i3rosa

HELP(){
	echo "$0 is a helper library for i3 config actions" ; echo ""
	echo "Usage:" 
	echo "$0 <function> <pars>"
	echo OR
	echo "source $(realpath $0) ; <function> <pars>" ; echo ""
	echo "Avaliable functions:"
	cat $0 |grep \(\).*{$ |sed 's/()[[:space:]]*{[[:space:]]*$//'
	exit 1
}

for arg in $@
do
	case "${arg}" in
		"-h" | "--help") [ "`basename $0`" = "pfs" ] && { HELP ;exit 1 ; } ;;
	esac
done


ShowKeys() {
    i3-msg -t get_config | sed -ne 's/\$mod/MOD/' -e 's/\$alt/ALT/' -e '/^### /{x;d} ; /^bindsym /{H;d} ;/^[[:space:]]*$/{p;x;p}' |
    sed  -e '/./,/^$/!d' -e 's/bindsym //' | 
    zenity --text-info  --width 600 --height 600  --cancel-label="O" --ok-label="k"
}

QuickTerm() {
    xdotool search --onlyvisible --classname dropdown windowunmap && return
    if ! xdotool search --classname dropdown windowmap ; then 
	export URXVT_PERL_LIB=/usr/share/rosa-i3-configs/
        urxvt -pe tabbedalt -name dropdown &
	until xdotool search --classname dropdown windowmap ; do
	    sleep 0.1
	done
    fi
    i3-msg [instance="dropdown"] focus, move position center, move container to workspace current
}

QuickTermResize() {
	screen_size=$(xrandr |grep \* |awk '{print $1}' |sort| head -n1)
	width=$(echo $screen_size |cut -d x -f1)
	height=$(echo $screen_size |cut -d x -f2)
	WIDTH=$(($width - 100))
	HEIGHT=$(($height - 100))
	i3-msg [instance="dropdown"] resize set $WIDTH $HEIGHT
}

PowerMenu() {
MSG01=$(gettext -s "Power_menu")
MSG02=$(gettext -s "Poweroff")
MSG03=$(gettext -s "Reboot")
MSG04=$(gettext -s "Sleep")
MSG05=$(gettext -s "Logout")
case $(zenity --list --text $MSG01 --column=1 --column=2 --hide-header --print-column=1 --hide-column=1 \
    poweroff $MSG02 reboot $MSG03 sleep $MSG04 logout $MSG05) in
    poweroff ) systemctl poweroff ;;
    reboot ) systemctl reboot ;;
    sleep ) systemctl suspend ;;
    logout ) i3-msg exit ;;
 esac
}

SwapMonitors() {
    killall polybar
    $(dirname $0)/monitors swap
    feh --bg-center /usr/share/wallpapers/default.jpg
    $(dirname $0)/polybar.sh
}

InitMonitors() {
    killall polybar
    $(dirname $0)/monitors init
    feh --bg-center /usr/share/wallpapers/default.jpg
    $(dirname $0)/polybar.sh
}

if [ "$(basename $0)" == "i3lib" ] ;then
    [ $1 ] || HELP
    command="$1"
    shift
    $command $@
    [ $? -ne 0 ] && echo "$command ERROR!"
fi
