-
Notifications
You must be signed in to change notification settings - Fork 3
/
mafia
executable file
·64 lines (57 loc) · 2.04 KB
/
mafia
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/bin/bash -eu
: ${MAFIA_HOME:=$HOME/.mafia}
: ${MAFIA_VERSIONS:=$MAFIA_HOME/versions}
: ${MAFIA_GIT_URL:="https://github.com/ambiata/mafia"}
: ${MAFIA_GIT_BRANCH:="ambiata"}
latest_version () {
git ls-remote ${MAFIA_GIT_URL} | grep "refs/heads/$MAFIA_GIT_BRANCH" | cut -f 1
}
build_version() {
MAFIA_VERSION="$1"
MAFIA_TEMP=$(mktemp -d 2>/dev/null || mktemp -d -t 'exec_mafia')
MAFIA_FILE=mafia-$MAFIA_VERSION
MAFIA_PATH=$MAFIA_VERSIONS/$MAFIA_FILE
mkdir -p $MAFIA_VERSIONS
echo "Building $MAFIA_FILE in $MAFIA_TEMP"
git clone ${MAFIA_GIT_URL} $MAFIA_TEMP
git --git-dir="$MAFIA_TEMP/.git" --work-tree="$MAFIA_TEMP" reset --hard $MAFIA_VERSION || {
echo "mafia version ($MAFIA_VERSION) could not be found." >&2
exit 1
}
(cd "$MAFIA_TEMP" && ./bin/bootstrap) || {
got=$?
echo "mafia version ($MAFIA_VERSION) could not be built." >&2
exit "$got"
}
chmod +x "$MAFIA_TEMP/.cabal-sandbox/bin/mafia"
# Ensure executable is on same file-system so final mv is atomic.
mv -f "$MAFIA_TEMP/.cabal-sandbox/bin/mafia" "$MAFIA_PATH.$$"
mv "$MAFIA_PATH.$$" "$MAFIA_PATH" || {
rm -f "$MAFIA_PATH.$$"
echo "INFO: mafia version ($MAFIA_VERSION) already exists not overiding," >&2
echo "INFO: this is expected if parallel builds of the same version of" >&2
echo "INFO: mafia occur, we are playing by first in, wins." >&2
exit 0
}
}
enable_version() {
if [ $# -eq 0 ]; then
MAFIA_VERSION="$(latest_version)"
echo "INFO: No explicit mafia version requested installing latest ($MAFIA_VERSION)." >&2
else
MAFIA_VERSION="$1"
fi
[ -x "${MAFIA_VERSIONS}/mafia-$MAFIA_VERSION" ] || build_version "$MAFIA_VERSION"
ln -sf "${MAFIA_VERSIONS}/mafia-$MAFIA_VERSION" "${MAFIA_VERSIONS}/mafia"
}
exec_mafia () {
[ -x "${MAFIA_VERSIONS}/mafia" ] || enable_version
"${MAFIA_VERSIONS}/mafia" "$@"
}
#
# The actual start of the script.....
#
case "${1:-}" in
upgrade) shift; enable_version "$@" ;;
*) exec_mafia "$@"
esac