about summary refs log tree commit diff
path: root/pkgs/stdenv/mingw/simple-stdenv/builder.sh
blob: 0cbc13e4fab33222577213f10f002199534cf24c (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
75
76
77
78
79
80
if test -z "$out"; then
  out="$OUT"
  initialPath="$INITIALPATH"
  shell="$SHELL"
fi

setupPath=
for i in $initialPath; do
  setupPath=$setupPath${setupPath:+:}$i
done

PATH=$setupPath
export PATH

mkdir $out

cat > $out/setup <<EOF
PATH=$setupPath
export PATH

SHELL=$shell
export SHELL

# make fetchurl usable
header() {
  echo "\$1"
}

stopNest() {
  echo "Nothing to do"
}

# !!! Awful copy&paste.
substitute() {
    local input="\$1"
    local output="\$2"

    local -a params=("\$@")

    local sedScript=\$NIX_BUILD_TOP/.sedargs
    rm -f \$sedScript
    touch \$sedScript

    local n p pattern replacement varName
    
    for ((n = 2; n < \${#params[*]}; n += 1)); do
        p=\${params[\$n]}

        if test "\$p" = "--replace"; then
            pattern=\${params[\$((n + 1))]}
            replacement=\${params[\$((n + 2))]}
            n=\$((n + 2))
            echo "s^\$pattern^\$replacement^g" >> \$sedScript
            sedArgs=("\${sedArgs[@]}" "-e" )
        fi

        if test "\$p" = "--subst-var"; then
            varName=\${params[\$((n + 1))]}
            n=\$((n + 1))
            echo "s^@\${varName}@^\${!varName}^g" >> \$sedScript
        fi

        if test "\$p" = "--subst-var-by"; then
            varName=\${params[\$((n + 1))]}
            replacement=\${params[\$((n + 2))]}
            n=\$((n + 2))
            echo "s^@\${varName}@^\$replacement^g" >> \$sedScript
        fi

    done

    sed -f \$sedScript < "\$input" > "\$output".tmp
    if test -x "\$output"; then
        chmod +x "\$output".tmp
    fi
    mv -f "\$output".tmp "\$output"
}
EOF

chmod +x $out/setup