summaryrefslogtreecommitdiff
path: root/functions
blob: b55e507dc5f439f038cb72aa49ae02c9844aad3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/bin/bash
#
# functions
#

# width:

STAT_COL=$(stty size)
# strip the rows number, we just want columns
STAT_COL=${STAT_COL##* }
if [ "$STAT_COL" = "0" ]; then
	# if output was 0 (serial console), set default width to 80
	STAT_COL=80
elif [ ! -t 1 ]; then
	# disable color on output to non-tty
	STAT_COL=80
	USECOLOR=""
fi
# we use 13 characters for our own stuff
STAT_COL=$[$STAT_COL - 13]

# disable colors on broken terminals
TERM_COLORS="$(tput colors 2>/dev/null)"
if [ $? = 3 ]; then
		TERM_COLORS=8
elif [ -n "${TERM_COLORS}" ]; then
	case "${TERM_COLORS}" in
		*[!0-9]*)
			USECOLOR=""
			;;
		*)
			[ "${TERM_COLORS}" -lt 8 ] && USECOLOR=""
			;;
	esac
else
	USECOLOR=""
fi
unset TERM_COLORS

# colors:
if [ "$USECOLOR" = "YES" -o "$USECOLOR" = "yes" ]; then
	C_MAIN="\033[1;37;40m"       # main text
	C_OTHER="\033[1;34;40m"      # prefix & brackets
	C_SEPARATOR="\033[1;30;40m"  # separator
	
	C_BUSY="\033[0;36;40m"		# busy
	C_FAIL="\033[1;31;40m"		# failed
	C_DONE="\033[1;37;40m"		# completed
	C_BKGD="\033[1;35;40m"		# backgrounded
	
	C_H1="\033[1;37;40m"		# highlight text 1
	C_H2="\033[1;36;40m"		# highlight text 2
	
	C_CLEAR="\033[1;0m"
fi

if [ -t 1 ]; then
	SAVE_POSITION="\033[s"
	RESTORE_POSITION="\033[u"
	DEL_TEXT="\033[$(($STAT_COL+4))G"
fi

# prefixes:

PREFIX_REG="::"
PREFIX_HL=" >"

# functions:

deltext() {
	echo -ne "$DEL_TEXT"
}

printhl() {
	echo -e "$C_OTHER$PREFIX_HL $C_H1$1$C_CLEAR "
}

printsep() {
	echo -e "\n$C_SEPARATOR   ------------------------------\n"
}

stat_bkgd() {
	echo -ne "$C_OTHER$PREFIX_REG $C_MAIN$1$C_CLEAR "
	deltext
	echo -ne "   $C_OTHER[${C_BKGD}BKGD$C_OTHER]$C_CLEAR "
}

stat_busy() {
	echo -ne "$C_OTHER$PREFIX_REG $C_MAIN$1$C_CLEAR "
	echo -ne "${SAVE_POSITION}"
	deltext
	echo -ne "   $C_OTHER[${C_BUSY}BUSY$C_OTHER]$C_CLEAR "
}

stat_append() {
	echo -ne "${RESTORE_POSITION}"
	echo -ne "$C_MAIN$1$C_CLEAR"
	echo -ne "${SAVE_POSITION}"
}

stat_done() {
	deltext
	echo -e "   $C_OTHER[${C_DONE}DONE$C_OTHER]$C_CLEAR "
}

stat_fail() {
	deltext
	echo -e "   $C_OTHER[${C_FAIL}FAIL$C_OTHER]$C_CLEAR "
}

stat_die() {
	retval=1
	[ "$1" = "" ] || retval=$1
	stat_fail
	exit $retval
}

status() {
	stat_busy "$1"
	shift
	$* >/dev/null 2>&1
	if [ $? -eq 0 ]; then
		stat_done
		return 0
	else
		stat_fail
		return 1
	fi
}

# daemons:

add_daemon() {
	[ -d /var/run/daemons ] || mkdir -p /var/run/daemons
	touch /var/run/daemons/$1
}

rm_daemon() {
	rm -f /var/run/daemons/$1
}

ck_daemon() {
	[ -f /var/run/daemons/$1 ] && return 1
	return 0
}

source_functions() {
  if [ -d /etc/rc.d/functions.d/ ]; then
    for f in /etc/rc.d/functions.d/*; do
      . $f
    done
  fi
}

# End of file
# vim: set ts=2 noet: