/work/tools.sh
Shell | 273 lines | 249 code | 19 blank | 5 comment | 29 complexity | 2d1c779a7aa34dcc659ddb9f7419ce92 MD5 | raw file
1# to be sourced by bash tools. 2 3export EXE_SUFFIX='' 4export OS=UNIX 5 6case `uname -s` in 7 CYGWIN*) 8 flavor=generic 9 OS=Cygwin 10 EXE_SUFFIX=".exe" 11 CC_TYPE=${CC_TYPE:-gcc} 12 CC=${CC:-$CC_TYPE} 13 CFLAGS="-pipe" 14 CXX_TYPE=${CXX_TYPE:-g++} 15 CXX=${CXX:-${CXX_TYPE}} 16 CXXFLAGS="-pipe" 17 LDFLAGS="-Xlinker -${hyphen}no-as-needed" 18 germ_cc=${CC} 19 germ_cflags="-pipe -O2 -c -x c" 20 ;; 21 Linux) 22 flavor=Linux 23 jobs=$((1 + $(grep '^processor' /proc/cpuinfo|wc -l))) 24 CC_TYPE=${CC_TYPE:-gcc} 25 CC=${CC:-$CC_TYPE} 26 CFLAGS="-pipe" 27 CXX_TYPE=${CXX_TYPE:-g++} 28 CXX=${CXX:-${CXX_TYPE}} 29 CXXFLAGS="-pipe" 30 LDFLAGS="-Xlinker -${hyphen}no-as-needed" 31 germ_cc=${CC} 32 germ_cflags="-pipe -O2 -c -x c" 33 ;; 34 Darwin) 35 flavor=Darwin 36 jobs=$((1 + $(sysctl -n hw.physicalcpu))) 37 CC_TYPE=${CC_TYPE:-gcc} 38 CC=${CC:-$CC_TYPE} 39 CFLAGS="-pipe" 40 CXX_TYPE=${CXX_TYPE:-g++} 41 CXX=${CXX:-${CXX_TYPE}} 42 CXXFLAGS="-pipe" 43 LDFLAGS="" 44 germ_cc=${CC} 45 germ_cflags="-pipe -O2 -c -x c" 46 ;; 47 SunOS) 48 export PATH=/usr/gnu/bin:$PATH 49 flavor=Solaris 50 jobs=$((1 + $(/sbin/psrinfo | wc -l))) 51 CC_TYPE=${CC_TYPE:-gcc} 52 CC=${CC:-$CC_TYPE} 53 CFLAGS="-pipe" 54 CXX_TYPE=${CXX_TYPE:-g++} 55 CXX=${CXX:-${CXX_TYPE}} 56 CXXFLAGS="-pipe" 57 LDFLAGS="" 58 germ_cc=${CC} 59 germ_cflags="-pipe -O2 -c -x c" 60 ;; 61 *) 62 flavor=uknown 63 jobs=1 64 germ_cc=${CC:-cc} 65 germ_cflags="-O2 -c" 66 ;; 67esac 68 69 70if [ x$plain == x ]; then 71 plain=FALSE 72fi 73if [ x"$(tty)" == "not a tty" ]; then 74 plain=TRUE 75fi 76if [ $plain != TRUE ]; then 77 eval `tset -s` 78 tput init 79fi 80 81bold() { 82 test $plain != TRUE && tput bold 83} 84 85normal() { 86 test $plain != TRUE && tput sgr0 87} 88 89italic() { 90 test $plain != TRUE && tput sitm 91} 92 93underline() { 94 test $plain != TRUE && tput smul 95} 96 97foreground() { 98 if test $plain != TRUE; then 99 case $1 in 100 black) 101 tput setaf 0 102 ;; 103 red) 104 tput setaf 1 105 ;; 106 green) 107 tput setaf 2 108 ;; 109 yellow) 110 tput setaf 3 111 ;; 112 blue) 113 tput setaf 4 114 ;; 115 magenta) 116 tput setaf 5 117 ;; 118 cyan) 119 tput setaf 6 120 ;; 121 white) 122 tput setaf 7 123 ;; 124 esac 125 fi 126} 127 128background() { 129 if test $plain != TRUE; then 130 case $1 in 131 black) 132 tput setab 0 133 ;; 134 red) 135 tput setab 1 136 ;; 137 green) 138 tput setab 2 139 ;; 140 yellow) 141 tput setab 3 142 ;; 143 blue) 144 tput setab 4 145 ;; 146 magenta) 147 tput setab 5 148 ;; 149 cyan) 150 tput setab 6 151 ;; 152 white) 153 tput setab 7 154 ;; 155 esac 156 fi 157} 158 159title() { 160 if test $plain = TRUE; then 161 echo "== $1 ==" 162 else 163 tput el 164 foreground blue 165 bold 166 echo "$1" 167 foreground black 168 normal 169 fi 170} 171 172show_status() { 173 if test $plain = TRUE; then 174 if test $1 = 0; then 175 echo " OK" 176 else 177 echo " KO" 178 fi 179 else 180 tput el 181 if test $1 = 0; then 182 tput setaf 2 183 echo " OK" 184 else 185 tput bold 186 tput setaf 1 187 echo " KO" 188 fi 189 tput setaf 0 190 tput sgr0 191 echo 192 fi 193} 194 195progress() { 196 size=$1 197 current=$2 198 max=$3 199 label="$4" 200 201 echo '~~~~ '$label' ~~~~' >> $LOG 202 # no cut label as workaround for mawk bug https://github.com/ThomasDickey/original-mawk/issues/42 203 labsize=${#label} 204 if [ $labsize -gt 50 ]; then 205 label=${label:0:50}"..." 206 fi 207 208 if test $plain = TRUE; then 209 awk -v max=$max -v cur=$current ' 210 BEGIN { 211 printf(" * %02d/%02d: %s\n", cur, max, "'"$label"'"); 212 exit; 213 }' </dev/null 214 else 215 col=`expr \`tput cols\` - $size - 11` 216 tput setaf 0 217 tput sgr0 218 # For perceived performance, use a non-linear progress bar 219 # See http://blog.codinghorror.com/actual-performance-perceived-performance/ 220 # (Linear is still available if $linear is non-zero) 221 awk -v max=$max -v cur=$current -v size=$size -v col=$col -v linear=0$linear ' 222 BEGIN { 223 x = cur / max; 224 if (linear) { 225 fill = int(size * x + .5); 226 printf(" '`tput bold`'%3.1f%%'`tput sgr0`'\t'`tput setab 6`'", 100 * x); 227 } else { 228 col += 6; 229 fill = int(size * (x + (1-x)/2) ^ 8 + .5); 230 printf(" '`tput setab 6`'"); 231 } 232 for (i = 0; i < fill; i++) printf(" "); 233 printf("'`tput setab 4`'"); 234 for (i = fill; i < size; i++) printf(" "); 235 printf("'`tput sgr0`' '`tput setaf 5`'%-*.*s'`tput sgr0`' \r", col, col, "'"$label"'"); 236 exit; 237 }' </dev/null >/dev/tty 238 fi 239} 240 241error_message() { 242 if test $plain != TRUE; then 243 tput el 244 tput setaf 1 245 tput bold 246 fi 247 echo "$1" | tee -a $LOG 248 echo "$2" | tee -a $LOG 249 if test $plain != TRUE; then 250 tput setaf 0 251 tput sgr0 252 fi 253} 254 255error() { 256 error_message "$2 failed with status $1" "Please look at $LOG" 257} 258 259run() { 260 echo "$(date) - $@" >> $LOG 261 if eval "$*" >>$LOG 2>&1; then 262 status=0 263 else 264 s=$? 265 error $s "$1" 266 status=1 267 fi 268 return $status 269} 270 271ok() { 272 return 273}