| garciadeblas | 8d8cd99 | 2024-05-21 16:04:14 +0200 | [diff] [blame] | 1 | ####################################################################################### |
| 2 | # Copyright ETSI Contributors and Others. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 13 | # implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | ####################################################################################### |
| 17 | |
| 18 | function goodbye() { |
| 19 | local DURATION=$(date --date=@$(( "$(date +%s)" - "$TRAP_START_TIME" )) --utc +%T) |
| 20 | local CODE=$1 |
| 21 | cd "$TRAP_DIR" |
| 22 | if [ "$CODE" == 0 ]; then |
| 23 | m "$(realpath --relative-to="$HERE" "$0") succeeded! $DURATION" "$GREEN" |
| 24 | elif [ "$CODE" == abort ]; then |
| 25 | m "Aborted $(realpath --relative-to="$HERE" "$0")! $DURATION" "$RED" |
| 26 | else |
| 27 | m "Oh no! $(realpath --relative-to="$HERE" "$0") failed! $DURATION" "$RED" |
| 28 | fi |
| 29 | } |
| 30 | |
| 31 | function trap_EXIT() { |
| 32 | local ERR=$? |
| 33 | goodbye "$ERR" |
| 34 | exit "$ERR" |
| 35 | } |
| 36 | |
| 37 | function trap_INT() { |
| 38 | goodbye abort |
| 39 | trap - EXIT |
| 40 | exit 1 |
| 41 | } |
| 42 | |
| 43 | TRAP_DIR=$PWD |
| 44 | TRAP_START_TIME=$(date +%s) |
| 45 | |
| 46 | trap trap_INT INT |
| 47 | |
| 48 | trap trap_EXIT EXIT |