summaryrefslogtreecommitdiff
blob: 7d5113826d4ce662f39cca0d9ffa9631d1da8f0e (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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
# Copyright 1999-2009 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: /var/cvsroot/gentoo-x86/eclass/games-mods.eclass,v 1.34 2009/10/06 13:37:42 nyhm Exp $

# Variables to specify in an ebuild which uses this eclass:
# GAME - (doom3, quake4 or ut2004, etc), unless ${PN} starts with e.g. "doom3-"
# MOD_BINS - Name of the binary to run
# MOD_DESC - Description for the mod
# MOD_DIR - Subdirectory name for the mod, if applicable
# MOD_ICON - Custom icon for the mod, instead of the default
# MOD_NAME - Creates a command-line wrapper and desktop icon for the mod
# MOD_TBZ2 - File to extract within the Makeself archive

inherit eutils games

EXPORT_FUNCTIONS src_unpack src_install pkg_postinst

[[ -z ${GAME} ]] && GAME=${PN%%-*}

# Set our default title, icon, and cli options
case ${GAME} in
	doom3)
		GAME_PKGS="games-fps/doom3"
		GAME_TITLE="Doom 3"
		DEFAULT_MOD_ICON="doom3"
		SELECT_MOD="+set fs_game "
		GAME_EXE="doom3"
		DED_EXE="doom3-ded"
		DED_OPTIONS="+set dedicated 1 +exec server.cfg"
		DED_CFG_DIR=".doom3"
		;;
	enemy-territory)
		GAME_PKGS="games-fps/enemy-territory"
		GAME_TITLE="Enemy Territory"
		DEFAULT_MOD_ICON="ET"
		SELECT_MOD="+set fs_game "
		GAME_EXE="et"
		DED_EXE="et-ded"
		DED_OPTIONS="+set dedicated 1 +exec server.cfg"
		DED_CFG_DIR=".etwolf"
		;;
	quake3)
		GAME_PKGS="games-fps/quake3 games-fps/quake3-bin"
		GAME_TITLE="Quake III"
		DEFAULT_MOD_ICON="quake3"
		SELECT_MOD="+set fs_game "
		GAME_EXE="quake3"
		DED_EXE="quake3-ded"
		DED_OPTIONS="+set dedicated 1 +exec server.cfg"
		DED_CFG_DIR=".q3a"
		;;
	quake4)
		GAME_PKGS="games-fps/quake4-bin"
		GAME_TITLE="Quake 4"
		DEFAULT_MOD_ICON="/usr/share/pixmaps/quake4.bmp"
		SELECT_MOD="+set fs_game "
		GAME_EXE="quake4"
		DED_EXE="quake4-ded"
		DED_OPTIONS="+set dedicated 1 +exec server.cfg"
		DED_CFG_DIR=".quake4"
		;;
	ut2003)
		GAME_PKGS="games-fps/ut2003"
		GAME_TITLE="UT2003"
		DEFAULT_MOD_ICON="ut2003"
		SELECT_MOD="-mod="
		GAME_EXE="ut2003"
		DED_EXE="ucc"
		DED_OPTIONS=""
		DED_CFG_DIR=""
		;;
	ut2004)
		GAME_PKGS="games-fps/ut2004"
		GAME_TITLE="UT2004"
		DEFAULT_MOD_ICON="ut2004"
		SELECT_MOD="-mod="
		GAME_EXE="ut2004"
		DED_EXE="ut2004-ded"
		DED_OPTIONS=""
		DED_CFG_DIR=""
		;;
	*)
		eerror "This game is either not supported or you must set the GAME"
		eerror "variable to the proper game."
		die "unsupported game"
		;;
esac

