#!/usr/bin/env bash
# irc -- attach to my tmux session with the IRC client

. lib.bash || exit

usage() {
	echo "Usage: $progname [-f] [-m] [-s]"
	echo
	echo_opt "-f"	"allow duplicate attach"
	echo_opt "-m"	"use Mosh"
	echo_opt "-s"	"use SSH"
}

IRC_HOST=star

session=${XDG_SESSION_ID:-${DISPLAY:-${WAYLAND_DISPLAY:-none}}}
lockfile=${XDG_RUNTIME_DIR?}/irc-$session.lock

opt_mosh=0
opt_force=0

while getopts :fms OPT; do
	case $OPT in
	f) opt_force=1;;
	m) opt_mosh=1;;
	s) opt_mosh=0;;
	*) lib:die_getopts;;
	esac
done; shift $((OPTIND-1))

exec {fd}<>"$lockfile"
if (( !opt_force )); then
	flock -xn $fd || vdie "already connected from $HOSTNAME/$session"
fi

if (( opt_mosh )); then
	export MOSH_TITLE_NOPREFIX=y
	mosh "$IRC_HOST" -- tmux attach -t irc; r=$?
else
	ssh -t "$IRC_HOST" "tmux attach -t irc"; r=$?
fi

exec {fd}>&-
hi &> /dev/null &
exit $r
