scripts/lib.sh
changeset 7 ab661ceed4dc
parent 1 51b1db97f448
child 8 ba98eb53344a
equal deleted inserted replaced
6:8ed4a750d133 7:ab661ceed4dc
     1 ## library functions
     1 ## library functions
     2 
     2 
       
     3 LOG_DEBUG=
       
     4 LOG_CMD=y
       
     5 LOG_INFO=y
       
     6 LOG_WARN=y
       
     7 LOG_ERROR=y
       
     8 
       
     9 function log_debug () {
       
    10     [ $LOG_DEBUG ] && echo "... $@" >&2
       
    11 }
       
    12 
       
    13 function log_info () {
       
    14     [ $LOG_INFO ] && echo "--- $@" >&2
       
    15 }
       
    16 
       
    17 function log_cmd () {
       
    18     [ $LOG_CMD ] && echo ">>> $@" >&2
       
    19 }
       
    20 
       
    21 function log_warn () {
       
    22     [ $LOG_WARN ] && echo "XXX $@" >&2
       
    23 }
       
    24 
       
    25 function log_error () {
       
    26     [ $LOG_ERROR ] && echo "!!! $@" >&2
       
    27 }
       
    28 
     3 function die () {
    29 function die () {
     4     echo "!!! $@" >&2
    30     log_error "$@"
     5 
    31 
     6     exit 1
    32     exit 1
     7 }
    33 }
     8 
    34 
     9 # Execute command verbosely, and exit on failure
    35 # Execute command verbosely, and exit on failure
       
    36 CMD_MOCK=
       
    37 CMD_PROMPT=y
       
    38 
    10 function cmd () {
    39 function cmd () {
    11     echo ">>> $@"
    40     log_cmd "$@"
    12 
    41 
    13     [ $_MOCK ] && return 0
    42     [ $CMD_MOCK ] && return 0
    14 
    43 
    15     eval "$@" # return $?
    44     eval "$@" # return $?
       
    45 }
       
    46 
       
    47 function cmd_confirm () {
       
    48     [ $CMD_PROMPT ] && read -p "Confirm: $1"
       
    49 
       
    50     cmd "$@"
    16 }
    51 }
    17 
    52 
    18 function expand_MB () {
    53 function expand_MB () {
    19     local size=${1^}
    54     local size=${1^}
    20 
    55 
    65         read md5sum path
   100         read md5sum path
    66         echo $md5sum
   101         echo $md5sum
    67     )
   102     )
    68 }
   103 }
    69 
   104 
       
   105 function expand_line () {
       
   106     local line=$1
       
   107 
       
   108     # evaluate {...} expressions
       
   109     # a slight hack, but it works \o/
       
   110     # http://stackoverflow.com/questions/415677/how-to-replace-placeholders-in-a-text-file/7633579#7633579
       
   111     line="${line//\\/\\\\}"
       
   112     line="${line//\"/\\\"}"
       
   113     line="${line//\`/\\\`}"
       
   114     line="${line//\$/\\\$}"
       
   115     line="${line//{/\${}"   # This is just for vim: } "
       
   116 
       
   117     # log_debug "($line)" >&2
       
   118     eval "echo \"$line\""
       
   119 }
       
   120 
    70 function expand_template () {
   121 function expand_template () {
    71     local tpl=$1
   122     local tpl=$1
    72     local out=$2
   123     local out=$2
    73 
   124 
    74     local linecount=0
   125     local linecount=0
    80         if [ "${line:0:1}" == "#" ]; then
   131         if [ "${line:0:1}" == "#" ]; then
    81             # ignore comments; pass through as-is
   132             # ignore comments; pass through as-is
    82             echo "$line"
   133             echo "$line"
    83         
   134         
    84         else
   135         else
    85             # evaluate {...} expressions
   136             expand_line "$line" || die "Error at $tpl:$linecount: $line"
    86             # a slight hack, but it works \o/
       
    87             # http://stackoverflow.com/questions/415677/how-to-replace-placeholders-in-a-text-file/7633579#7633579
       
    88             line="${line//\\/\\\\}"
       
    89             line="${line//\"/\\\"}"
       
    90             line="${line//\`/\\\`}"
       
    91             line="${line//\$/\\\$}"
       
    92             line="${line//{/\${}"   # This is just for vim: } "
       
    93 
       
    94             # echo "($line)" >&2
       
    95             (eval "echo \"$line\"") || die "Error at $tpl:$linecount: $line"
       
    96         fi
   137         fi
    97  
   138  
    98     done < $tpl > $out
   139     done < $tpl > $out
    99 }
   140 }
   100 
   141 
   108     for path in ${src}/${filter}; do
   149     for path in ${src}/${filter}; do
   109         local name=$(basename $path)
   150         local name=$(basename $path)
   110         local target=$dst/$name
   151         local target=$dst/$name
   111 
   152 
   112         if [ -d $path ]; then
   153         if [ -d $path ]; then
   113             echo "expand_tree: $path -> $target"
   154             log_debug "expand_tree: $path -> $target"
   114             echo "expand_tree $path $target $filter"
   155             expand_tree $path $target $filter
   115 
   156 
   116         elif [ -f $path ]; then
   157         elif [ -f $path ]; then
   117             echo "expand_file: $path -> $target"
   158             log_debug "expand_file: $path -> $target"
   118             echo "expand_file $path $target"
   159             expand_file $path $target
   119 
   160 
   120         else
   161         else
   121             echo "XXX: ignore weird file: $path"
   162             log_warn "ignore weird file: $path"
   122 
   163 
   123         fi
   164         fi
   124     done
   165     done
   125 }
   166 }