10f66f451Sopenharmony_ci#!/bin/bash
20f66f451Sopenharmony_ci
30f66f451Sopenharmony_ci# Set up command recording wrapper
40f66f451Sopenharmony_ci
50f66f451Sopenharmony_ci[ -z "$WRAPDIR" ] && WRAPDIR="$PWD"/record-commands && RM=$(which rm)
60f66f451Sopenharmony_ci[ -z "$WRAPLOG" ] && export WRAPLOG="$PWD"/log.txt
70f66f451Sopenharmony_ci
80f66f451Sopenharmony_ciif [ $# -eq 0 ]
90f66f451Sopenharmony_cithen
100f66f451Sopenharmony_ci  echo "usage: WRAPDIR=dir WRAPLOG=log.txt record-commands COMMAND..."
110f66f451Sopenharmony_ci  echo 'Then examine log.txt. "record-commands echo" to just setup $WRAPDIR'
120f66f451Sopenharmony_ci  exit 1
130f66f451Sopenharmony_cifi
140f66f451Sopenharmony_ci
150f66f451Sopenharmony_ciif [ ! -x "$WRAPDIR/logwrapper" ]
160f66f451Sopenharmony_cithen
170f66f451Sopenharmony_ci  make logwrapper
180f66f451Sopenharmony_ci  mkdir -p "$WRAPDIR" && mv logwrapper "$WRAPDIR" || exit 1
190f66f451Sopenharmony_ci
200f66f451Sopenharmony_ci  echo "$PATH" | tr : '\n' | while read DIR
210f66f451Sopenharmony_ci  do
220f66f451Sopenharmony_ci    ls "$DIR/" | while read FILE
230f66f451Sopenharmony_ci    do
240f66f451Sopenharmony_ci      ln -s logwrapper "$WRAPDIR/$FILE" 2>/dev/null
250f66f451Sopenharmony_ci    done
260f66f451Sopenharmony_ci  done
270f66f451Sopenharmony_cifi
280f66f451Sopenharmony_ci
290f66f451Sopenharmony_ci# Delete old log (if any)
300f66f451Sopenharmony_cirm -f "$WRAPLOG"
310f66f451Sopenharmony_ci
320f66f451Sopenharmony_ciX=0
330f66f451Sopenharmony_ciif [ ! -z "$1" ]
340f66f451Sopenharmony_cithen
350f66f451Sopenharmony_ci  PATH="$WRAPDIR:$PATH" "$@"
360f66f451Sopenharmony_cifi
370f66f451Sopenharmony_ciX=$?
380f66f451Sopenharmony_ci[ ! -z "$RM" ] && "$RM" -rf "$WRAPDIR"
390f66f451Sopenharmony_ci
400f66f451Sopenharmony_ciexit $X
41