--- a/scripts/lib.sh Wed Dec 28 11:04:02 2011 +0200
+++ b/scripts/lib.sh Thu Jan 05 14:28:31 2012 +0200
@@ -1,20 +1,55 @@
## library functions
+LOG_DEBUG=
+LOG_CMD=y
+LOG_INFO=y
+LOG_WARN=y
+LOG_ERROR=y
+
+function log_debug () {
+ [ $LOG_DEBUG ] && echo "... $@" >&2
+}
+
+function log_info () {
+ [ $LOG_INFO ] && echo "--- $@" >&2
+}
+
+function log_cmd () {
+ [ $LOG_CMD ] && echo ">>> $@" >&2
+}
+
+function log_warn () {
+ [ $LOG_WARN ] && echo "XXX $@" >&2
+}
+
+function log_error () {
+ [ $LOG_ERROR ] && echo "!!! $@" >&2
+}
+
function die () {
- echo "!!! $@" >&2
+ log_error "$@"
exit 1
}
# Execute command verbosely, and exit on failure
+CMD_MOCK=
+CMD_PROMPT=y
+
function cmd () {
- echo ">>> $@"
+ log_cmd "$@"
- [ $_MOCK ] && return 0
+ [ $CMD_MOCK ] && return 0
eval "$@" # return $?
}
+function cmd_confirm () {
+ [ $CMD_PROMPT ] && read -p "Confirm: $1"
+
+ cmd "$@"
+}
+
function expand_MB () {
local size=${1^}
@@ -67,6 +102,22 @@
)
}
+function expand_line () {
+ local line=$1
+
+ # evaluate {...} expressions
+ # a slight hack, but it works \o/
+ # http://stackoverflow.com/questions/415677/how-to-replace-placeholders-in-a-text-file/7633579#7633579
+ line="${line//\\/\\\\}"
+ line="${line//\"/\\\"}"
+ line="${line//\`/\\\`}"
+ line="${line//\$/\\\$}"
+ line="${line//{/\${}" # This is just for vim: } "
+
+ # log_debug "($line)" >&2
+ eval "echo \"$line\""
+}
+
function expand_template () {
local tpl=$1
local out=$2
@@ -82,17 +133,7 @@
echo "$line"
else
- # evaluate {...} expressions
- # a slight hack, but it works \o/
- # http://stackoverflow.com/questions/415677/how-to-replace-placeholders-in-a-text-file/7633579#7633579
- line="${line//\\/\\\\}"
- line="${line//\"/\\\"}"
- line="${line//\`/\\\`}"
- line="${line//\$/\\\$}"
- line="${line//{/\${}" # This is just for vim: } "
-
- # echo "($line)" >&2
- (eval "echo \"$line\"") || die "Error at $tpl:$linecount: $line"
+ expand_line "$line" || die "Error at $tpl:$linecount: $line"
fi
done < $tpl > $out
@@ -110,15 +151,15 @@
local target=$dst/$name
if [ -d $path ]; then
- echo "expand_tree: $path -> $target"
- echo "expand_tree $path $target $filter"
+ log_debug "expand_tree: $path -> $target"
+ expand_tree $path $target $filter
elif [ -f $path ]; then
- echo "expand_file: $path -> $target"
- echo "expand_file $path $target"
+ log_debug "expand_file: $path -> $target"
+ expand_file $path $target
else
- echo "XXX: ignore weird file: $path"
+ log_warn "ignore weird file: $path"
fi
done