#!/usr/bin/env bash
#
# wallstreet: fill your console with Wall Street-like news and stats
#
# Copyright 2014-2026 Dustin Kirkland <dustin.kirkland@gmail.com>
# macOS port and new data sources 2026.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

PKG="wallstreet"
VERSION="1.7.0"
widget_dir="$(cd "$(dirname "$0")/../lib/$PKG" && pwd)"

case "$1" in
	--demo)
		# Invented quotes and headlines, nothing is fetched
		export WALLSTREET_DEMO=1
	;;
	-V|--version)
		echo "$PKG $VERSION"
		exit 0
	;;
	-h|--help)
		exec man $PKG
	;;
	?*)
		echo "usage: $PKG [--demo] [--version]" >&2
		exit 2
	;;
esac

if [ -z "$TMUX" ]; then
	# Not in a tmux session: start one, with
	# byobu if it's found, else with plain tmux
	if command -v byobu >/dev/null 2>&1; then
		tmux_launcher=byobu
	else
		tmux_launcher=tmux
	fi
	$tmux_launcher new-session -d -s $PKG "bash"
	$tmux_launcher send-keys -t $PKG "WALLSTREET_OWN_SESSION=1 $0 $1; exit"
	$tmux_launcher send-keys -t $PKG Enter
	exec $tmux_launcher attach-session -t $PKG
	exit 1
fi

cleanup() {
	trap - EXIT HUP INT QUIT TERM
	[ -n "$win" ] && tmux kill-window -t "$win" >/dev/null 2>&1
	# Only take the session down if this script created it
	[ -n "$WALLSTREET_OWN_SESSION" ] && tmux kill-session -t $PKG >/dev/null 2>&1
	exit 0
}
trap cleanup EXIT HUP INT QUIT TERM

# Panes take their environment from the tmux server, not from this script
run="env WALLSTREET_DEMO=${WALLSTREET_DEMO:-0}"

# Layout: the quote table on the left, beside it the chart of the selected
# stock above the feed reader and a news picture, three tickers of three rows
# each below.  Narrow terminals get fewer panes.
win=$(tmux new-window -P -F '#{window_id}' -n $PKG "$run $widget_dir/quotes") || exit 1
quotes=$(tmux list-panes -t "$win" -F '#{pane_id}' | head -n1)
width=$(tmux display-message -p -t "$win" '#{window_width}')
# Clicks reach the quote table only with tmux's mouse support.  A session this
# script created gets it; somebody else's session keeps its own setting, the
# arrow keys work either way.
if [ -n "$WALLSTREET_OWN_SESSION" ]; then
	tmux set-option -t $PKG mouse on \; \
		set-option -t $PKG status-style "bg=colour235,fg=colour246" \; \
		set-option -t $PKG status-format[0] "#[fg=colour220,bold] $PKG #[default]#[align=right]click a stock  ·  / search  ·  1-5 span  ·  d remove from watchlist  ·  Ctrl-C quit " \
		>/dev/null 2>&1
fi
tmux split-window -v -l 3 -t "$quotes" "$run $widget_dir/ticker topNews" >/dev/null 2>&1
tmux split-window -v -l 3 -t "$quotes" "$run $widget_dir/ticker -" >/dev/null 2>&1
tmux split-window -v -l 3 -t "$quotes" "$run $widget_dir/ticker +" >/dev/null 2>&1
if [ "$width" -ge 110 ]; then
	chart=$(tmux split-window -h -l $((width - 81)) -P -F '#{pane_id}' -t "$quotes" "$run $widget_dir/chart" 2>/dev/null)
	height=$(tmux display-message -p -t "$quotes" '#{pane_height}')
	news=
	if [ -n "$chart" ] && [ "$height" -ge 24 ]; then
		news=$(tmux split-window -v -l $((height * 45 / 100)) -P -F '#{pane_id}' -t "$chart" "$run $widget_dir/newsboat" 2>/dev/null)
	fi
	if [ -n "$news" ] && [ "$width" -ge 150 ]; then
		tmux split-window -h -l 44 -t "$news" "$run $widget_dir/logo" >/dev/null 2>&1
	fi
	# Open the first feed, so the reader shows headlines instead of a feed list
	[ -n "$news" ] && sleep 3 && tmux send-keys -t "$news" Enter >/dev/null 2>&1
fi
tmux select-pane -t "$quotes" >/dev/null 2>&1

# Stay around until the last widget is gone
while tmux list-panes -t "$win" -F x 2>/dev/null | grep -q x; do
	sleep 1
done
