1#!/usr/bin/env bash
2
3# This shell script will do the following.
4#
5# - create a temp directory.
6# - run the given command line, by providing the information about the
7#   location of the temp directory in any desired way (see below for
8#   typical uses)
9# - clean up the temp directory.  If CADO_DEBUG is set to something, the
10#   temp directory is not cleaned.
11#
12# Typical uses:
13#
14# The provided temp dir can be given to the subcommand via a command line
15# option (anything that starts with a dash is interpreted as such).
16#
17#       ./provide-wdir.sh --arg --wdir ./foo.sh [args]
18#
19#       creates a temp dir (say $T) and run ./foo.sh [args] --wdir $T
20#
21# If the token that follows --arg does not start with a dash, the command
22# line addition looks like an assignment instead. Note that several
23# arguments can be set to the created temp directory.
24#
25#       ./provide-wdir.sh --arg wdir --arg x ./foo.sh [args]
26#
27#       creates a temp dir (say $T) and run ./foo.sh [args] wdir=$T x=$T
28#
29# The provided temp dir can be given to the subcommand via an environment
30# variable.
31#
32#       ./provide-wdir.sh --env wdir ./foo.sh [args]
33#
34#       creates a temp dir (say $T) and run env wdir=$T ./foo.sh [args]
35#
36#
37# It is also possible to create several temp directories.
38#
39#       ./provide-wdir.sh --env wdir --other --env wdir2 ./foo.sh [args]
40#
41#       creates two temp dirs (say $T and $T2) and run env wdir=$T wdir2=$T2 ./foo.sh [args]
42#
43
44: ${TMPDIR:=/tmp}
45t=`mktemp -d $TMPDIR/cado-nfs.XXXXXXXXXXXXXX`
46temps=("$t")
47
48cleanup() { rm -rf "${temps[@]}" ; }
49
50if [ "$CADO_DEBUG" ] ; then
51    echo "debug mode, data will be left in $t"
52    set -x
53else
54    trap cleanup EXIT
55fi
56
57extra=()
58
59while [ $# -gt 0 ] ; do
60    if [ "$1" = "--" ] ; then
61        shift
62        break;
63    elif [ "$1" = "--env" ] ; then
64        shift
65        var="$1"
66        eval "$var=$t"
67        eval "export $var"
68        shift
69    elif [ "$1" = "--arg" ] ; then
70        shift
71        case "$1" in
72            -*) extra+=("$1" "$t");;
73            *) extra+=("$1=$t");;
74        esac
75        shift
76    elif [ "$1" = "--other" ] ; then
77        t=`mktemp -d $TMPDIR/cado-nfs.XXXXXXXXXXXXXX`
78        temps+=("$t")
79        if [ "$CADO_DEBUG" ] ; then
80            echo "debug mode, data will be left in $t (in addition to other temp directories above)"
81        fi
82        shift
83    else
84        # Then we finish processing, presumably the user omitted the --
85        # separator.
86        break
87    fi
88done
89
90# add our extra parameters before the first occurrence of the -- separator, if we happen to find any.
91main=()
92
93while [ $# -gt 0 ] ; do
94    if [ "$1" = "--" ] ; then
95        break
96    else
97        main+=("$1")
98        shift
99    fi
100done
101
102"${main[@]}" "${extra[@]}" "$@"
103
104rc=$?
105
106if [ "$CADO_DEBUG" ] ; then
107    echo "debug mode, data left in ${temps[@]}"
108    exit $rc
109fi
110
111if [ $rc != 0 ] ; then
112    trap - EXIT
113    # what do we do when the script failed ?
114    rm -rf "${temps[@]}"
115fi
116
117exit $rc
118