games-mods_get_rdepend() {
	[[ $# -lt 1 ]] && die "${FUNCNAME}: need args"
	[[ $# -gt 1 ]] && echo -n "|| ( "

	case ${EAPI:-0} in
		0|1) echo -n $@ ;;
		2)
			local pkg
			for pkg in $@ ; do
				if [[ -z ${MOD_BINS} && -z ${MOD_DIR} ]] ; then
					echo -n " ${pkg}"
				else
					echo -n " ${pkg}[dedicated=,opengl=]"
				fi
			done
			;;
	esac

	[[ $# -gt 1 ]] && echo -n " )"
}

DESCRIPTION="${GAME_TITLE} ${MOD_NAME} - ${MOD_DESC}"

SLOT="0"
RESTRICT="mirror strip"

DEPEND="app-arch/unzip"
RDEPEND="$(games-mods_get_rdepend ${GAME_PKGS})"

S=${WORKDIR}

dir=${GAMES_DATADIR}/${GAME}

games-mods_use_opengl() {
	[[ -z ${MOD_BINS} && -z ${MOD_DIR} ]] && return 1

	if use opengl || ! use dedicated ; then
		# Use opengl by default
		return 0
	fi

	return 1
}

games-mods_use_dedicated() {
	[[ -z ${MOD_BINS} && -z ${MOD_DIR} ]] && return 1

	use dedicated && return 0 || return 1
}

games-mods_src_unpack() {
	# The first thing we do here is determine exactly what we're dealing with
	for src_uri in ${A} ; do
		URI_SUFFIX="${src_uri##*.}"
		case ${URI_SUFFIX##*.} in
			bin|run)
				# We have a Makeself archive, use unpack_makeself
				unpack_makeself "${src_uri}"
				# Since this is a Makeself archive, it has a lot of useless
				# files (for us), so we delete them.
				rm -rf setup.data setup.sh uninstall
				;;
			bz2|gz|Z|z|ZIP|zip)
				# We have a normal tarball/zip file, use unpack
				unpack "${src_uri}"
				;;
		esac
	done

	# This code should only be executed for Makeself archives
	for tarball in ${MOD_TBZ2} ; do
		mkdir -p "${S}"/unpack
		for name in "${tarball}_${PV}-english" "${tarball}_${PV}" "${tarball}" ; do
			for ext in tar.bz2 tar.gz tbz2 tgz ; do
				if [[ -e "${name}.${ext}" ]] ; then
					tar xf "${name}.${ext}" -C "${S}"/unpack \
						|| die "uncompressing tarball"
					# Remove the tarball after we unpack it
					rm -f "${name}.${ext}"
				fi
			done
		done
	done
	# Since we remove all of these anyway, let's move it to the eclass
	rm -f 3355_patch 3339_patch
}

games-mods_src_install() {
	local readme MOD_ICON_EXT new_bin_name bin_name mod files directories i j
	INS_DIR=${dir}

	# We check if we have a specific MOD_DIR listed
	if [[ -n "${MOD_DIR}" ]] ; then
		# Am installing into a new subdirectory of the game
		if [[ -d "${S}"/unpack/"${MOD_DIR}" ]] ; then
			INS_DIR=${dir}
		elif [[ -d "${S}"/"${MOD_DIR}" ]] ; then
			S=${WORKDIR}/${MOD_DIR}
			INS_DIR=${dir}/${MOD_DIR}
		fi
	fi

	cd "${S}"

	# If we have a README, install it
	for readme in README* ; do
		if [[ -s "${readme}" ]] ; then
			dodoc "${readme}" || die "dodoc failed"
		fi
	done

	if games-mods_use_opengl ; then
		if [[ -n "${MOD_ICON}" ]] ; then
			# Install custom icon
			MOD_ICON_EXT=${MOD_ICON##*.}
			newicon "${MOD_ICON}" "${PN}.${MOD_ICON_EXT}"
			case ${MOD_ICON_EXT} in
				bmp|ico)
					MOD_ICON=/usr/share/pixmaps/${PN}.${MOD_ICON_EXT}
					;;
				*)
					MOD_ICON=${PN}
					;;
			esac
		else
			# Use the game's standard icon
			MOD_ICON=${DEFAULT_MOD_ICON}
		fi

		# Set up command-line and desktop menu entries
		if [[ -n "${MOD_BINS}" ]] ; then
			for binary in ${MOD_BINS} ; do
				if [[ -n "${MOD_DIR}" ]] ; then
					games_make_wrapper "${GAME_EXE}-${MOD_BINS}" \
						"${GAME_EXE} ${SELECT_MOD}${MOD_DIR}" "${dir}" "${dir}"
					make_desktop_entry "${GAME_EXE}-${MOD_BINS}" \
						"${GAME_TITLE} - ${MOD_NAME}" "${MOD_ICON}"
				elif [[ -e "${S}"/bin/"${binary}" ]] ; then
					exeinto "${dir}"
					newexe bin/${binary} ${GAME_EXE}-${binary} \
						|| die "newexe failed"
					new_bin_name=
					bin_name=$(echo ${binary} | sed -e 's:[-_.]: :g')
					# We want our wrapper to use the libraries/starting
					# directory of our game.  If the game is in
					# GAMES_PREFIX_OPT, then we want to start there.
					if [[ -d "${GAMES_PREFIX_OPT}"/${GAME} ]] ; then
						GAME_DIR="${GAMES_PREFIX_OPT}/${GAME}"
					else
						GAME_DIR="${dir}"
					fi
					games_make_wrapper "${GAME_EXE}-${binary}" \
						./"${GAME_EXE}-${binary}" "${GAME_DIR}" "${GAME_DIR}"
					if [[ "${bin_name}" == "${binary}" ]] ; then
						bin_name=${MOD_NAME}
					else
						for tmp1 in ${bin_name} ; do
							tmp2=$(echo ${tmp1} | cut -b1 | tr [[:lower:]] \
								[[:upper:]])
							tmp3=$(echo ${tmp1} | cut -b2-)
							new_bin_name="${new_bin_name} ${tmp2}${tmp3}"
						done
						new_bin_name=$(echo ${new_bin_name} | cut -b1-)
						bin_name="${MOD_NAME} (${new_bin_name})"
					fi
					make_desktop_entry "${GAME_EXE}-${binary}" \
						"${GAME_TITLE} - ${bin_name}" "${MOD_ICON}"
					# We remove the binary after we have installed it.
					rm -f bin/${binary}
				fi
			done
			# We don't want to leave the binary directory around
			rm -rf bin
		elif [[ -n "${MOD_DIR}" ]] ; then
			games_make_wrapper "${GAME_EXE}-${PN/${GAME}-}" \
				"${GAME_EXE} ${SELECT_MOD}${MOD_DIR}" "${dir}" "${dir}"
			make_desktop_entry "${GAME_EXE}-${PN/${GAME}-}" \
				"${GAME_TITLE} - ${MOD_NAME}" "${MOD_ICON}"
			# Since only quake3 has both a binary and a source-based install,
			# we only look for quake3 here.
			case "${GAME_EXE}" in
				"quake3")
					if has_version games-fps/quake3-bin ; then
						games_make_wrapper "${GAME_EXE}-bin-${PN/${GAME}-}" \
							"${GAME_EXE}-bin ${SELECT_MOD}${MOD_DIR}" \
							"${dir}" "${dir}"
					fi
					make_desktop_entry "${GAME_EXE}-bin-${PN/${GAME}-}" \
						"${GAME_TITLE} - ${MOD_NAME} (binary)" \
						"${MOD_ICON}"
				;;
			esac
		fi
	fi

	# Copy our unpacked files, if it exists
	if [[ -d "${S}"/unpack ]] ; then
		insinto "${INS_DIR}"
		doins -r "${S}"/unpack/* || die "copying files"
		rm -rf "${S}"/unpack
	fi

	# We expect anything not wanted to have been deleted by the ebuild
	if [[ ! -z $(ls "${S}"/* 2> /dev/null) ]] ; then
		insinto "${INS_DIR}"
		doins -r * || die "doins -r failed"
	fi

	# We are installing everything for these mods into ${INS_DIR}, which should
	# be ${GAMES_DATADIR}/${GAME}/${MOD_DIR} in most cases, and symlinking it
	# into ${GAMES_PREFIX_OPT}/${GAME}/${MOD_DIR} for each game.  This should
	# allow us to support both binary and source-based games easily.
	if [[ -d "${GAMES_PREFIX_OPT}"/"${GAME}" ]] && \
		[[ "${GAMES_PREFIX_OPT}" != "${GAMES_DATADIR}" ]] ; then
		dodir "${GAMES_PREFIX_OPT}"/"${GAME}"
		mod=$(echo "${INS_DIR}" | sed -e "s:${GAMES_DATADIR}/${GAME}::" -e "s:^/::" )
		if [[ -z "${mod}" ]] ; then
			# Our mod doesn't have its own directory.  We now traverse the
			# directory structure and try to symlink everything to
			# GAMES_PREFIX_OPT/GAME so it'll work.
			directories=$(cd "${D}"/"${INS_DIR}";find . -maxdepth 1 -type d -printf '%P ')
			for i in ${directories} ; do
				if [[ -h "${GAMES_PREFIX_OPT}"/"${GAME}"/${i} ]] ; then
					# Skip this directory, and just run a symlink
					dosym "${INS_DIR}"/${i} \
						"${GAMES_PREFIX_OPT}"/"${GAME}"/${i} || die
				elif [[ -d "${GAMES_PREFIX_OPT}"/"${GAME}"/${i} ]] ; then
					dodir "${GAMES_PREFIX_OPT}"/"${GAME}"/${i}
					cd "${D}"/"${INS_DIR}"/${i}
					files="$(find . -type f -printf '%P ')"
					for j in ${files} ; do
						if has_version ${CATEGORY}/${PN} ; then
							dosym "${INS_DIR}"/${i}/${j} \
								"${GAMES_PREFIX_OPT}"/"${GAME}"/${i}/${j} \
								|| die
						elif [[ ! -e "${GAMES_PREFIX_OPT}"/"${GAME}"/${i}/${j} ]] ; then
							dosym "${INS_DIR}"/${i}/${j} \
								"${GAMES_PREFIX_OPT}"/"${GAME}"/${i}/${j} \
								|| die
						fi
					done
				else
					# Skip this directory, and just run a symlink
					dosym "${INS_DIR}"/${i} \
						"${GAMES_PREFIX_OPT}"/"${GAME}"/${i} || die
				fi
			done
			files=$(cd "${D}"/"${INS_DIR}";find . -maxdepth 1 -type f -printf '%P ')
			for i in ${files} ; do
				# Why don´t we use symlinks? Because these use ./$bin when
				# they run and that doesn't work if the binary is in
				# GAMES_PREFIX_OPT but the mod is in GAMES_DATADIR.
				#	dosym "${INS_DIR}"/${i} \
				#		"${GAMES_PREFIX_OPT}"/"${GAME}"/${i} || die
					cp -a "${D}"/"${INS_DIR}"/${i} \
						${D}/"${GAMES_PREFIX_OPT}"/"${GAME}"/${i} || die
			done
		elif [[ ! -f "${GAMES_PREFIX_OPT}"/"${GAME}"/${mod} ]] ; then
			elog "Creating symlink for ${mod}"
			dosym "${INS_DIR}" "${GAMES_PREFIX_OPT}"/"${GAME}" || die
		fi
	fi

	if games-mods_use_dedicated ; then
		if [[ -f ${FILESDIR}/server.cfg ]] ; then
			insinto "${GAMES_SYSCONFDIR}"/${GAME}/${MOD_DIR}
			doins "${FILESDIR}"/server.cfg || die "Copying server config"
			dodir "${GAMES_PREFIX}"/${DED_CFG_DIR}/${MOD_DIR}
			dosym "${GAMES_SYSCONFDIR}"/${GAME}/${MOD_DIR}/server.cfg \
				"${GAMES_PREFIX}"/${DED_CFG_DIR}/${MOD_DIR}/server.cfg
		fi
		games_make_wrapper \
			${GAME_EXE}-${PN/${GAME}-}-ded \
			"${DED_EXE} ${SELECT_MOD}${MOD_DIR} ${DED_OPTIONS}" 
		games-mods_make_initd
		games-mods_make_confd
	fi

	prepgamesdirs
}

games-mods_pkg_postinst() {
	games_pkg_postinst
	if games-mods_use_opengl ; then
		if [[ -n "${MOD_BINS}" ]] ; then
			for binary in ${MOD_BINS} ; do
				elog "To play this mod run:"
				elog " ${GAME_EXE}-${binary}"
				echo
			done
		elif [[ -n "${MOD_DIR}" ]] ; then
			elog "To play this mod run:"
			elog " ${GAME_EXE}-${PN/${GAME}-}"
			echo
		fi
	fi
	if games-mods_use_dedicated ; then
		elog "To launch a dedicated server run:"
		elog " ${GAME_EXE}-${PN/${GAME}-}-ded"
		echo
		elog "To launch server at startup run:"
		elog " rc-update add ${GAME_EXE}-${PN/${GAME}-}-ded default"
		echo
	fi
}

games-mods_make_initd() {
	cat <<EOF > "${T}"/${GAME_EXE}-${PN/${GAME}-}-ded
#!/sbin/runscript
$(head -n 2 ${PORTDIR}/header.txt)
# Generated by games-mods.eclass

depend() {
	need net
}

start() {
	ebegin "Starting ${GAME_TITLE} ${MOD_NAME} dedicated server"
	start-stop-daemon --start --quiet --background --chuid \\
		${GAMES_USER_DED}:${GAMES_GROUP} --env HOME="${GAMES_PREFIX}" --exec \\
		${GAMES_BINDIR}/${GAME_EXE}-${PN/${GAME}-}-ded -- \\
		\${${GAME_EXE}_${PN/${GAME}-}_opts}
	eend \$?
}

stop() {
	ebegin "Stopping ${GAME_TITLE} ${MOD_NAME} dedicated server"
	start-stop-daemon --stop --quiet --exec \\
		${GAMES_BINDIR}/${GAME_EXE}-${PN/${GAME}-}-ded
	eend \$?
}
EOF

	doinitd "${T}"/${GAME_EXE}-${PN/${GAME}-}-ded || die "doinitd failed"
}

games-mods_make_confd() {
	cat <<-EOF > "${T}"/${GAME_EXE}-${PN/${GAME}-}-ded
	# Any extra options you want to pass to the dedicated server
	${GAME_EXE}_${PN/${GAME}-}_opts=""
	EOF

	doconfd "${T}"/${GAME_EXE}-${PN/${GAME}-}-ded || die "doconfd failed"
}