summaryrefslogtreecommitdiffstats
path: root/tools/ghc-rpm-macros/cabal-tweak-flag
blob: 2db3a72309e00b5c5ad20ed9d6acecd86d55d6ad (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
#!/bin/sh

# TODO:
# support setting flag when no upstream default

set -e +x

USAGE="Usage: $0 FLAG [True|False]"

if [ $# -ne 2 ]; then
    echo "$USAGE"
    exit 1
fi

FLAG=$1

NEW=$2
case $NEW in
    True) OLD=False ;;
    False) OLD=True ;;
    *) echo "Flag value can only be set to True or False" ; exit 1 ;;
esac

CABALFILE=$(ls *.cabal)

if [ $(echo $CABALFILE | wc -w) -ne 1 ]; then
   echo "There needs to be one .cabal file in the current dir!"
   exit 1
fi

if ! grep -q -i "^flag *$FLAG" $CABALFILE; then
   echo "$CABALFILE does have flag $FLAG"
   exit 1
fi

if ! grep -A3 -i "^flag *$FLAG" $CABALFILE | grep -q -i "default:"; then
   echo "$CABALFILE: $FLAG flag might not have a default"
   exit 1
fi

if ! grep -A3 -i "^flag *$FLAG" $CABALFILE | grep -q -i "default: *$OLD"; then
   echo "$CABALFILE: $FLAG flag already defaults to $NEW"
   exit 1
fi

if [ ! -f $CABALFILE.orig ]; then
    BACKUP=.orig
fi

sed -i$BACKUP -e "/[Ff]lag *$FLAG/,/[Dd]efault: *$OLD/ s/\([Dd]efault: *\)$OLD/\1$NEW/" $CABALFILE