1#!/bin/sh
2#
3#    dirs: some dirs needed by all library status scripts
4#
5#    Copyright (C) 2011-2014 Dustin Kirkland
6#
7#    Authors: Dustin Kirkland <kirkland@byobu.org>
8#
9#    This program is free software: you can redistribute it and/or modify
10#    it under the terms of the GNU General Public License as published by
11#    the Free Software Foundation, version 3 of the License.
12#
13#    This program is distributed in the hope that it will be useful,
14#    but WITHOUT ANY WARRANTY; without even the implied warranty of
15#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16#    GNU General Public License for more details.
17#
18#    You should have received a copy of the GNU General Public License
19#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
21PKG="byobu"
22
23# Some users build and install byobu themselves, rather than from a distro
24[ -r "$HOME/.byoburc" ] && . "$HOME/.byoburc"
25[ -n "$BYOBU_PREFIX" ] || BYOBU_PREFIX="@prefix@"
26
27# Create and export the user configuration directory
28if [ -d "$BYOBU_CONFIG_DIR" ]; then
29	export BYOBU_CONFIG_DIR="$BYOBU_CONFIG_DIR"
30elif [ -d "$XDG_CONFIG_HOME" ]; then
31	# Use XDG, as some users insist on such nonsense :-)
32	export BYOBU_CONFIG_DIR="$XDG_CONFIG_HOME/$PKG"
33elif [ -d "$HOME/.config/$PKG" ]; then
34	# Use XDG config directory, if it exists
35	export BYOBU_CONFIG_DIR="$HOME/.config/$PKG"
36elif [ -d "$HOME/.local/share/$PKG" ]; then
37	# Use XDG local directory, if it exists
38	export BYOBU_CONFIG_DIR="$HOME/.local/share/$PKG"
39else
40	# And to default to good old classic config dir location!
41	export BYOBU_CONFIG_DIR="$HOME/.$PKG"
42fi
43[ -d "$BYOBU_CONFIG_DIR" ] || mkdir -p "$BYOBU_CONFIG_DIR/bin"
44
45# Grab the global, then local socket directory
46[ -r "/etc/$PKG/socketdir" ] && . "/etc/$PKG/socketdir"
47[ -r "$BYOBU_CONFIG_DIR/socketdir" ] && . "$BYOBU_CONFIG_DIR/socketdir"
48
49# Create and export the runtime cache directory
50if [ -w /dev/shm ]; then
51	# Use shm for performance, if possible
52	for i in /dev/shm/$PKG-$USER-*; do
53		if [ -d "$i" ] && [ -O "$i" ]; then
54			export BYOBU_RUN_DIR="$i"
55			break
56		fi
57	done
58	# Still empty, make a new one
59	if [ ! -d "$BYOBU_RUN_DIR" ] || [ ! -O "$BYOBU_RUN_DIR" ]; then
60		export BYOBU_RUN_DIR=$(mktemp -d /dev/shm/$PKG-$USER-XXXXXXXX)
61	fi
62fi
63if [ ! -d "$BYOBU_RUN_DIR" ] || [ ! -O "$BYOBU_RUN_DIR" ] || [ ! -w "$BYOBU_RUN_DIR" ]; then
64	# For distros that don't have a /dev/shm, use local disk
65	if [ -d "$XDG_CACHE_HOME" ]; then
66		# Use XDG, as some users insist on such nonsense :-)
67		export BYOBU_RUN_DIR="$XDG_CACHE_HOME/$PKG"
68	else
69		# But if not, we'll use a cache directory
70		export BYOBU_RUN_DIR="$HOME/.cache/$PKG"
71	fi
72fi
73