summaryrefslogtreecommitdiff
path: root/wrappers/shadow-strip.sh
blob: 3ff7d831558ec6e5c16eb8c577882fc4798125dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/bin/bash

set -euf -o pipefail

top="#TOP#"
shadow_top="#SHADOW_TOP#"

orig_strip="#ORIG_TOOL#"
shadow_strip="#SHADOW_TOOL#"

errors=()
warnings=()
skips=()

orig_out=()
shadow_out=()
orig_input=()
shadow_input=()
orig_opts=()
shadow_opts=()

dump_log ()
{
    (
    set +u
    IFS=";"
    cat <<EOF
errors=(${errors[@]})
warnings=(${warnings[@]})
skips=(${skips[@]})
orig_out=(${orig_out[@]})
shadow_out=(${shadow_out[@]})
orig_input=(${orig_input[@]})
shadow_input=(${shadow_input[@]})
orig_opts=($0 ${orig_opts[@]})
shadow_opts=(${shadow_opts[@]})

EOF
    )
}

report_errors () {
    res=$?
    if [ $res != 0 ]; then
	errors+=("wrapper failed with $res")
    fi
    if [ ${#errors[@]} != 0 ] \
	   || [ ${#warnings[@]} != 0 ] \
	   || [ ${#skips[@]} != 0 ]; then
	(
	    $HOME/bin/myflock -e 9

	    if [ ${#errors[@]} != 0 ]; then
		log=$shadow_top.errors
	    elif [ ${#warnings[@]} != 0 ]; then
		log=$shadow_top.warnings
	    else
		log=$shadow_top.skips
	    fi
	    dump_log >>$log
	) 9>>$shadow_top.lock
    fi
}

trap report_errors EXIT

while [ "$#" -gt 0 ]; do
    opt="$1"
    shift

    orig_opts+=("$opt")

    case "$opt" in

	"-o")
	    if [ "$#" -gt 0 ]; then
		orig_out=("$1" "${orig_out[@]}")
		orig_opts+=("$1")
		shift
	    else
		errors+=("$opt with no argument")
	    fi
	    ;;

	"-i")
	    if [ "$#" -gt 0 ]; then
		orig_input=("$1" "${orig_input[@]}")
		orig_opts+=("$1")
		shift
	    else
		errors+=("$opt with no argument")
	    fi
	    ;;

	"-d")
	    if [ "$#" -gt 0 ]; then
		orig_opts+=("$1")
		shift
	    else
		errors+=("$opt with no argument")
	    fi
	    ;;

	"-"*)
	    ;;
    esac
done

print_shadow_path ()
{
    local path="$1"
    local prefix="${2-}"

    case "$path" in
	"/dev/null") echo "$path"; return ;;
	"/"*) errors+=("Absolute path: $path") ;;
    esac

    # Check that we are under $top
    case "$(pwd -P | sed -e "s#^$top##")" in
	"/"*) errors+=("Running outside of $top") ;;
    esac

    echo "$(pwd -P)/$path" | sed -e "s#$top#$shadow_top${prefix}#"
}

make_shadow_opts ()
{
    local opt

    while [ "$#" -gt 0 ]; do
	opt="$1"
	shift

	case "$opt" in
	    "-o")
		shadow_opts+=("$opt")
		opt=$(print_shadow_path "$1")
		shadow_out+=("$opt")
		shift
		;;

	    "-i")
		shadow_opts+=("$opt")
		opt=$(print_shadow_path "$1")
		shadow_input+=("$opt")
		shift
		;;

	    "-d")
		shadow_opts+=("$opt")
		opt="/dev/null"
		shift
		;;
	esac
	shadow_opts+=("$opt")
    done
}

if [ -e "${shadow_input[*]}" ] \
       && [ ${#errors[@]} = 0 ] && [ ${#skips[@]} = 0 ]; then
    make_shadow_opts "${orig_opts[@]}"
fi

if [ -e "${shadow_input[*]}" ] \
       && [ ${#errors[@]} = 0 ] && [ ${#skips[@]} = 0 ]; then
    shadow_log=$(print_shadow_path "${orig_out[*]}" "-logs")

    mkdir -p "$(dirname "${shadow_out[*]}")"
    mkdir -p "$(dirname "$shadow_log")"

    dump_log > "$shadow_log"
    echo "$shadow_strip" "${shadow_opts[@]}" >> "$shadow_log"
    "$shadow_strip" "${shadow_opts[@]}" >> "$shadow_log" 2>&1 &
    shadow_res=0 && wait $! || shadow_res=$?

    if [ $shadow_res != 0 ]; then
	errors+=("shadow_strip failed with $shadow_res")
    elif ! [ -e "${shadow_out[*]}" ]; then
	errors+=("shadow_strip failed to generate output")
    else
	shadow_link=$(IFS=""; print_shadow_path "${orig_out[*]}" "-default-A")
	mkdir -p "$(dirname "$shadow_link")"
	ln -s -f "${shadow_out[*]}" "$shadow_link"
    fi
fi

"$orig_strip" "${orig_opts[@]}" &
orig_res=0 && wait $! || orig_res=$?

if [ $orig_res != 0 ]; then
    errors+=("orig_strip failed with $orig_res")
fi

exit $orig_res