1#!/bin/sh -e
2#
3#    custom: run the user's custom status scripts
4#
5#    Copyright (C) 2009 Canonical Ltd.
6#    Copyright (C) 2011-2014 Dustin Kirkland
7#
8#    Authors: Dustin Kirkland <kirkland@byobu.org>
9#
10#    This program is free software: you can redistribute it and/or modify
11#    it under the terms of the GNU General Public License as published by
12#    the Free Software Foundation, version 3 of the License.
13#
14#    This program is distributed in the hope that it will be useful,
15#    but WITHOUT ANY WARRANTY; without even the implied warranty of
16#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17#    GNU General Public License for more details.
18#
19#    You should have received a copy of the GNU General Public License
20#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
22__custom_detail() {
23	return
24}
25
26__custom() {
27	local cache="$BYOBU_RUN_DIR/cache.$BYOBU_BACKEND/custom"
28	local i= output=
29	for i in "$BYOBU_CONFIG_DIR/bin/"[0-9]*_*; do
30		# Loop over custom scripts
31		# Ensure executable
32		[ -x "$i" ] || continue
33		# Ignore vim backup files ending in ~
34		case "$i" in
35			*~) continue ;;
36		esac
37		local now="$(date +%s)"
38		local script=${i##*/}
39		local freq=${script%%_*}
40		freq=${freq#0}
41		local lastrun=
42		[ -r "$cache.$script.last" ] && read lastrun < "$cache.$script.last" || lastrun=0
43		local expiration=$(($lastrun+$freq))
44		if [ $now -ge $expiration ]; then
45			"$i" > "$cache.$script" 2>/dev/null
46			printf "%s\n" "${now}" > "$cache.$script.last"
47		fi
48		readfile < "$cache.$script"
49		local str="${_RET}"
50		case "$str" in
51			*"$ESC{"*)
52				# User has formatted the colors
53				output="$output$(printf "$str")"
54			;;
55			*)
56				# Default coloring
57				output="$output$str "
58			;;
59		esac
60	done
61	if [ -z "$output" ]; then
62		rm -f "$BYOBU_RUN_DIR/status.$BYOBU_BACKEND/custom"*
63		return
64	fi
65	printf "$output" | $BYOBU_SED ':a;N;$!ba;s/\n//g'
66}
67
68# vi: syntax=sh ts=4 noexpandtab
69