-
Notifications
You must be signed in to change notification settings - Fork 5
/
mafia
executable file
·123 lines (92 loc) · 2.78 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/bin/sh -eu
: ${MAFIA_HOME:=$HOME/.mafia}
fetch_latest () {
if [ -z ${MAFIA_TEST_MODE+x} ]; then
TZ=$(date +"%T")
curl --silent "https://raw.githubusercontent.com/ambiata/mafia/master/script/mafia?$TZ"
else
cat ../script/mafia
fi
}
latest_version () {
git ls-remote https://github.com/ambiata/mafia | grep refs/heads/master | cut -f 1
}
local_version () {
awk '/^# Version: / { print $3; exit 0; }' $0
}
run_upgrade () {
MAFIA_TEMP=$(mktemp 2>/dev/null || mktemp -t 'upgrade_mafia')
clean_up () {
rm -f "$MAFIA_TEMP"
}
trap clean_up EXIT
MAFIA_CUR="$0"
if [ -L "$MAFIA_CUR" ]; then
echo 'Refusing to overwrite a symlink; run `upgrade` from the canonical path.' >&2
exit 1
fi
echo "Checking for a new version of mafia ..."
fetch_latest > $MAFIA_TEMP
LATEST_VERSION=$(latest_version)
echo "# Version: $LATEST_VERSION" >> $MAFIA_TEMP
if ! cmp $MAFIA_CUR $MAFIA_TEMP >/dev/null 2>&1; then
mv $MAFIA_TEMP $MAFIA_CUR
chmod +x $MAFIA_CUR
echo "New version found and upgraded. You can now commit it to your git repo."
else
echo "You have latest mafia."
fi
}
exec_mafia () {
MAFIA_VERSION=$(local_version)
if [ "x$MAFIA_VERSION" = "x" ]; then
# If we can't find the mafia version, then we need to upgrade the script.
run_upgrade
else
MAFIA_BIN=$MAFIA_HOME/bin
MAFIA_FILE=mafia-$MAFIA_VERSION
MAFIA_PATH=$MAFIA_BIN/$MAFIA_FILE
[ -f "$MAFIA_PATH" ] || {
# Create a temporary directory which will be deleted when the script
# terminates. Unfortunately `mktemp` doesn't behave the same on
# Linux and OS/X so we need to try two different approaches.
MAFIA_TEMP=$(mktemp -d 2>/dev/null || mktemp -d -t 'exec_mafia')
# Create a temporary file in MAFIA_BIN so we can do an atomic copy/move dance.
mkdir -p $MAFIA_BIN
clean_up () {
rm -rf "$MAFIA_TEMP"
}
trap clean_up EXIT
echo "Building $MAFIA_FILE in $MAFIA_TEMP"
( cd "$MAFIA_TEMP"
git clone https://github.com/ambiata/mafia
cd mafia
git reset --hard $MAFIA_VERSION
bin/bootstrap ) || exit $?
MAFIA_PATH_TEMP=$(mktemp --tmpdir=$MAFIA_BIN $MAFIA_FILE-XXXXXX 2>/dev/null || TMPDIR=$MAFIA_BIN mktemp -t $MAFIA_FILE)
clean_up_temp () {
clean_up
rm -f "$MAFIA_PATH_TEMP"
}
trap clean_up_temp EXIT
cp "$MAFIA_TEMP/mafia/.cabal-sandbox/bin/mafia" "$MAFIA_PATH_TEMP"
chmod 755 "$MAFIA_PATH_TEMP"
mv "$MAFIA_PATH_TEMP" "$MAFIA_PATH"
clean_up_temp
}
exec $MAFIA_PATH "$@"
fi
}
#
# The actual start of the script.....
#
if [ $# -gt 0 ]; then
MODE="$1"
else
MODE=""
fi
case "$MODE" in
upgrade) shift; run_upgrade "$@" ;;
*) exec_mafia "$@"
esac
# Version: 7c6993f5ad2ac2a605cbc46cd5a108358b5e8c06