tl-setup/configure_weechat_session.sh

162 lines
3.9 KiB
Bash
Executable File

#!/bin/sh
# This script is to be run on the remote host in order to create a weechat session
# NOTE: This script only works with tmux, mosh and screen are not supported
# TODO: Cleanup function and robustness
#. util.sh
#weechat_relay_port="9000"
#weechat_relay_password="password1234"
# these are set when running the script
create_tmux_session() {
new_session_name="weechat"
echo "Creating new tmux session..."
while tmux list-sessions | grep -q "^$new_session_name"
do
new_session_name="$new_session_name-"
done
tmux new-session -d -s "$new_session_name"
tmux send -t "$new_session_name":0 'weechat' ENTER
}
attatch_tmux_session() {
choice=""
tmux_sessions=""
session_name=""
window_number=""
echo "Attatching to existing tmux session..."
if tmux list-sessions >/dev/null
then
tmux_sessions="$(tmux list-sessions \
| cut -f 1 -d ' ' \
| rev | cut -c 2- | rev)"
clear
echo "Which tmux session would you like to attach to?"
echo "$tmux_sessions"
read -r choice
if is_substring "$choice" "$tmux_sessions"
then
session_name="$choice"
else
echo "\"$choice\" is not recognized as a valid session, please try again"
attatch_tmux_session
fi
else
echo "No tmux session appears to be running, a new session will be created"
create_tmux_session
exit
fi
window_number="$(( "$(tmux list-sessions | grep "^$session_name" | cut -f 2 -d ' ')" + 1 ))"
tmux new-window -t "$session_name:$window_number"
tmux send -t "$session_name:$window_number" 'weechat' ENTER
echo "Waiting for weechat session..."
while ! ps xo 'cmd=' | grep -q '^weechat'
do
sleep 0.5
done
}
create_weechat_relay(){
echo "Configuring new WeeChat relay..."
if [ -z "$weechat_relay_port" ]
then
echo "weechat_relay_port is not set! exiting..."
exit
fi
if [ -z "$weechat_relay_password" ]
then
echo "weechat_relay_password is not set! exiting..."
exit
fi
weechat_pane_name="WeeChat $(weechat -v)"
weechat_pane_id="$(tmux list-panes -a -F '#{pane_id} #{pane_title}' \
| grep "$weechat_pane_name" \
| cut -f 1 -d ' ')"
if [ "$weechat_pane_id" = " " ]
then
echo "Failed to find WeeChat session, exiting..."
exit
fi
tmux send -t "$weechat_pane_id" '^W'
tmux send -t "$weechat_pane_id" "/relay add weechat $weechat_relay_port" ENTER
tmux send -t "$weechat_pane_id" "/secure set relay $weechat_relay_password" ENTER
tmux send -t "$weechat_pane_id" '/set relay.network.password "${sec.data.relay}"' ENTER
tmux send -t "$weechat_pane_id" "/relay start weechat" ENTER
sleep 1
tmux send -t "$weechat_pane_id" '^Y'
echo "WeeChat relay configured!"
}
configure_weechat_session() {
choice=""
clear
echo "=== WeeChat Relay Setup ==="
echo "Do you enter a password to start WeeChat? (encrypted config)"
echo "1. Yes"
echo "2. No"
while :
do
read -r choice
case "$choice" in
"1")
echo "In this case you will need to start WeeChat manually inside tmux or this script will break. Once you have done this press ENTER"
read
break
;;
"2")
echo "Starting config wizard..."
echo
break
;;
*)
echo "Please enter a valid option"
;;
esac
done
if ! ps xo 'cmd=' | grep -q '^weechat'
then
if ! tmux list-sessions >/dev/null
then
create_tmux_session
else
while :
do
clear
echo "Found existing tmux sessions:"
echo "$(tmux list-sessions)"
echo
echo "tmux is already running, what would you like to do?"
echo "1. Start WeeChat in a new window in an existing tmux session"
echo "2. Start WeeChat in new tmux session"
read -r choice
case "$choice" in
"1")
clear
attatch_tmux_session
create_weechat_relay
break
;;
"2")
clear
create_tmux_session
create_weechat_relay
break
;;
"*")
echo "Please enter a valid option"
;;
esac
done
fi
else
clear
echo "A WeeChat process was found, configuring relay..."
create_weechat_relay
fi
}
configure_weechat_session