#!/bin/bash

orient="$@"

if [ -z "$orient" ]; then orient=5; fi

dim=$(xdpyinfo | grep dimensions)
h_dim=${dim%%x*}
h_dim=${h_dim##* }
v_dim=${dim#*x}
v_dim=${v_dim%% *}

if [ -n "$(cat /tmp/hsperfdata_root/tint2/tint2rc | grep '^panel_position' | grep 'vertical')" ]; then
    h_offset=84
    v_offset=20
else
    h_offset=0
    v_offset=50
fi

if [ ${DISPLAY:1:1} -gt 0 ]; then h_offset=0; v_offset=14; fi

w_act_arr=($(wmctrl -lG | grep $(printf '%x\n' $(xdotool getactivewindow))))

case $orient in
    1) xdotool windowmove $(xdotool getactivewindow) $h_offset $((v_dim-w_act_arr[5]-v_offset));;
    2) xdotool windowmove $(xdotool getactivewindow) $(((h_dim-w_act_arr[4]+h_offset)/2)) $((v_dim-w_act_arr[5]-v_offset));;
    3) xdotool windowmove $(xdotool getactivewindow) $((h_dim-w_act_arr[4])) $((v_dim-w_act_arr[5]-v_offset));;
    4) xdotool windowmove $(xdotool getactivewindow) $h_offset $(((v_dim-w_act_arr[5]-v_offset)/2));;
    5) xdotool windowmove $(xdotool getactivewindow) $(((h_dim-w_act_arr[4]+h_offset)/2)) $(((v_dim-w_act_arr[5]-v_offset)/2));;
    6) xdotool windowmove $(xdotool getactivewindow) $(((h_dim-w_act_arr[4]))) $(((v_dim-w_act_arr[5]-v_offset)/2));;
    7) xdotool windowmove $(xdotool getactivewindow) $h_offset 0;;
    8) xdotool windowmove $(xdotool getactivewindow) $(((h_dim-w_act_arr[4]+h_offset)/2)) 0;;
    9) xdotool windowmove $(xdotool getactivewindow) $((h_dim-w_act_arr[4])) 0;;
    s1) xdotool windowmove $(xdotool getactivewindow) $h_offset $(((v_dim-v_offset)/2)) windowsize $(xdotool getactivewindow) $(((h_dim-h_offset)/2)) $(((v_dim-v_offset)/2));;
    s3) xdotool windowmove $(xdotool getactivewindow) $(((h_dim+h_offset)/2)) $(((v_dim-v_offset)/2)) windowsize $(xdotool getactivewindow) $(((h_dim-h_offset)/2)) $(((v_dim-v_offset)/2));;
    s7) xdotool windowmove $(xdotool getactivewindow) $h_offset 0 windowsize $(xdotool getactivewindow) $(((h_dim-h_offset)/2)) $(((v_dim-v_offset)/2));;
    s9) xdotool windowmove $(xdotool getactivewindow) $(((h_dim+h_offset)/2)) 0 windowsize $(xdotool getactivewindow) $(((h_dim-h_offset)/2)) $(((v_dim-v_offset)/2));;
    s4) xdotool windowmove $(xdotool getactivewindow) $h_offset 0 windowsize $(xdotool getactivewindow) $(((h_dim-h_offset)/2)) $((v_dim-v_offset));;
    s6) xdotool windowmove $(xdotool getactivewindow) $(((h_dim+h_offset)/2)) 0 windowsize $(xdotool getactivewindow) $(((h_dim-h_offset)/2)) $((v_dim-v_offset));;
    s8) xdotool windowmove $(xdotool getactivewindow) $h_offset 0 windowsize $(xdotool getactivewindow) $((h_dim-h_offset)) $(((v_dim-v_offset)/2));;
    s2) xdotool windowmove $(xdotool getactivewindow) $h_offset $(((v_dim-v_offset)/2)) windowsize $(xdotool getactivewindow) $((h_dim-h_offset)) $(((v_dim-v_offset)/2));;
esac
