/work/tools.sh

http://github.com/tybor/Liberty · Shell · 273 lines · 249 code · 19 blank · 5 comment · 29 complexity · 2d1c779a7aa34dcc659ddb9f7419ce92 MD5 · raw file

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