blob: 2846dc42d0fc11a36c04360a6ea43d572fb963ff [file] [log] [blame]
John Garbuttdaadf742012-04-27 18:28:28 +01001#!/bin/bash
2
3set -e
4set -o xtrace
5
Bob Ball5fb83a02014-07-29 13:40:57 +01006if [ -z "${on_exit_hooks:-}" ]; then
7 on_exit_hooks=()
8fi
John Garbuttdaadf742012-04-27 18:28:28 +01009
10on_exit()
11{
Sean Dague16dd8b32014-02-03 09:10:54 +090012 for i in $(seq $((${#on_exit_hooks[*]} - 1)) -1 0); do
John Garbuttdaadf742012-04-27 18:28:28 +010013 eval "${on_exit_hooks[$i]}"
14 done
15}
16
17add_on_exit()
18{
19 local n=${#on_exit_hooks[*]}
20 on_exit_hooks[$n]="$*"
Sean Dague16dd8b32014-02-03 09:10:54 +090021 if [[ $n -eq 0 ]]; then
John Garbuttdaadf742012-04-27 18:28:28 +010022 trap on_exit EXIT
23 fi
24}