export TERMINAL=xterm
interfaces+=("xterm");

resolution() {
   { reso=$( awk '/\*/ {print $1}' <(xrandr) | head -n1; ); } &>/dev/null
    for i in ${!available_resos[@]}; do [[ ${available_resos[$i]} == $reso ]] && {  reson=$reso; pos=$i; }; done 
    [[ $reson != "" ]] && { export resonset=1;  markwarn $"Automatically setting resolution to $reso" ; }

		xhost &>/dev/null || { 
			error $"Access control for X is disabled for root. Please, as your normal user execute 'xhost +`whoami`' and press enter to continue"; 
		} 
	
	while true; do 
		[[ $autoset == 1 ]] && { set_resolution_data 2; return; }
		if [[ ${resonset} == "" ]]; then mkmenu $"Set resolution" ${available_resos[@]}; pos=$choice; fi
	    set_resolution_data $pos
		[[ $TLX == "" ]] && { $clear; error $"Unknown resolution, try again"; } || break;
	done
}

function set_resolution_data(){
while true; do
    case $1 in
		1  ) TLX="83";TLY="11";TRX="60";TRY="18";BLX="75";BLY="18";
               BRX="27";BRY="17";bLX="100";bLY="30";bRX="54";bRY="25"; setterminal; break;;
		2 ) TLX="90";TLY="11";TRX="60";TRY="18";BLX="78";BLY="26";
               BRX="52";BRY="15";bLX="130";bLY="30";bRX="78";bRY="25"; setterminal; break;;
		3 ) TLX="92";TLY="11";TRX="68";TRY="25";BLX="78";BLY="26";
               BRX="52";BRY="15";bLX="92" ;bLY="39";bRX="78";bRY="24"; setterminal; break;;
		4 ) TLX="92";TLY="14";TRX="68";TRY="25";BLX="92";BLY="36";
               BRX="74";BRY="20";bLX="100";bLY="52";bRX="54";bRY="25"; setterminal; break;;
		5 ) TLX="92";TLY="14";TRX="68";TRY="25";BLX="92";BLY="36";
               BRX="74";BRY="20";bLX="100";bLY="52";bRX="54";bRY="25"; setterminal; break;;
    	6 ) TLX="100";TLY="20";TRX="109";TRY="20";BLX="100";BLY="30";
               BRX="109";BRY="20";bLX="100";bLY="52";bRX="109";bRY="30"; setterminal; break;;
		7 ) TLX="100";TLY="20";TRX="109";TRY="20";BLX="100";BLY="30";
               BRX="109";BRY="20";bLX="100";bLY="52";bRX="109";bRY="30"; setterminal; break;;
		8 ) TLX="110";TLY="35";TRX="99";TRY="40";BLX="110";BLY="35";
               BRX="99";BRY="30";bLX="110";bLY="72";bRX="99";bRY="40"; setterminal; break;;
		9 ) TLX="110";TLY="35";TRX="99";TRY="40";BLX="110";BLY="35";
               BRX="99";BRY="30";bLX="110";bLY="72";bRX="99";bRY="40"; setterminal; break;;
		10) TLX="130";TLY="40";TRX="68";TRY="25";BLX="130";BLY="40";
               BRX="132";BRY="35";bLX="130";bLY="85";bRX="132";bRY="48"; setterminal; break;;
		* ) break;;  
		esac
done
    }

getterminal(){
    if [ -x $TERMBIN/$TERMINAL ]; then
	    _debug "Using configured terminal"
	else
		_debug "$TERMINAL was not used, not found on path"
		_debug "Using default terminal"
			TERMINAL=`ls -l1 /etc/alternatives/x-terminal-emulator|cut -d ">" -f 2|cut -d " " -f 2|head -n1` &>/dev/null;
	fi

	if [ -x "$TERMBIN/$TERMINAL" ] || [ -x "/usr/bin/$TERMINAL" ] || [ -x "/usr/sbin/$TERMINAL" ]; then D="1" 
	else
        if [ -e $TERM ]; then 
		    _debug "Using environment defined terminal ($TERM)"
			TERMINAL=$TERM
		else
            if [ -x "$TERMBIN/xterm" ]; then
				TERMINAL="xterm" && _debug "Using Xterm"
			else
			echo $"I cant find any good terminal, please set one on your conffile
				Your TERMINAL var contains no valid temrinal
				Your alternative against x-terminal-emulator contains no terminal
				Xterm can\'t be found on your system, Maybe not in /usr/bin?"
				exit
			fi
		fi     
	fi
}


function setterminal {
	getterminal
	_debug "Im going to set terminal options for your terminal now"

    case $TERMINAL in 
		xterm|uxterm ) 
			TOPLEFT="-geometry $TLX*$TLY+0+0 "
			TOPRIGHT="-geometry $TRX*$TRY-0+0 "
			BOTTOMLEFT="-geometry $BLX*$BLY+0-0 "
			BOTTOMRIGHT="-geometry $BRX*$BRY-0-0 "
			TOPLEFTBIG="-geometry $bLX*$bLY+0+0 "
			TOPRIGHTBIG="-geometry $bLX*$bLY+0-0 "
			HOLDFLAG="-hold"
			TITLEFLAG="-T"
			FGC="-fg"
			BGC="-bg"
			EXECFLAG="-e"
            ;;
		gnome-terminal|gnome-terminal.wrapper ) 
			TOPLEFT="-geometry=$TLX*$TLY+0+0 "
			TOPRIGHT="-geometry=$TRX*$TRY-0+0 "
			BOTTOMLEFT="-geometry=$BLX*$BLY+0-0 "
			BOTTOMRIGHT="-geometry=$BRX*$BRY-0-0 "
			TOPLEFTBIG="-geometry=$bLX*$bLY+0+0 "
			TOPRIGHTBIG="-geometry=$bLX*$bLY+0-0 "
			EXECFLAG="-e "
			HOLDFLAG="" 
			TITLEFLAG="-t"
			FGC=""
			DUMPING_COLOR=""
			INJECTION_COLOR=""
			ASSOCIATION_COLOR=""
			DEAUTH_COLOR=""
			BACKGROUND_COLOR=""
			BGC=""
            ;;
	esac
    [[ "$debug" = "1" ]] && echo $TOPLEFT \
				$TOPRIGHT \
				$BOTTOMLEFT \
				$BOTTOMRIGHT \
				$TOPLEFTBIG \
				$TOPRIGHTBIG \
				$HOLDFLAG\
				$TITLEFLAG\
				$FGC\
			    $BGC\
				printf -- "$EXECFLAG \n"
}

# Resolution stuff
    available_resos=( 640x480 800x480 800x600 1024x600 1024x768 1280x768 1280x800 1280x1024 1440x900 1600x1200 )
	[[ ! $dont_check_x ]] && resolution
	export termargs="$TOPLEFTBIG $BGC $BACKGROUND_COLOR $FGC $DUMPING_COLOR $EXECFLAG ";
