terom@1: ## library functions terom@1: terom@7: LOG_DEBUG= terom@7: LOG_CMD=y terom@7: LOG_INFO=y terom@7: LOG_WARN=y terom@7: LOG_ERROR=y terom@7: terom@7: function log_debug () { terom@7: [ $LOG_DEBUG ] && echo "... $@" >&2 terom@9: terom@9: return 0 terom@7: } terom@7: terom@8: function log_debugf () { terom@8: local fmt=$1; shift 1 terom@8: terom@8: [ $LOG_DEBUG ] && printf "... $fmt\n" "$@" >&2 terom@9: terom@9: return 0 terom@8: } terom@8: terom@7: function log_info () { terom@7: [ $LOG_INFO ] && echo "--- $@" >&2 terom@9: terom@9: return 0 terom@7: } terom@7: terom@7: function log_cmd () { terom@7: [ $LOG_CMD ] && echo ">>> $@" >&2 terom@9: terom@9: return 0 terom@7: } terom@7: terom@7: function log_warn () { terom@7: [ $LOG_WARN ] && echo "XXX $@" >&2 terom@9: terom@9: return 0 terom@7: } terom@7: terom@7: function log_error () { terom@7: [ $LOG_ERROR ] && echo "!!! $@" >&2 terom@9: terom@9: return 0 terom@7: } terom@7: terom@1: function die () { terom@7: log_error "$@" terom@1: terom@1: exit 1 terom@1: } terom@1: terom@1: # Execute command verbosely, and exit on failure terom@7: CMD_MOCK= terom@7: CMD_PROMPT=y terom@7: terom@1: function cmd () { terom@7: log_cmd "$@" terom@1: terom@7: [ $CMD_MOCK ] && return 0 terom@1: terom@17: "$@" # return $? terom@1: } terom@1: terom@7: function cmd_confirm () { terom@10: [ $CMD_PROMPT ] && read -p "Confirm: $* ? " terom@7: terom@7: cmd "$@" terom@7: } terom@7: terom@30: function cmd_sudo () { terom@30: [ $CMD_PROMPT ] && read -p "Confirm: sudo: $* ? " terom@30: terom@30: cmd sudo -- "$@" terom@30: } terom@30: terom@30: terom@1: function expand_MB () { terom@1: local size=${1^} terom@1: terom@1: case ${size: -1} in terom@1: G) terom@1: size=$(( ${size%G} * 1024)) terom@1: terom@1: ;; terom@1: esac terom@1: terom@1: echo $size terom@1: } terom@1: terom@1: function extract_iso () { terom@1: iso=$1 terom@1: dst=$2 terom@1: terom@1: [ ! -r "$iso" ] && die "Given .iso is not readable: $iso" terom@1: [ -z "$dst" ] && die "Must give destination: $dst" terom@1: [ -e "$dst" ] && die "Given destination already exists: $dst" terom@1: terom@1: # temporary mount terom@1: mnt=$(mktemp -d mnt.XXXX) terom@1: terom@1: # clean on exit terom@1: function cleanup () { terom@1: if mountpoint -q $mnt; then terom@1: sudo umount $mnt terom@1: fi terom@1: terom@1: [ -d $mnt ] && rmdir $mnt terom@1: terom@1: return $1 terom@1: } terom@1: terom@1: # loop-mount terom@30: cmd_sudo mount -o loop $iso $mnt || cleanup 1 terom@1: terom@1: # copy terom@11: cmd cp -rd $mnt $dst || cleanup 1 terom@1: terom@1: # done, cleanup terom@1: cleanup 0 terom@1: } terom@1: terom@1: function my_md5sum () { terom@1: /usr/bin/md5sum $1 | ( terom@1: read md5sum path terom@1: echo $md5sum terom@1: ) terom@1: } terom@1: terom@7: function expand_line () { terom@7: local line=$1 terom@7: terom@7: # evaluate {...} expressions terom@7: # a slight hack, but it works \o/ terom@7: # http://stackoverflow.com/questions/415677/how-to-replace-placeholders-in-a-text-file/7633579#7633579 terom@7: line="${line//\\/\\\\}" terom@7: line="${line//\"/\\\"}" terom@7: line="${line//\`/\\\`}" terom@7: line="${line//\$/\\\$}" terom@7: line="${line//{/\${}" # This is just for vim: } " terom@7: terom@7: # log_debug "($line)" >&2 terom@7: eval "echo \"$line\"" terom@7: } terom@7: terom@1: function expand_template () { terom@1: local linecount=0 terom@1: terom@1: # read in each line at a time terom@1: while IFS='' read -r line; do terom@1: linecount=$((linecount + 1)) terom@1: terom@1: if [ "${line:0:1}" == "#" ]; then terom@1: # ignore comments; pass through as-is terom@1: echo "$line" terom@1: terom@1: else terom@7: expand_line "$line" || die "Error at $tpl:$linecount: $line" terom@1: fi terom@1: terom@25: done terom@1: } terom@1: terom@15: function expand_file () { terom@15: local src=$1 terom@15: local dst=$2 terom@15: terom@15: log_info "expand: file $src -> $dst" terom@15: terom@25: expand_template < $src > $dst terom@15: } terom@15: terom@1: # Recursive expand_template files from src -> dst terom@1: # XXX: not used terom@1: function expand_tree () { terom@1: local src=$1 terom@1: local dst=$2 terom@1: local filter=${3:-'*'} terom@1: terom@15: log_info "expand: tree $src/$filter -> $dst" terom@15: terom@15: [ ! -d $dst ] && cmd mkdir $dst terom@15: terom@1: for path in ${src}/${filter}; do terom@1: local name=$(basename $path) terom@1: local target=$dst/$name terom@1: terom@15: log_debug "expand_tree: $src: path=$path, name=$name, target=$target" terom@15: terom@1: if [ -d $path ]; then terom@15: log_debug "expand_tree: $src: tree $name -> $target" terom@15: expand_tree $path $target "$filter" terom@1: terom@1: elif [ -f $path ]; then terom@15: log_debug "expand_tree: $src: file $name -> $target" terom@7: expand_file $path $target terom@1: terom@1: else terom@15: log_warn "expand_tree: $src: ignore $name" terom@1: terom@1: fi terom@1: done terom@1: }