perl-functions.eclass: should 'just work' in EAPI=6
[gentoo.git] / eclass / games-mods.eclass
1 # Copyright 1999-2011 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3 # $Id$
4
5 # Variables to specify in an ebuild which uses this eclass:
6 # GAME - (doom3, quake4 or ut2004, etc), unless ${PN} starts with e.g. "doom3-"
7 # MOD_DESC - Description for the mod
8 # MOD_NAME - Creates a command-line wrapper and desktop icon for the mod
9 # MOD_DIR - Subdirectory name for the mod, if applicable
10 # MOD_ICON - Custom icon for the mod, instead of the default
11
12 inherit eutils games
13
14 EXPORT_FUNCTIONS src_install pkg_postinst
15
16 [[ -z ${GAME} ]] && GAME=${PN%%-*}
17
18 case ${GAME} in
19         doom3)
20                 GAME_PKGS="games-fps/doom3"
21                 GAME_DIRS=( "${GAMES_PREFIX_OPT}"/doom3 )
22                 GAME_NAME="Doom 3"
23                 GAME_BIN="doom3"
24                 GAME_ICON="doom3"
25                 DED_PKGS=""
26                 DED_BIN="doom3-ded"
27                 DED_OPTS="+set dedicated 1 +exec server.cfg"
28                 DED_CFG_DIR=".doom3"
29                 SELECT_MOD="+set fs_game "
30                 ;;
31         enemy-territory)
32                 GAME_PKGS="games-fps/enemy-territory"
33                 GAME_DIRS=( "${GAMES_PREFIX_OPT}"/enemy-territory )
34                 GAME_NAME="Enemy Territory"
35                 GAME_BIN="et"
36                 GAME_ICON="ET"
37                 DED_PKGS=""
38                 DED_BIN="et-ded"
39                 DED_OPTS="+set dedicated 1 +exec server.cfg"
40                 DED_CFG_DIR=".etwolf"
41                 SELECT_MOD="+set fs_game "
42                 ;;
43         quake3)
44                 GAME_PKGS="games-fps/quake3 games-fps/quake3-bin"
45                 GAME_DIRS=( "${GAMES_DATADIR}"/quake3 "${GAMES_PREFIX_OPT}"/quake3 )
46                 GAME_NAME="Quake III"
47                 GAME_BIN="quake3"
48                 GAME_ICON="quake3"
49                 DED_PKGS=""
50                 DED_BIN="quake3-ded"
51                 DED_OPTS="+set dedicated 1 +exec server.cfg"
52                 DED_CFG_DIR=".q3a"
53                 SELECT_MOD="+set fs_game "
54                 ;;
55         quake4)
56                 GAME_PKGS="games-fps/quake4-bin"
57                 GAME_DIRS=( "${GAMES_PREFIX_OPT}"/quake4 )
58                 GAME_NAME="Quake 4"
59                 GAME_BIN="quake4"
60                 GAME_ICON="/usr/share/pixmaps/quake4.bmp"
61                 DED_PKGS=""
62                 DED_BIN="quake4-ded"
63                 DED_OPTS="+set dedicated 1 +exec server.cfg"
64                 DED_CFG_DIR=".quake4"
65                 SELECT_MOD="+set fs_game "
66                 ;;
67         ut2003)
68                 GAME_PKGS="games-fps/ut2003"
69                 GAME_DIRS=( "${GAMES_PREFIX_OPT}"/ut2003 )
70                 GAME_NAME="UT2003"
71                 GAME_BIN="ut2003"
72                 GAME_ICON="ut2003"
73                 DED_PKGS=""
74                 DED_BIN="ucc"
75                 DED_OPTS=""
76                 DED_CFG_DIR=""
77                 SELECT_MOD="-mod="
78                 ;;
79         ut2004)
80                 GAME_PKGS="games-fps/ut2004"
81                 GAME_DIRS=( "${GAMES_PREFIX_OPT}"/{ut2004,ut2004-ded} )
82                 GAME_NAME="UT2004"
83                 GAME_BIN="ut2004"
84                 GAME_ICON="ut2004"
85                 DED_PKGS="games-server/ut2004-ded"
86                 DED_BIN="ut2004-ded"
87                 DED_OPTS=""
88                 DED_CFG_DIR=""
89                 SELECT_MOD="-mod="
90                 ;;
91         *)
92                 eerror "This game is either not supported or you must set the GAME"
93                 eerror "variable to the proper game."
94                 die "games-mods.eclass: unsupported GAME"
95                 ;;
96 esac
97
98 MOD_BIN="${GAME_BIN}-${PN/${GAME}-}"
99 MOD_DED_BIN="${MOD_BIN}-ded"
100
101 games-mods_get_rdepend() {
102         local pkgs
103
104         if [[ ${1} == "--ded" ]] ; then
105                 pkgs=( ${DED_PKGS} ${GAME_PKGS} )
106         else
107                 pkgs=( ${GAME_PKGS} )
108         fi
109
110         [[ ${#pkgs[@]} -gt 1 ]] && echo -n "|| ( "
111
112         case ${EAPI:-0} in
113                 0|1) echo -n "${pkgs[@]}" ;;
114                 [23456])
115                         local p
116                         if [[ ${1} == "--ded" ]] ; then
117                                 echo -n "${DED_PKGS}"
118                                 for p in ${GAME_PKGS} ; do
119                                         echo -n " ${p}[dedicated]"
120                                 done
121                         else
122                                 for p in ${GAME_PKGS} ; do
123                                         echo -n " || ( ${p}[opengl] ${p}[-dedicated] )"
124                                 done
125                         fi
126                         ;;
127                 *) die "EAPI ${EAPI} not supported"
128         esac
129
130         [[ ${#pkgs[@]} -gt 1 ]] && echo -n " )"
131 }
132
133 DESCRIPTION="${GAME_NAME} ${MOD_NAME} - ${MOD_DESC}"
134
135 SLOT="0"
136 IUSE="dedicated opengl"
137 RESTRICT="mirror strip"
138
139 DEPEND="app-arch/unzip"
140 RDEPEND="dedicated? ( $(games-mods_get_rdepend --ded) )
141         opengl? ( $(games-mods_get_rdepend) )
142         !dedicated? ( !opengl? ( $(games-mods_get_rdepend) ) )"
143
144 S=${WORKDIR}
145
146 INS_DIR=${GAMES_DATADIR}/${GAME}
147
148 games-mods_use_opengl() {
149         [[ -z ${MOD_DIR} ]] && return 1
150
151         if use opengl || ! use dedicated ; then
152                 # Use opengl by default
153                 return 0
154         fi
155
156         return 1
157 }
158
159 games-mods_use_dedicated() {
160         [[ -z ${MOD_DIR} ]] && return 1
161
162         use dedicated && return 0 || return 1
163 }
164
165 games-mods_dosyms() {
166         # We are installing everything for these mods into ${INS_DIR},
167         # ${GAMES_DATADIR}/${GAME} in most cases, and symlinking it
168         # into ${GAMES_PREFIX_OPT}/${GAME} for each game.  This should
169         # allow us to support both binary and source-based games easily.
170         local dir
171         for dir in "${GAME_DIRS[@]}" ; do
172                 [[ -z ${dir} || ${INS_DIR} == ${dir} ]] && continue
173                 pushd "${D}/${INS_DIR}" > /dev/null || die "pushd failed"
174                 local i
175                 for i in * ; do
176                         if [[ -d ${i} ]] ; then
177                                 if [[ ${i} == ${MOD_DIR} ]] ; then
178                                         dosym "${INS_DIR}/${i}" "${dir}/${i}" \
179                                                 || die "dosym ${i} failed"
180                                 else
181                                         local f
182                                         while read f ; do
183                                                 dosym "${INS_DIR}/${f}" "${dir}/${f}" \
184                                                         || die "dosym ${f} failed"
185                                         done < <(find "${i}" -type f)
186                                 fi
187                         elif [[ -f ${i} ]] ; then
188                                 dosym "${INS_DIR}/${i}" "${dir}/${i}" \
189                                         || die "dosym ${i} failed"
190                         else
191                                 die "${i} shouldn't be there"
192                         fi
193                 done
194                 popd > /dev/null || die "popd failed"
195         done
196 }
197
198 games-mods_make_initd() {
199         cat <<EOF > "${T}"/${MOD_DED_BIN}
200 #!/sbin/runscript
201 $(head -n 2 ${PORTDIR}/header.txt)
202 # Generated by games-mods.eclass
203
204 depend() {
205         need net
206 }
207
208 start() {
209         ebegin "Starting ${MOD_DED_BIN}"
210         start-stop-daemon --start --quiet --background --make-pidfile \\
211                 --pidfile /var/run/${MOD_DED_BIN}.pid \\
212                 --chuid \${${MOD_DED_BIN//-/_}_user}:\${${MOD_DED_BIN//-/_}_group} \\
213                 --env HOME="\${${MOD_DED_BIN//-/_}_home}" \\
214                 --exec "${GAMES_BINDIR}/${MOD_DED_BIN}" \\
215                 -- \${${MOD_DED_BIN//-/_}_opts}
216         eend \$?
217 }
218
219 stop() {
220         ebegin "Stopping ${MOD_DED_BIN}"
221         start-stop-daemon --stop \\
222                 --pidfile /var/run/${MOD_DED_BIN}.pid
223         eend \$?
224 }
225 EOF
226
227         doinitd "${T}"/${MOD_DED_BIN} || die "doinitd failed"
228 }
229
230 games-mods_make_confd() {
231         cat <<-EOF > "${T}"/${MOD_DED_BIN}
232         # User and group the server should run as
233         ${MOD_DED_BIN//-/_}_user="${GAMES_USER_DED}"
234         ${MOD_DED_BIN//-/_}_group="${GAMES_GROUP}"
235
236         # Directory to use for HOME
237         ${MOD_DED_BIN//-/_}_home="${GAMES_PREFIX}"
238
239         # Any extra options you want to pass to the dedicated server
240         ${MOD_DED_BIN//-/_}_opts=""
241         EOF
242
243         doconfd "${T}"/${MOD_DED_BIN} || die "doconfd failed"
244 }
245
246 games-mods_src_install() {
247         if games-mods_use_opengl ; then
248                 if [[ -n ${MOD_ICON} ]] ; then
249                         # Install custom icon
250                         local ext=${MOD_ICON##*.}
251                         if [[ -f ${MOD_ICON} ]] ; then
252                                 newicon "${MOD_ICON}" ${PN}.${ext} || die "newicon failed"
253                         else
254                                 newicon ${MOD_DIR}/"${MOD_ICON}" ${PN}.${ext} \
255                                         || die "newicon failed"
256                         fi
257                         case ${ext} in
258                                 bmp|ico)
259                                         MOD_ICON=/usr/share/pixmaps/${PN}.${ext}
260                                         ;;
261                                 *)
262                                         MOD_ICON=${PN}
263                                         ;;
264                         esac
265                 else
266                         # Use the game's standard icon
267                         MOD_ICON=${GAME_ICON}
268                 fi
269
270                 games_make_wrapper ${MOD_BIN} "${GAME_BIN} ${SELECT_MOD}${MOD_DIR}"
271                 make_desktop_entry ${MOD_BIN} "${GAME_NAME} - ${MOD_NAME}" "${MOD_ICON}"
272                 # Since only quake3 has both a binary and a source-based install,
273                 # we only look for quake3 here.
274                 case ${GAME} in
275                         quake3)
276                                 if has_version games-fps/quake3-bin ; then
277                                         games_make_wrapper ${GAME_BIN}-bin-${PN/${GAME}-} \
278                                                 "${GAME_BIN}-bin ${SELECT_MOD}${MOD_DIR}"
279                                 fi
280                                 make_desktop_entry ${GAME_BIN}-bin-${PN/${GAME}-} \
281                                         "${GAME_NAME} - ${MOD_NAME} (binary)" "${MOD_ICON}"
282                                 ;;
283                 esac
284         fi
285
286         # We expect anything not wanted to have been deleted by the ebuild
287         insinto "${INS_DIR}"
288         doins -r * || die "doins -r failed"
289         games-mods_dosyms
290
291         if games-mods_use_dedicated ; then
292                 if [[ -f ${FILESDIR}/server.cfg ]] ; then
293                         insinto "${GAMES_SYSCONFDIR}"/${GAME}/${MOD_DIR}
294                         doins "${FILESDIR}"/server.cfg || die "doins server.cfg failed"
295                         dosym "${GAMES_SYSCONFDIR}"/${GAME}/${MOD_DIR}/server.cfg \
296                                 "${GAMES_PREFIX}"/${DED_CFG_DIR}/${MOD_DIR}/server.cfg \
297                                 || die "dosym server.cfg failed"
298                 fi
299                 games_make_wrapper ${MOD_DED_BIN} \
300                         "\"${GAMES_BINDIR}/${DED_BIN}\" ${SELECT_MOD}${MOD_DIR} ${DED_OPTS}"
301                 games-mods_make_initd
302                 games-mods_make_confd
303         fi
304
305         prepgamesdirs
306 }
307
308 games-mods_pkg_postinst() {
309         games_pkg_postinst
310         if games-mods_use_opengl ; then
311                 elog "To play this mod run:"
312                 elog "  ${MOD_BIN}"
313         fi
314         if games-mods_use_dedicated ; then
315                 elog "To launch a dedicated server run:"
316                 elog "  ${MOD_DED_BIN}"
317                 elog "To launch the server at startup run:"
318                 elog "  rc-update add ${MOD_DED_BIN} default"
319         fi
320 }