Fixing check for terminal width
[grml-etc-core.git] / etc / grml / lsb-functions
1 # lsb init-functions {{{
2 # /lib/lsb/init-functions for Debian -*- shell-script -*-
3 #
4 # Copyright (c) 2002-03 Chris Lawrence
5 # All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
9 # are met:
10 # 1. Redistributions of source code must retain the above copyright
11 #    notice, this list of conditions and the following disclaimer.
12 # 2. Redistributions in binary form must reproduce the above copyright
13 #    notice, this list of conditions and the following disclaimer in the
14 #    documentation and/or other materials provided with the distribution.
15 # 3. Neither the name of the author nor the names of other contributors
16 #    may be used to endorse or promote products derived from this software
17 #    without specific prior written permission.
18 #
19 # THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 # ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 # SUCH DAMAGE.
30
31 log_success_msg () {
32     echo " * $@"
33 }
34
35 log_failure_msg () {
36     TPUT=/usr/bin/tput
37     if [ -x $TPUT ] && $TPUT hpa 60 >/dev/null 2>&1; then
38         RED=`$TPUT setaf 1`
39         #NORMAL=`$TPUT op`
40         echo " ${RED}*${NORMAL} $@"
41     else
42         echo " * $@"
43     fi
44 }
45
46 log_warning_msg () {
47     TPUT=/usr/bin/tput
48     if [ -x $TPUT ] && $TPUT hpa 60 >/dev/null 2>&1; then
49         YELLOW=`$TPUT setaf 3`
50         #NORMAL=`$TPUT op`
51         # echo " *${NORMAL} $@"
52         echo " ${BLUE}*${NORMAL} $@"
53     else
54         echo " * $@"
55     fi
56 }
57
58 log_warning_msg_nn () {
59     TPUT=/usr/bin/tput
60     if [ -x $TPUT ] && $TPUT hpa 60 >/dev/null 2>&1; then
61         YELLOW=`$TPUT setaf 3`
62         echo -n " ${BLUE}*${NORMAL} $@"
63     else
64         echo -n " * $@"
65     fi
66 }
67
68 # int log_begin_message (char *message)
69 log_begin_msg () {
70         if [ -z "$1" ]; then
71                 return 1
72         fi
73         echo " ${GREEN}*${NORMAL} $@"
74 }
75
76 log_begin_msg_nn () {
77         if [ -z "$1" ]; then
78                 return 1
79         fi
80         echo -n " ${GREEN}*${NORMAL} $@"
81 }
82
83
84 SUBMSG="   ${GREEN}-${NORMAL} "
85   
86 # int log_end_message (int exitstatus)
87 log_end_msg () {
88
89     # If no arguments were passed, return
90     [ -z "$1" ] && return 1
91
92     # Only do the fancy stuff if we have an appropriate terminal
93     # and if /usr is already mounted
94     TPUT=/usr/bin/tput
95     EXPR=/usr/bin/expr
96     if [ -x $TPUT ] && [ -x $EXPR ] && $TPUT hpa 60 >/dev/null 2>&1; then
97         COLS=`$TPUT cols`
98         if [ -n "$COLS" ]; then
99             COL=`$EXPR $COLS - 7`
100         else
101             COL=73
102         fi
103         UP=`$TPUT cuu1`
104         END=`$TPUT hpa $COL`
105         START=`$TPUT hpa 0`
106         #RED=`$TPUT setaf 1`
107         #NORMAL=`$TPUT op`
108         if [ $1 -eq 0 ]; then
109             echo "$UP$END${BLUE}[ ${GREEN}ok ${BLUE}]${NORMAL}"
110         else
111             echo -e "${UP}${START} ${RED}*${NORMAL}${END}[${RED}fail${NORMAL}]"
112         fi
113     else
114         if [ $1 -eq 0 ]; then
115             echo "   ...done."
116         else
117             echo "   ...fail!"
118         fi
119     fi
120     return $1
121 }
122
123 # void esyslog(char* priority, char* tag, char* message)
124 #    use the system logger to log a message
125 #esyslog() {
126 #        local pri=
127 #        local tag=
128 #
129 #        if [ -x /usr/bin/logger ]
130 #        then
131 #                pri="$1"
132 #                tag="$2"
133 #
134 #                shift 2
135 #                [[ -z "$*" ]] && return 0
136 #
137 #                /usr/bin/logger -p "${pri}" -t "${tag}" -- "$*"
138 #        fi
139 #
140 #        return 0
141 #}
142
143 # Copyright 1999-2005 Gentoo Foundation
144 # Distributed under the terms of the GNU General Public License v2
145 # $Header: /var/cvsroot/gentoo-src/rc-scripts/sbin/functions.sh,v 1.81.2.6 2005/05/15 20:00:31 vapier Exp $
146
147 RC_GOT_FUNCTIONS="yes"
148
149 # Different types of dependencies
150 deptypes="need use"
151 # Different types of order deps
152 ordtypes="before after"
153
154 #
155 # Internal variables
156 #
157
158 # Dont output to stdout?
159 RC_QUIET_STDOUT="no"
160 RC_VERBOSE="${RC_VERBOSE:-no}"
161
162 # Should we use color?
163 if [ -r /proc/cmdline ] ; then
164    grep -q ' nocolor' /proc/cmdline && RC_NOCOLOR='yes'
165 fi
166 RC_NOCOLOR="${RC_NOCOLOR:-no}"
167 # Can the terminal handle endcols?
168 RC_ENDCOL="yes"
169
170 #
171 # Default values for rc system
172 #
173 RC_TTY_NUMBER=11
174 RC_NET_STRICT_CHECKING="no"
175 RC_PARALLEL_STARTUP="no"
176 RC_USE_CONFIG_PROFILE="yes"
177
178 #
179 # Default values for e-message indentation and dots
180 #
181 RC_INDENTATION=''
182 RC_DEFAULT_INDENT=2
183 #RC_DOT_PATTERN=' .'
184 RC_DOT_PATTERN=''
185
186 # void splash(...)
187 #
188 #  Notify bootsplash/splashutils/gensplash/whatever about
189 #  important events.
190 #
191 splash() {
192         return 0
193 }
194
195 # void profiling(...)
196 #
197 #  Notify bootsplash/whatever about important events.
198 #
199 profiling() {
200         return 0
201 }
202
203 # void get_bootconfig()
204 #
205 #    Get the BOOTLEVEL and SOFTLEVEL by setting
206 #    'bootlevel' and 'softlevel' via kernel
207 #    parameters.
208 #
209 get_bootconfig() {
210         local copt=
211         local newbootlevel=
212         local newsoftlevel=
213
214         for copt in $(</proc/cmdline) ; do
215                 case "${copt%=*}" in
216                         "bootlevel")
217                                 newbootlevel="${copt##*=}"
218                                 ;;
219                         "softlevel")
220                                 newsoftlevel="${copt##*=}"
221                                 ;;
222                 esac
223         done
224
225         if [ -n "${newbootlevel}" ] ; then
226                 export BOOTLEVEL="${newbootlevel}"
227         else
228                 export BOOTLEVEL="boot"
229         fi
230
231         if [ -n "${newsoftlevel}" ] ; then
232                 export DEFAULTLEVEL="${newsoftlevel}"
233         else
234                 export DEFAULTLEVEL="default"
235         fi
236
237         return 0
238 }
239
240 # void get_libdir(void)
241 #
242 #    prints the current libdir {lib,lib32,lib64}
243 #
244 get_libdir() {
245         if [ -n "${CONF_LIBDIR_OVERRIDE}" ] ; then
246                 CONF_LIBDIR="${CONF_LIBDIR_OVERRIDE}"
247         elif [ -x "/usr/bin/portageq" ] ; then
248                 CONF_LIBDIR="$(/usr/bin/portageq envvar CONF_LIBDIR)"
249         fi
250         echo ${CONF_LIBDIR:=lib}
251 }
252
253 # void esyslog(char* priority, char* tag, char* message)
254 #
255 #    use the system logger to log a message
256 #
257 esyslog() {
258         local pri=
259         local tag=
260
261         if [ -x /usr/bin/logger ]
262         then
263                 pri="$1"
264                 tag="$2"
265
266                 shift 2
267                 [ -z "$*" ] && return 0
268
269                 /usr/bin/logger -p "${pri}" -t "${tag}" -- "$*"
270         fi
271
272         return 0
273 }
274
275 # void eindent(int num)
276 #
277 #    increase the indent used for e-commands.
278 #
279 eindent() {
280         local i=$1
281         (( i > 0 )) || (( i = RC_DEFAULT_INDENT ))
282         esetdent $(( ${#RC_INDENTATION} + i ))
283 }
284
285 # void eoutdent(int num)
286 #
287 #    decrease the indent used for e-commands.
288 #
289 eoutdent() {
290         local i=$1
291         (( i > 0 )) || (( i = RC_DEFAULT_INDENT ))
292         esetdent $(( ${#RC_INDENTATION} - i ))
293 }
294
295 # void esetdent(int num)
296 #
297 #    hard set the indent used for e-commands.
298 #    num defaults to 0
299 #
300 esetdent() {
301         local i=$1
302         (( i < 0 )) && (( i = 0 ))
303         RC_INDENTATION=$(printf "%${i}s" '')
304 }
305
306 # void einfo(char* message)
307 #
308 #    show an informative message (with a newline)
309 #
310 einfo() {
311         einfon "$*\n"
312         LAST_E_CMD=einfo
313         return 0
314 }
315
316 # void einfon(char* message)
317 #
318 #    show an informative message (without a newline)
319 #
320 einfon() {
321         [ "${RC_QUIET_STDOUT}" = "yes" ] && return 0
322         [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
323         echo -ne " ${GOOD}*${NORMAL} ${RC_INDENTATION}$*"
324         LAST_E_CMD=einfon
325         return 0
326 }
327
328 # void ewarn(char* message)
329 #
330 #    show a warning message + log it
331 #
332 ewarn() {
333         if [ "${RC_QUIET_STDOUT}" = "yes" ]; then
334                 echo " $*"
335         else
336                 [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
337                 echo -e " ${WARN}*${NORMAL} ${RC_INDENTATION}$*"
338         fi
339
340         # Log warnings to system log
341         esyslog "daemon.warning" "rc-scripts" "$*"
342
343         LAST_E_CMD=ewarn
344         return 0
345 }
346
347 # void eerror(char* message)
348 #
349 #    show an error message + log it
350 #
351 eerror() {
352         if [ "${RC_QUIET_STDOUT}" = "yes" ]; then
353                 echo " $*" >/dev/stderr
354         else
355                 [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
356                 echo -e " ${BAD}*${NORMAL} ${RC_INDENTATION}$*"
357         fi
358
359         # Log errors to system log
360         esyslog "daemon.err" "rc-scripts" "$*"
361
362         LAST_E_CMD=eerror
363         return 0
364 }
365
366 # void ebegin(char* message)
367 #
368 #    show a message indicating the start of a process
369 #
370 ebegin() {
371         local msg="$*" dots spaces
372         spaces="$(printf '%'"${#RC_DOT_PATTERN}"'s' '')"
373         [ "${RC_QUIET_STDOUT}" = "yes" ] && return 0
374
375         if [ -n "${RC_DOT_PATTERN}" ]; then
376                 dots=$(printf "%$(( COLS - 3 - ${#RC_INDENTATION} - ${#msg} - 7 ))s" '')
377                 while [ "${dots#${spaces}}" != "${dots}" ] ; do
378                         dots="${dots#${spaces}}${RC_DOT_PATTERN}"
379                 done
380                 msg="${msg}${dots}"
381         else
382                 msg="${msg} ..."
383         fi
384         einfon "${msg}"
385         [ "${RC_ENDCOL}" = "yes" ] && echo
386
387         LAST_E_LEN=$(( 3 + ${#RC_INDENTATION} + ${#msg} ))
388         LAST_E_CMD=ebegin
389         return 0
390 }
391
392 # void _eend(int error, char *efunc, char* errstr)
393 #
394 #    indicate the completion of process, called from eend/ewend
395 #    if error, show errstr via efunc
396 #
397 #    This function is private to functions.sh.  Do not call it from a
398 #    script.
399 #
400 _eend() {
401         local retval=${1:-0} efunc=${2:-eerror} msg
402         shift 2
403
404         if [ "${retval}" -eq 0 ]; then
405                 [ "${RC_QUIET_STDOUT}" = "yes" ] && return 0
406                 msg="${BRACKET}[ ${GOOD}ok${BRACKET} ]${NORMAL}"
407         else
408                 if [ -n "$*" ]; then
409                         "${efunc}" "$*"
410                 fi
411                 msg="${BRACKET}[ ${BAD}!!${BRACKET} ]${NORMAL}"
412         fi
413
414         if [ "${RC_ENDCOL}" = "yes" ]; then
415                 echo -e "${ENDCOL}  ${msg}"
416         else
417                 [ "${LAST_E_CMD}" = "ebegin" ] || LAST_E_LEN=0
418                 printf "%$(( COLS - LAST_E_LEN - 6 ))s%b\n" '' "${msg}"
419         fi
420
421         return ${retval}
422 }
423
424 # void eend(int error, char* errstr)
425 #
426 #    indicate the completion of process
427 #    if error, show errstr via eerror
428 #
429 eend() {
430         local retval=${1:-0}
431         shift
432
433         _eend ${retval} eerror "$*"
434
435         LAST_E_CMD=eend
436         return $retval
437 }
438
439 # void ewend(int error, char* errstr)
440 #
441 #    indicate the completion of process
442 #    if error, show errstr via ewarn
443 #
444 ewend() {
445         local retval=${1:-0}
446         shift
447
448         _eend ${retval} ewarn "$*"
449
450         LAST_E_CMD=ewend
451         return $retval
452 }
453
454 # v-e-commands honor RC_VERBOSE which defaults to no.
455 # The condition is negated so the return value will be zero.
456 veinfo() { [ "${RC_VERBOSE}" != yes ] || einfo "$@"; }
457 veinfon() { [ "${RC_VERBOSE}" != yes ] || einfon "$@"; }
458 vewarn() { [ "${RC_VERBOSE}" != yes ] || ewarn "$@"; }
459 veerror() { [ "${RC_VERBOSE}" != yes ] || eerror "$@"; }
460 vebegin() { [ "${RC_VERBOSE}" != yes ] || ebegin "$@"; }
461 veend() {
462         [ "${RC_VERBOSE}" = "yes" ] && { eend "$@"; return $?; }
463         return ${1:-0}
464 }
465 veend() {
466         [ "${RC_VERBOSE}" = "yes" ] && { ewend "$@"; return $?; }
467         return ${1:-0}
468 }
469
470 # char *KV_major(string)
471 #
472 #    Return the Major (X of X.Y.Z) kernel version
473 #
474 KV_major() {
475         [ -z "$1" ] && return 1
476
477         local KV=$@
478         echo "${KV%%.*}"
479 }
480
481 # char *KV_minor(string)
482 #
483 #    Return the Minor (Y of X.Y.Z) kernel version
484 #
485 KV_minor() {
486         [ -z "$1" ] && return 1
487
488         local KV="$@"
489         KV="${KV#*.}"
490         echo "${KV%%.*}"
491 }
492
493 # char *KV_micro(string)
494 #
495 #    Return the Micro (Z of X.Y.Z) kernel version.
496 #
497 KV_micro() {
498         [ -z "$1" ] && return 1
499
500         local KV="$@"
501         KV="${KV#*.*.}"
502         echo "${KV%%[^[:digit:]]*}"
503 }
504
505 # int KV_to_int(string)
506 #
507 #    Convert a string type kernel version (2.4.0) to an int (132096)
508 #    for easy compairing or versions ...
509 #
510 KV_to_int() {
511         [ -z "$1" ] && return 1
512
513         local KV_MAJOR="$(KV_major "$1")"
514         local KV_MINOR="$(KV_minor "$1")"
515         local KV_MICRO="$(KV_micro "$1")"
516         local KV_int=$(( KV_MAJOR * 65536 + KV_MINOR * 256 + KV_MICRO ))
517
518         # We make version 2.2.0 the minimum version we will handle as
519         # a sanity check ... if its less, we fail ...
520         if [ "${KV_int}" -ge 131584 ] ; then
521                 echo "${KV_int}"
522                 return 0
523         fi
524
525         return 1
526 }
527
528 # int get_KV()
529 #
530 #    Return the kernel version (major, minor and micro concated) as an integer.
531 #    Assumes X and Y of X.Y.Z are numbers.  Also assumes that some leading
532 #    portion of Z is a number.
533 #    e.g. 2.4.25, 2.6.10, 2.6.4-rc3, 2.2.40-poop, 2.0.15+foo
534 #
535 get_KV() {
536         local KV="$(uname -r)"
537
538         echo "$(KV_to_int "${KV}")"
539
540         return $?
541 }
542
543 # bool get_bootparam(param)
544 #
545 #   return 0 if gentoo=param was passed to the kernel
546 #
547 #   EXAMPLE:  if get_bootparam "nodevfs" ; then ....
548 #
549 get_bootparam() {
550         local x copt params retval=1
551
552         [ ! -r "/proc/cmdline" ] && return 1
553
554         for copt in $(< /proc/cmdline)
555         do
556                 if [ "${copt%=*}" = "gentoo" ]
557                 then
558                         params="$(gawk -v PARAMS="${copt##*=}" '
559                                 BEGIN {
560                                         split(PARAMS, nodes, ",")
561                                         for (x in nodes)
562                                                 print nodes[x]
563                                 }')"
564
565                         # Parse gentoo option
566                         for x in ${params}
567                         do
568                                 if [ "${x}" = "$1" ]
569                                 then
570 #                                       echo "YES"
571                                         retval=0
572                                 fi
573                         done
574                 fi
575         done
576
577         return ${retval}
578 }
579
580 # Safer way to list the contents of a directory,
581 # as it do not have the "empty dir bug".
582 #
583 # char *dolisting(param)
584 #
585 #    print a list of the directory contents
586 #
587 #    NOTE: quote the params if they contain globs.
588 #          also, error checking is not that extensive ...
589 #
590 dolisting() {
591         local x=
592         local y=
593         local tmpstr=
594         local mylist=
595         local mypath="$*"
596
597         if [ "${mypath%/\*}" != "${mypath}" ]
598         then
599                 mypath="${mypath%/\*}"
600         fi
601
602         for x in ${mypath}
603         do
604                 [ ! -e "${x}" ] && continue
605
606                 if [ ! -d "${x}" ] && ( [ -L "${x}" -o -f "${x}" ] )
607                 then
608                         mylist="${mylist} $(ls "${x}" 2> /dev/null)"
609                 else
610                         [ "${x%/}" != "${x}" ] && x="${x%/}"
611
612                         cd "${x}"; tmpstr="$(ls)"
613
614                         for y in ${tmpstr}
615                         do
616                                 mylist="${mylist} ${x}/${y}"
617                         done
618                 fi
619         done
620
621         echo "${mylist}"
622 }
623
624 # char *add_suffix(char * configfile)
625 #
626 #    Returns a config file name with the softlevel suffix
627 #    appended to it.  For use with multi-config services.
628 add_suffix() {
629         if [ "${RC_USE_CONFIG_PROFILE}" = "yes" -a -e "$1.${DEFAULTLEVEL}" ]
630         then
631                 echo "$1.${DEFAULTLEVEL}"
632         else
633                 echo "$1"
634         fi
635
636         return 0
637 }
638
639 # Network filesystems list for common use in rc-scripts.
640 # This variable is used in is_net_fs and other places such as
641 # localmount.
642 NET_FS_LIST="afs cifs coda gfs ncpfs nfs nfs4 shfs smbfs"
643
644 # bool is_net_fs(path)
645 #
646 #   return 0 if path is the mountpoint of a networked filesystem
647 #
648 #   EXAMPLE:  if is_net_fs / ; then ...
649 #
650 is_net_fs() {
651         local fstype
652         # /proc/mounts is always accurate but may not always be available
653         if [ -e /proc/mounts ]; then
654                 fstype="$( sed -n -e '/^rootfs/!s:.* '"$1"' \([^ ]*\).*:\1:p' /proc/mounts )"
655         else
656                 fstype="$( mount | sed -n -e 's:.* on '"$1"' type \([^ ]*\).*:\1:p' )"
657         fi
658         case " ${NET_FS_LIST} " in
659                 *" ${fstype} "*)
660                         return 0
661                         ;;
662                 *)
663                         return 1
664                         ;;
665         esac
666 }
667
668 # bool is_uml_sys()
669 #
670 #   return 0 if the currently running system is User Mode Linux
671 #
672 #   EXAMPLE:  if is_uml_sys ; then ...
673 #
674 is_uml_sys() {
675         grep -qs 'UML' /proc/cpuinfo
676         return $?
677 }
678
679 # bool is_vserver_sys()
680 #
681 #   return 0 if the currently running system is a Linux VServer
682 #
683 #   EXAMPLE:  if is_vserver_sys ; then ...
684 #
685 is_vserver_sys() {
686         grep -qs '^s_context:[[:space:]]*[1-9]' /proc/self/status
687         return $?
688 }
689
690 # bool get_mount_fstab(path)
691 #
692 #   return the parameters to pass to the mount command generated from fstab
693 #
694 #   EXAMPLE: cmd=$( get_mount_fstab /proc )
695 #            cmd=${cmd:--t proc none /proc}
696 #            mount -n ${cmd}
697 #
698 get_mount_fstab() {
699         awk '$1 ~ "^#" { next }
700              $2 == "'$*'" { if (found++ == 0) { print "-t "$3,"-o "$4,$1,$2 } }
701              END { if (found > 1) { print "More than one entry for '$*' found in /etc/fstab!" > "/dev/stderr" } }
702         ' /etc/fstab
703 }
704
705 # char *reverse_list(list)
706 #
707 #   Returns the reversed order of list
708 #
709 reverse_list() {
710         local ret
711         ret=''
712         while [ "$#" -gt 0 ] ; do
713                 if [ -z "${ret}" ] ; then
714                         ret="$1"
715                 else
716                         ret="$1 ${ret}"
717                 fi
718                 shift
719         done
720         printf '%s' "${ret}"
721 }
722
723
724 # bool is_older_than(reference, files/dirs to check)
725 #
726 #   return 0 if any of the files/dirs are newer than
727 #   the reference file
728 #
729 #   EXAMPLE: if is_older_than a.out *.o ; then ...
730 is_older_than() {
731         local x=
732         local ref="$1"
733         shift
734
735         for x in "$@" ; do
736                 [ "${x}" -nt "${ref}" ] && return 0
737
738                 if [ -d "${x}" ] ; then
739                         is_older_than "${ref}" "${x}"/* && return 0
740                 fi
741         done
742
743         return 1
744 }
745
746 if [ -z "${EBUILD}" ] ; then
747         # Setup a basic $PATH.  Just add system default to existing.
748         # This should solve both /sbin and /usr/sbin not present when
749         # doing 'su -c foo', or for something like:  PATH= rcscript start
750         PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/sbin:${PATH}"
751
752         if [ "$(/sbin/consoletype 2> /dev/null)" = "serial" ] ; then
753                 # We do not want colors/endcols on serial terminals
754                 RC_NOCOLOR="yes"
755                 RC_ENDCOL="no"
756         fi
757
758         for arg in "$@" ; do
759                 case "${arg}" in
760                         # Lastly check if the user disabled it with --nocolor argument
761                         --nocolor|-nc)
762                                 RC_NOCOLOR="yes"
763                                 ;;
764                 esac
765         done
766
767 else
768         # Should we use colors ?
769         case "$*" in
770                 *depend*)
771                         # Check user pref in portage
772                         RC_NOCOLOR="$(portageq envvar NOCOLOR 2>/dev/null)"
773                         [ "${RC_NOCOLOR}" = "true" ] && RC_NOCOLOR="yes"
774                         ;;
775                 *)
776                 # We do not want colors during emerge depend
777                 RC_NOCOLOR="yes"
778                 # No output is seen during emerge depend, so this is not needed.
779                 RC_ENDCOL="no"
780                 ;;
781         esac
782 fi
783
784 COLS=0
785
786 case "$*" in
787         *depend*)
788         # We do not want stty to run during emerge depend
789         [ -n "${EBUILD}" ] && COLS=80
790 esac
791
792 # Setup COLS and ENDCOL so eend can line up the [ ok ]
793 # width of [ ok ] == 7
794 if [ "${COLS}" -eq 0 ] ; then
795         COLS="$(stty size 2>/dev/null | cut -d' ' -f2)"
796         if [ -z "${COLS}" ] || [ "${COLS}" -gt 0 ] ; then
797                 COLS=80
798         fi
799 fi
800
801 if [ "${RC_ENDCOL}" = "yes" ]; then
802         ENDCOL="\e[A\e[$(( ${COLS} - 8 ))G"
803 else
804         ENDCOL=''
805 fi
806
807 # Setup the colors so our messages all look pretty
808 if [ "${RC_NOCOLOR}" = "yes" ]; then
809         unset GOOD WARN BAD NORMAL HILITE BRACKET
810 else
811         GOOD='\e[32;01m'
812         WARN='\e[33;01m'
813         BAD='\e[31;01m'
814         NORMAL='\e[0m'
815         HILITE='\e[36;01m'
816         BRACKET='\e[34;01m'
817 fi
818
819 # vim:ts=4
820 # }}}