#!/bin/bash -eu

function printError(){
    local message="$*"
    printf "ERROR    %s\n" "$message" >&2
}

function printWarning(){
    local message="$*"
    printf "WARNING  %s\n" "$message" >&2
}

function printInfo(){
    local message="$*"
    printf "INFO     %s\n" "$message" >&2
}

function printMessage(){
    printInfo "$@"
}

function quote(){
    # concatenate all the arguments and shell-quote them.
    printf '%s\n' "$*" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/'/"
}

function quoteRegexp(){
    # concatenate all the arguments and regexp-quote them.
    printf '%s\n' "$*" | sed 's/\(.\)/[\1]/g'
}


lockfile=''
function with_lock(){
    local file="$1";shift
    lockfile="${file}.lock"
    while ! shlock -f "${lockfile}" -p $$ ; do
        sleep 1
    done
    trap 'rm -f "${lockfile}" 2>/dev/null' 0
}

function saveStatus(){
    local statusFile="$1"
    local status="$2"
    with_lock "$statusFile"
    echo "$status" >> "$statusFile"
}


function main(){
    local statusFile="$1"
    shift
    local status=0
    "$@" || status=$?
    saveStatus "$statusFile" "$status"
    exit $status
}

main "$@"
ViewGit