1#!/bin/bash
2
3pushd $(dirname "$0") > /dev/null
4D=$(pwd -P)
5popd > /dev/null
6
7BASED="$D/.build"
8HOMED="$D/.home"
9
10SOURCED="$HOMED/source"
11INSTALLD="$HOMED/.local"
12
13GTK_OSX_BASE_URL="https://git.gnome.org/browse/gtk-osx/plain/"
14JHBUILD_SOURCED="$SOURCED/jhbuild"
15
16ME="$0"
17
18if [ -z "$GEDIT_SDK" ]; then
19	export GEDIT_SDK=10.7
20fi
21
22export PATH="$INSTALLD/bin:$PATH"
23
24function do_exit() {
25	printf "$@"
26	echo ""
27	exit 1
28}
29
30if [ ! which git &>/dev/null ]; then
31	do_exit "You need to have git installed to build gedit for OS X"
32fi
33
34if [ ! xcodebuild -version &>/dev/null ]; then
35	do_exit "You need to have Xcode installed to build gedit for OS X"
36fi
37
38xcodebase=$(xcode-select -p)
39sdkdir="$xcodebase/Platforms/MacOSX.platform/Developer/SDKs/MacOSX$GEDIT_SDK.sdk"
40
41if [ ! -d "$sdkdir" ]; then
42	do_exit "SDK base directory for $GEDIT_SDK not found. Please make sure to install the SDK at $sdkdir"
43fi
44
45function checkout_jhbuild() {
46	#JHBUILD_REVISION=$(curl -ks "$GTK_OSX_BASE_URL/jhbuild-revision")
47
48	#if test x"$JHBUILD_REVISION" = x; then
49	#	do_exit "Could not find jhbuild revision to use."
50	#fi
51
52	mkdir -p $(dirname "$JHBUILD_SOURCED")
53
54	git clone https://gitlab.gnome.org/GNOME/jhbuild.git "$JHBUILD_SOURCED" || do_exit "Failed to clone jhbuild."
55	#git --work-tree "$JHBUILD_SOURCED" checkout -b stable $JHBUILD_REVISION || do_exit "Failed to checkout stable jhbuild revision."
56}
57
58function build_jhbuild() {
59	echo "Building jhbuild..."
60	(cd "$JHBUILD_SOURCED" && ./autogen.sh --prefix="$INSTALLD" && make -f Makefile.plain DISABLE_GETTEXT=1 install) >/dev/null || do_exit "Jhbuild installation failed";
61
62	rm -f "$INSTALLD/bin/python2"
63
64	cat << PYTHON2 > "$INSTALLD/bin/python2"
65#!/bin/bash
66
67exec /usr/bin/python "\$@"
68PYTHON2
69	chmod +x "$INSTALLD/bin/python2"
70}
71
72function setup_jhbuildrc() {
73	echo "Installing jhbuild configuration..."
74
75	mkdir -p "$HOMED"
76    curl -ks "$GTK_OSX_BASE_URL/jhbuildrc-gtk-osx" | \
77    	sed -e 's/^.*PYTHONPATH.*$//g' -e 's/^.*PYTHON_SITE_PACKAGES.*$//g' > "$HOMED/.jhbuildrc" || do_exit "Failed to get jhbuildrc."
78
79    rm -f "$HOMED/.jhbuildrc-gedit"
80    ln -s "$D/config/jhbuildrc-gedit" "$HOMED/.jhbuildrc-gedit" || exit 1
81}
82
83function init_help_short() {
84	echo "Initialize the in-tree build environment (installs jhbuild)"
85}
86
87function cmd_init() {
88	mkdir -p "$SOURCED"
89
90	if [ ! -d "$JHBUILD_SOURCED" ]; then
91		checkout_jhbuild
92	fi
93
94	build_jhbuild
95	setup_jhbuildrc
96
97	rm -f "$BASED/$GEDIT_SDK/source/gedit"
98	mkdir -p "$BASED/$GEDIT_SDK/source"
99	ln -s $(cd "$D/../../" && pwd) "$BASED/$GEDIT_SDK/source/gedit"
100}
101
102function bootstrap_help_short() {
103	echo "Run jhbuild bootstrap"
104}
105
106function cmd_bootstrap() {
107	# Built python once
108	cmd_jh bootstrap -q python || exit 1
109
110	# Built python twice! There is a bug where python somehow manages to link
111	# its main binary to the system framework library, which then doesn't work...
112	# Building python again seems to resolve the issue...
113	cmd_jh bootstrap -q -f -t python python || exit 1
114
115	# Bootstrap all the rest
116	cmd_jh bootstrap -q || exit 1
117}
118
119function jh_help_usage() {
120	echo "jhbuild-command ..."
121}
122
123function jh_help_short() {
124	echo "Run jhbuild commands"
125}
126
127function cmd_jh() {
128	# Setup our jhbuild environment
129	export GEDIT_OSX_SOURCE_BASE="$D"
130	export GEDIT_OSX_BUILD_BASE="$BASED"
131	export __GEDIT_OSX_OLD_HOME="$HOME"
132	export HOME="$HOMED"
133	export JHB=gedit
134
135	# We set this because without it, GNU gettext intltool or whatever the cause
136	# may be, will set DATADIRNAME=lib during configure because gettext is not part
137	# of libc on OS X. The result is that translations would be installed in lib/
138	# instead of share/ and would not get picked up.
139	export DATADIRNAME=share
140
141	"$INSTALLD/bin/jhbuild" -f "$HOMED/.jhbuildrc" "$@"
142}
143
144function dbg_help_usage() {
145	echo "build command ..."
146}
147
148function dbg_help_short() {
149	echo "Run remaining build commands in a debug environment"
150}
151
152function cmd_dbg() {
153	export GEDIT_OSX_DEBUG=1
154	_process "$@"
155}
156
157function shell_help_short() {
158	echo "Start the jhbuild shell (shorthand for jh shell)"
159}
160
161function cmd_shell() {
162	cmd_jh shell
163}
164
165function make_help_usage() {
166	echo "module-source-directory"
167}
168
169function make_help_short() {
170	echo "Run jhbuild make at the provided source directory"
171}
172
173function cmd_make() {
174	cd "$BASED/$GEDIT_SDK/source/$1" && cmd_jh make
175}
176
177function run_help_usage() {
178	echo "program ..."
179}
180
181function run_help_short() {
182	echo "Run the provided program in the jhbuild environment (shorthand for jh run)"
183}
184
185function cmd_run() {
186	cmd_jh run "$@"
187}
188
189function env_help_short() {
190	echo "Obtain certain environment paths"
191}
192
193function cmd_env() {
194	vars=(source inst bin home local-bin)
195
196	case "$1" in
197	source)
198		echo "$BASED/$GEDIT_SDK/source"
199		;;
200	inst)
201		echo "$BASED/$GEDIT_SDK/inst"
202		;;
203	bin)
204		echo "$BASED/$GEDIT_SDK/inst/bin"
205		;;
206	home)
207		echo "$HOMED"
208		;;
209	local-bin)
210		echo "$HOMED/.local/bin"
211		;;
212	"")
213		for v in "${vars[@]}"; do
214			u=$(echo -n "$v" | tr '-' '_')
215			echo -n "$u="
216			cmd_env "$v"
217		done
218		;;
219	*)
220		varnames=$(printf ", \033[1m%s\033[0m" "${vars[@]}")
221		varnames=${varnames:2}
222
223		do_exit "Unknown environment variable \033[1m$1\033[0m, available variables are: $varnames"
224		;;
225	esac
226}
227
228function all_help_short() {
229	echo "Runs the init, bootstrap and build commands"
230}
231
232function cmd_all() {
233	if [ ! -d "$JHBUILD_SOURCED" ]; then
234		cmd_init || exit 1
235	fi
236
237	if [ ! -f "$BASED/$GEDIT_SDK/inst/bin/python" ]; then
238		cmd_bootstrap || exit 1
239	fi
240
241	cmd_jh build -q
242}
243
244function help_help_short() {
245	echo "Shows this help message"
246}
247
248function cmd_help() {
249	if [ -z "$1" ]; then
250		echo "Usage: $ME [command]"
251		echo ""
252		echo "Available commands:"
253		echo ""
254
255		for cmd in "${commands[@]}"; do
256			printf "  \033[1m$cmd\x1B[0m "
257
258			l=${#cmd}
259			let d="$commandsmaxlen - $l + 1"
260
261			printf "%${d}s" ""
262			echo -n "- "
263
264			if [[ $(type -t "${cmd}_help_short") = "function" ]]; then
265				"${cmd}_help_short"
266			else
267				echo ""
268			fi
269		done
270
271		echo ""
272	else
273		cmd="cmd_$1"
274
275		if [[ $(type -t "$cmd") != "function" ]]; then
276			do_exit "Invalid command \033[1m$1\033[0m, available commands are: $cmds"
277		fi
278
279		printf "Usage: $ME \033[1m$1\033[0m "
280
281		if [[ $(type -t "$1_help_usage") = "function" ]]; then
282			"$1_help_usage"
283		else
284			echo ""
285		fi
286
287		echo ""
288
289		if [[ $(type -t "$1_help_long") != "function" ]]; then
290			"$1_help_short"
291		else
292			"$1_help_long"
293		fi
294	fi
295}
296
297commands=()
298commandsmaxlen=0
299
300while read line
301do
302	cmd=${line#declare -f }
303
304	if [[ "$cmd" = cmd_* ]]; then
305		cname=${cmd#cmd_}
306
307		commands+=($cname)
308
309		l=${#cname}
310
311		if [[ $l > $commandsmaxlen ]]; then
312			commandsmaxlen=$l
313		fi
314	fi
315done < <(declare -F)
316
317cmds=$(printf ", \033[1m%s\033[0m" "${commands[@]}")
318cmds=${cmds:2}
319
320function _process() {
321	if [ -z "$1" ]; then
322		cmd_help
323		exit 0
324	fi
325
326	cmd="cmd_$1"
327
328	if [[ $(type -t "$cmd") != "function" ]]; then
329		do_exit "Invalid command $1, available commands are: $cmds"
330	fi
331
332	shift 1
333	"$cmd" "$@"
334}
335
336_process "$@"
337