#!/bin/bash

server_m=/home/jirikalvoda/bin/m
ssh='ssh jirikalvoda@kam.mff.cuni.cz -Y -t'

active_window()
{
	(echo { ;i3-msg -t get_tree | jq | grep focused\":\ true -B1550|  grep \"id\": | tail -n1 ; echo '"a":"b"}') | jq .id
}

if [[ "$1" == "l" ]]
then
	cm -c1  ~/Maildir/.* -od -ob -oo -op -ol1 -of  -os -k -K -Ll2\
		-sduration=6000 -smin-duration=3000 -scolor=orange\
		-m" osdc --to=exec m t lf %s "\
		-a]"osdc --to=exec m t rf %s"\
		-a\\"$0 lf %s"\
		-a="mutt -f %s"\
		-r"to:exec\n:m-repeat-notification\n:%t %n %m%B\n\n" -R"\\\\n<%a> %n %m"
elif [[ "$1" == "" ]]
then
	$ssh $server_m $1
elif [[ "$1" == "h" ]]
then
	$ssh $server_m $1
elif [[ "$1" == "rf" ]]
then
	$ssh $server_m rf $2
elif [[ "$1" == "lf" ]]
then
	file=$2
	file=$(echo ${file:1} | sed 's/[^\/]*\(\/.*\)/\1/')
	file=$(echo ${file:1} | sed 's/[^\/]*\(\/.*\)/\1/')
	file=$(echo ${file:1} | sed 's/[^\/]*\(\/.*\)/\1/')
	file=$(echo ${file:1})
	if [[ "$file" = "." ]]; then file=""; fi
	neomutt -f "notmuch://?query=folder:\"$file\"&limit=500"
	offlineimap-run $file
elif [[ "$1" == "t" ]]
then
	shift
	window="$(active_window)"
	terminal -e $0 $@ &
	for i in {0..10}
	do
		sleep 0.2
		i3-msg "[con_id=\"$window\"] resize set 450 px" 2>/dev/null >/dev/null
	done
fi
