#!bash
# vim:ff=unix:enc=utf8:ts=3:sw=3:et

# src2pkg-ng - package creation toolkit
# Copyright (C) 2005-2009 Gilbert Ashley
# Copyright (C) 2009 Timothy Goya

# src2pkg-ng is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2 as 
# published by the Free Software Foundation

# src2pkg-ng is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with src2pkg-ng.  If not, see <http://www.gnu.org/licenses/>.

add_cleanup_cmd() {
   declare LEVEL="$1"
   declare CMD="$2"
   if (( CLEANUP_LEVEL >= LEVEL )) ; then
      push_front CLEANUP_CMDS "$CMD"
   fi
   export CLEANUP_CMDS
}

cleanup() {
   set +u
   declare TMP_CLEANUP_CMDS=("${CLEANUP_CMDS[@]}")
   set -u
   unset CLEANUP_CMDS
   if (( ${#TMP_CLEANUP_CMDS[@]} )) ; then
      tprintf $"@summary{Cleaning up} - "
      for CMD in "${TMP_CLEANUP_CMDS[@]}" ; do
         echo " $CMD"
         "$CMD"
      done
      tprintf $"@success{Done}\n"
      unset CMD
   fi
   if [[ "${1:-""}" ]] ; then
      exit $1
   fi
}

trap "cleanup 1" SIGINT ERR
