forked from Open-Wine-Components/umu-launcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configure.sh
executable file
·189 lines (163 loc) · 4.64 KB
/
configure.sh
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
#!/usr/bin/env bash
set -eu
# Output helpers
COLOR_ERR=""
COLOR_STAT=""
COLOR_INFO=""
COLOR_CMD=""
COLOR_CLEAR=""
if [[ $(tput colors 2>/dev/null || echo 0) -gt 0 ]]; then
COLOR_ERR=$'\e[31;1m'
COLOR_STAT=$'\e[32;1m'
COLOR_INFO=$'\e[30;1m'
COLOR_CMD=$'\e[93;1m'
COLOR_CLEAR=$'\e[0m'
fi
sh_quote() {
local quoted
quoted="$(printf '%q ' "$@")"; [[ $# -eq 0 ]] || echo "${quoted:0:-1}";
}
err() { echo >&2 "${COLOR_ERR}!!${COLOR_CLEAR} $*"; }
stat() { echo >&2 "${COLOR_STAT}::${COLOR_CLEAR} $*"; }
info() { echo >&2 "${COLOR_INFO}::${COLOR_CLEAR} $*"; }
showcmd() { echo >&2 "+ ${COLOR_CMD}$(sh_quote "$@")${COLOR_CLEAR}"; }
die() { err "$@"; exit 1; }
finish() { stat "$@"; exit 0; }
cmd() { showcmd "$@"; "$@"; }
#
# Configure
#
THIS_COMMAND="$0 $*" # For printing, not evaling
MAKEFILE="./Makefile"
# This is not rigorous. Do not use this for untrusted input. Do not. If you need a version of
# this for untrusted input, rethink the path that got you here.
function escape_for_make() {
local escape="$1"
escape="${escape//\\/\\\\}" # '\' -> '\\'
escape="${escape//#/\\#}" # '#' -> '\#'
escape="${escape//\$/\$\$}" # '$' -> '$$'
escape="${escape// /\\ }" # ' ' -> '\ '
echo "$escape"
}
function configure() {
## Checks before writing config
if [[ -n "$arg_user_install" ]]; then
arg_prefix="$HOME/.local"
fi
if [[ $arg_prefix != $(realpath "$arg_prefix") ]]; then
die "PREFIX needs to be an absolute path"
fi
## Write out config
[[ ! -e "$MAKEFILE" ]] || rm "$MAKEFILE"
{
# Config
echo "# Generated by: $THIS_COMMAND"
echo ""
if [[ -n "$arg_user_install" ]]; then
echo "USERINSTALL := xtrue"
echo "INSTALLDIR := umu-launcher"
fi
# Prefix was specified, baking it into the Makefile
if [[ -n $arg_prefix ]]; then
echo "PREFIX := $(escape_for_make "$arg_prefix")"
fi
# Include base
echo ""
echo "include Makefile.in"
} >> "$MAKEFILE"
stat "Created $MAKEFILE, now run \"make\" to build."
}
#
# Parse arguments
#
arg_prefix=""
arg_user_install=""
arg_help=""
function parse_args() {
local arg;
local val;
local val_used;
local val_passed;
if [[ $# -eq 0 ]]; then
return 1
fi
while [[ $# -gt 0 ]]; do
arg="$1"
val=''
val_used=''
val_passed=''
if [[ -z $arg ]]; then # Sanity
err "Unexpected empty argument"
return 1
elif [[ ${arg:0:2} != '--' ]]; then
err "Unexpected positional argument ($1)"
return 1
fi
# Looks like an argument does it have a --foo=bar value?
if [[ ${arg%=*} != "$arg" ]]; then
val="${arg#*=}"
arg="${arg%=*}"
val_passed=1
else
# Otherwise for args that want a value, assume "--arg val" form
val="${2:-}"
fi
# The args
if [[ $arg = --help || $arg = --usage ]]; then
arg_help=1
elif [[ $arg = --prefix ]]; then
if [[ -n $arg_user_install ]]; then
die "--prefix cannot be used with --user-install"
fi
arg_prefix="$val"
val_used=1
elif [[ $arg = --user-install ]]; then
if [[ -n $arg_prefix ]]; then
die "--user-install cannot be used with --prefix"
fi
arg_user_install="1"
else
err "Unrecognized option $arg"
return 1
fi
# Check if this arg used the value and shouldn't have or vice-versa
if [[ -n $val_used && -z $val_passed ]]; then
# "--arg val" form, used $2 as the value.
# Don't allow this if it looked like "--arg --val"
if [[ ${val#--} != "$val" ]]; then
err "Ambiguous format for argument with value \"$arg $val\""
err " (use $arg=$val or $arg='' $val)"
return 1
fi
# Error if this was the last positional argument but expected $val
if [[ $# -le 1 ]]; then
err "$arg takes a parameter, but none given"
return 1
fi
shift # consume val
elif [[ -z $val_used && -n $val_passed ]]; then
# Didn't use a value, but passed in --arg=val form
err "$arg does not take a parameter"
return 1
fi
shift # consume arg
done
}
usage() {
"$1" "Usage: $0 { --prefix=path }"
"$1" " Generate a Makefile for building umu"
"$1" ""
"$1" " Options"
"$1" " --help"
"$1" " --usage Show this help text and exit"
"$1" ""
"$1" " --prefix=PREFIX Install architecture-independent files in PREFIX"
"$1" " [/usr]"
"$1" " --user-install Install under user-only location. Incompatible with --prefix"
"$1" " [$HOME/.local]"
"$1" ""
exit 1;
}
parse_args "$@" || usage err
[[ -z $arg_help ]] || usage info
configure