#!/bin/sh # vim:set noet ts=4 sts=4: # jpokorny@redhat.com # # internals # CHOICES="" check_nargs () { [ $1 -ge $2 ] ret=$? [ $ret -eq 0 ] || announce "action failed (expected $1+ args, got $2)" return $ret } check_ret () { [ $1 -ne 0 ] && announce "action failed with exit status $1" return $1 } do_del () { #echo "delete $1?" [ -d "$1" ] && rm -rfI -- "$1" || echo "$1 not present" } # # "API" for sourced choices # register () { # usage: $1=choice to register CHOICES="$CHOICES|$1" } announce () { # usage: $1=message to output # use colours if available echo if test -t 1; then echo -en "\\033[32m" echo -n "$1" echo -e "\\033[0m" else echo "[[ $1 ]]" fi } do_git_submodule () { # usage: $1=action ($1 passed from main), $2..$N=submodule(s) ret=0 # TODO: pushd $GIT_ROOT for submodule in "${@:2}"; do announce "$1 $submodule (git submodule)" if [ "$1" == "get" ]; then # TODO: avoid this workaround if [ ! -f .gitmodules ]; then submodule="$SUBMODULES_DIR/$submodule" fi git submodule update --init "$submodule" 2>&1 else do_del "$submodule" fi ret=$? [ $ret -ne 0 ] && break done popd check_ret $ret } do_wget () { # usage: $1=action ($1 passed from main), $2=DIR, $3..$N=arguments (to wget) check_nargs $# 3 || return $? toplevel="$2" if [ "$(dirname $2)" != "." ]; then toplevel="$(dirname $2)" fi announce "$1 $toplevel (wget)" if [ "$1" == "get" ]; then wget --no-verbose --no-clobber --execute robots=off \ --directory-prefix "$2" "${@:3}" 2>&1 else do_del "$toplevel" fi check_ret $? } do_svn () { # usage: $1=action ($1 passed from main), $2=DIR, $3=SVN [, $4=REV] check_nargs $# 3 || return $? toplevel="$2" if [ "$(dirname $2)" != "." ]; then toplevel="$(dirname $2)" fi announce "$1 $toplevel (svn)" if [ "$1" == "get" ]; then rev="" if [ $# -ge 4 ]; then rev="--revision $4" fi svn checkout --force $rev "$3" "$2" | grep "revision" else do_del "$toplevel" fi check_ret $? } # # source the choices (./init-* files) and go # for choice in $(ls init-*); do [ -f $choice ] && source ./$choice; done CHOICES=$(echo $CHOICES | sed 's/^|//') main () { action="get" if [ "$1" == "get" -o "$1" == "del" ]; then action="$1" [ $# -gt 1 ] && shift # || fall-through via unrecognized choice fi while [ -n "$1" ]; do eval "case \"$1\" in $CHOICES) $1 $action || exit $? shift continue;; all) main $action $(echo $CHOICES | sed 's/|/ /g') break;; help|*) echo \"usage: ./init.sh [del] ($CHOICES|all)+\" break;; esac" done } [ $# -eq 0 ] && main help || main "$@"