#!/bin/sh
# ttym on_start -- runs when a session begins.
#
#   $1  mode      countdown | stopwatch
#   $2  seconds   requested duration for a countdown, 0 for a stopwatch
#   $3  comment   the -c text, or an empty string
#
# stdin, stdout and stderr are /dev/null: printing here goes nowhere and
# cannot corrupt the redrawn progress line. timer waits for this to exit,
# so keep it quick or background the slow part with &.

mode=$1 seconds=$2 comment=$3

# Name the terminal window after what is running.
case $TERM in
xterm*|rxvt*|tmux*|screen*|alacritty|foot*)
	if [ "$mode" = countdown ]; then
		printf '\033]0;timer %sm %s\007' "$((seconds / 60))" "$comment" > /dev/tty
	else
		printf '\033]0;stopwatch %s\007' "$comment" > /dev/tty
	fi
	;;
esac

# Pause music while a countdown runs (uncomment to use):
# [ "$mode" = countdown ] && playerctl pause 2>/dev/null
