aboutsummaryrefslogtreecommitdiffstats
path: root/qst.sh
blob: be7e8edb4da8eb6f41cc5251c3111f455dd4e65d (plain) (blame)
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
#!/bin/sh
set -ue

VERBOSITY=0
BUILD=""

debug() {
    if [ $VERBOSITY -gt 0 ] ; then
        echo "$@"
    fi
}

while getopts ":M:v" opt; do
  case $opt in
    M)
      BUILD="${OPTARG}"
      ;;
    v)
      VERBOSITY=$((VERBOSITY+1))
      ;;
    \?)
      echo "Invalid option: -$OPTARG"
      exit 1
      ;;
    :)
      echo "Option -$OPTARG requires an argument."
      exit 1
      ;;
  esac
done

debug "VERBOSITY is ${VERBOSITY}"
debug "BUILD is ${BUILD}"

shift $((OPTIND - 1))

debug "Length of the remainder is $#"
debug "The remainder is $*"

if [ $# -gt 0 ] ; then
    for LAST in "$@" ; do : ; done
    SPEC_FILE_NAME="$LAST"
else
    SPEC_FILE_NAME="$(/bin/ls -- *.spec|sort -r|head -n1)"
fi

if [ ! -f "$SPEC_FILE_NAME" ] ; then
    echo "File $SPEC_FILE_NAME doesn't exist!"
    exit 1
fi

debug "SPEC_FILE_NAME = $SPEC_FILE_NAME"

if [ -n "$BUILD" ] ; then
    TMP_SPEC="$(mktemp /tmp/qstXXXXXX.spec)"
    trap 'rm -f "${TMP_SPEC}"' EXIT

    sed -E -e 's/@BUILD_FLAVOR@/'$BUILD'/' "$SPEC_FILE_NAME" >"$TMP_SPEC"
else
    TMP_SPEC="$SPEC_FILE_NAME"
fi

# TODO between '-v' and $TMP_SPEC there should be remainder of $@ without the last element
FAILF="$(mktemp /tmp/qstXXXXXX)"
trap 'rm -f "${FAILF}"' EXIT
RES="$(quilt setup -v "$TMP_SPEC" || echo >"$FAILF")"
if [ -s "$FAILF" ] ; then
    echo "$RES" 2>&1
    exit 1
fi
REPOCD="$(echo "$RES"|awk '/^+ rm -rf / {print $4}'|head -n1)"
if [ -n "$REPOCD" ] ; then
    echo "$REPOCD"
fi