blob: 2441e3d84a6ea2c7940cf67b8e32e0d95ea44e9e [file] [log] [blame]
John Garbuttdaadf742012-04-27 18:28:28 +01001#!/bin/bash
2
3set -e
4set -o xtrace
5
6declare -a on_exit_hooks
7
8on_exit()
9{
Sean Dague16dd8b32014-02-03 09:10:54 +090010 for i in $(seq $((${#on_exit_hooks[*]} - 1)) -1 0); do
John Garbuttdaadf742012-04-27 18:28:28 +010011 eval "${on_exit_hooks[$i]}"
12 done
13}
14
15add_on_exit()
16{
17 local n=${#on_exit_hooks[*]}
18 on_exit_hooks[$n]="$*"
Sean Dague16dd8b32014-02-03 09:10:54 +090019 if [[ $n -eq 0 ]]; then
John Garbuttdaadf742012-04-27 18:28:28 +010020 trap on_exit EXIT
21 fi
22}