Handle streams separately in tree_add_track()
[cmus.git] / scripts / utils.sh
blob3f03cb579964484940925fb98a28795eb2d75c20
1 #!/bin/sh
3 # Copyright 2005 Timo Hirvonen
5 # This file is licensed under the GPLv2.
7 # initialization {{{
9 LC_ALL=C
10 LANG=C
12 export LC_ALL LANG
14 if test "$CDPATH"
15 then
16 echo "Exporting CDPATH is dangerous and unnecessary!"
17 echo
19 unset CDPATH
21 __cleanup()
23 if test "$DEBUG_CONFIGURE" = y
24 then
25 echo
26 echo "DEBUG_CONFIGURE=y, not removing temporary files"
27 ls .tmp-[0-9]*-*
28 else
29 rm -f .tmp-[0-9]*-*
33 __abort()
35 # can't use "die" because stderr is often redirected to /dev/null
36 # (stdout could also be redirected but it's not so common)
37 echo
38 echo
39 echo "Aborting. configure failed."
40 # this executes __cleanup automatically
41 exit 1
44 # clean temporary files on exit
45 trap '__cleanup' 0
47 # clean temporary files and die with error message if interrupted
48 trap '__abort' 1 2 3 13 15
50 # }}}
52 # config.mk variable names
53 makefile_variables=""
55 # cross compilation, prefix for CC, LD etc.
56 CROSS=
58 # argc function_name $# min [max]
59 argc()
61 if test $# -lt 3 || test $# -gt 4
62 then
63 die "argc: expecting 3-4 arguments (got $*)"
66 if test $# -eq 3
67 then
68 if test $2 -lt $3
69 then
70 die "$1: expecting at least $3 arguments"
72 else
73 if test $2 -lt $3 || test $2 -gt $4
74 then
75 die "$1: expecting $3-$4 arguments"
80 # print warning message (all parameters)
81 warn()
83 echo "$@" >&2
86 # print error message (all parameters) and exit
87 die()
89 warn "$@"
90 exit 1
93 # usage: 'tmp_file .c'
94 # get filename for temporary file
95 tmp_file()
97 if test -z "$__tmp_file_counter"
98 then
99 __tmp_file_counter=0
102 while true
104 __tmp_filename=.tmp-${__tmp_file_counter}-${1}
105 __tmp_file_counter=`expr $__tmp_file_counter + 1`
106 test -f "$__tmp_filename" || break
107 done
108 echo "$__tmp_filename"
111 # get variable value
113 # @name: name of the variable
114 get_var()
116 eval echo '"$'${1}'"'
119 # set variable by name
121 # @name: name of the variable
122 # @value: value of the variable
123 set_var()
125 eval $1='$2'
128 # set variable @name to @default IF NOT SET OR EMPTY
130 # @name: name of the variable
131 # @value: value of the variable
132 var_default()
134 test "`get_var $1`" || set_var $1 "$2"
137 # usage: echo $foo | to_upper
138 to_upper()
140 # stupid solaris tr doesn't understand ranges
141 tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ
144 # portable which command
145 path_find()
147 case $1 in
148 */*)
149 if test -x "$1"
150 then
151 echo "$1"
152 return 0
154 return 1
156 esac
158 _ifs="$IFS"
159 IFS=:
160 for __pf_i in $PATH
162 if test -x "$__pf_i/$1"
163 then
164 IFS="$_ifs"
165 echo "$__pf_i/$1"
166 return 0
168 done
169 IFS="$_ifs"
170 return 1
173 show_usage()
175 cat <<EOF
176 Usage ./configure [-f FILE] [OPTION=VALUE]...
178 -f FILE Read OPTION=VALUE list from FILE (sh script)
179 $USAGE
181 exit 0
184 # @tmpfile: temporary file
185 # @file: file to update
187 # replace @file with @tmpfile if their contents differ
188 update_file()
190 if test -f "$2"
191 then
192 if cmp "$2" "$1" 2>/dev/null 1>&2
193 then
194 return 0
196 echo "updating $2"
197 else
198 echo "creating $2"
200 mv -f "$1" "$2"