kde5.eclass: Cleanup obsolete blocker
[gentoo.git] / eclass / games-mods.eclass
1 # Copyright 1999-2017 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3
4 # Variables to specify in an ebuild which uses this eclass:
5 # GAME - (doom3, quake4 or ut2004, etc), unless ${PN} starts with e.g. "doom3-"
6 # MOD_DESC - Description for the mod
7 # MOD_NAME - Creates a command-line wrapper and desktop icon for the mod
8 # MOD_DIR - Subdirectory name for the mod, if applicable
9 # MOD_ICON - Custom icon for the mod, instead of the default
10
11 inherit eutils games
12
13 EXPORT_FUNCTIONS src_install pkg_postinst
14
15 [[ -z ${GAME} ]] && GAME=${PN%%-*}
16
17 case ${GAME} in
18         doom3)
19                 GAME_PKGS="games-fps/doom3"
20                 GAME_DIRS=( "${GAMES_PREFIX_OPT}"/doom3 )
21                 GAME_NAME="Doom 3"
22                 GAME_BIN="doom3"
23                 GAME_ICON="doom3"
24                 DED_PKGS=""
25                 DED_BIN="doom3-ded"
26                 DED_OPTS="+set dedicated 1 +exec server.cfg"
27                 DED_CFG_DIR=".doom3"
28                 SELECT_MOD="+set fs_game "
29                 ;;
30         enemy-territory)
31                 GAME_PKGS="games-fps/enemy-territory"
32                 GAME_DIRS=( "${GAMES_PREFIX_OPT}"/enemy-territory )
33                 GAME_NAME="Enemy Territory"
34                 GAME_BIN="et"
35                 GAME_ICON="ET"
36                 DED_PKGS=""
37                 DED_BIN="et-ded"
38                 DED_OPTS="+set dedicated 1 +exec server.cfg"
39                 DED_CFG_DIR=".etwolf"
40                 SELECT_MOD="+set fs_game "
41                 ;;
42         quake3)
43                 GAME_PKGS="games-fps/quake3 games-fps/quake3-bin"
44                 GAME_DIRS=( "${GAMES_DATADIR}"/quake3 "${GAMES_PREFIX_OPT}"/quake3 )
45                 GAME_NAME="Quake III"
46                 GAME_BIN="quake3"
47                 GAME_ICON="quake3"
48                 DED_PKGS=""
49                 DED_BIN="quake3-ded"
50                 DED_OPTS="+set dedicated 1 +exec server.cfg"
51                 DED_CFG_DIR=".q3a"
52                 SELECT_MOD="+set fs_game "
53                 ;;
54         quake4)
55                 GAME_PKGS="games-fps/quake4-bin"
56                 GAME_DIRS=( "${GAMES_PREFIX_OPT}"/quake4 )
57                 GAME_NAME="Quake 4"
58                 GAME_BIN="quake4"
59                 GAME_ICON="/usr/share/pixmaps/quake4.bmp"
60                 DED_PKGS=""
61                 DED_BIN="quake4-ded"
62                 DED_OPTS="+set dedicated 1 +exec server.cfg"
63                 DED_CFG_DIR=".quake4"
64                 SELECT_MOD="+set fs_game "
65                 ;;
66         ut2003)
67                 GAME_PKGS="games-fps/ut2003"
68                 GAME_DIRS=( "${GAMES_PREFIX_OPT}"/ut2003 )
69                 GAME_NAME="UT2003"
70                 GAME_BIN="ut2003"
71                 GAME_ICON="ut2003"
72                 DED_PKGS=""
73                 DED_BIN="ucc"
74                 DED_OPTS=""
75                 DED_CFG_DIR=""
76                 SELECT_MOD="-mod="
77                 ;;
78         ut2004)
79                 GAME_PKGS="games-fps/ut2004"
80                 GAME_DIRS=( "${GAMES_PREFIX_OPT}"/{ut2004,ut2004-ded} )
81                 GAME_NAME="UT2004"
82                 GAME_BIN="ut2004"
83                 GAME_ICON="ut2004"
84                 DED_PKGS="games-server/ut2004-ded"
85                 DED_BIN="ut2004-ded"
86                 DED_OPTS=""
87                 DED_CFG_DIR=""
88                 SELECT_MOD="-mod="
89                 ;;
90         *)
91                 eerror "This game is either not supported or you must set the GAME"
92                 eerror "variable to the proper game."
93                 die "games-mods.eclass: unsupported GAME"
94                 ;;
95 esac
96
97 MOD_BIN="${GAME_BIN}-${PN/${GAME}-}"
98 MOD_DED_BIN="${MOD_BIN}-ded"
99
100 games-mods_get_rdepend() {
101         local pkgs
102
103         if [[ ${1} == "--ded" ]] ; then
104                 pkgs=( ${DED_PKGS} ${GAME_PKGS} )
105         else
106                 pkgs=( ${GAME_PKGS} )
107         fi
108
109         [[ ${#pkgs[@]} -gt 1 ]] && echo -n "|| ( "
110
111         case ${EAPI:-0} in
112                 0|1) echo -n "${pkgs[@]}" ;;
113                 [23456])
114                         local p
115                         if [[ ${1} == "--ded" ]] ; then
116                                 echo -n "${DED_PKGS}"
117                                 for p in ${GAME_PKGS} ; do
118                                         echo -n " ${p}[dedicated]"
119                                 done
120                         else
121                                 for p in ${GAME_PKGS} ; do
122                                         echo -n " || ( ${p}[opengl] ${p}[-dedicated] )"
123                                 done
124                         fi
125                         ;;
126                 *) die "EAPI ${EAPI} not supported"
127         esac
128
129         [[ ${#pkgs[@]} -gt 1 ]] && echo -n " )"
130 }
131
132 DESCRIPTION="${GAME_NAME} ${MOD_NAME} - ${MOD_DESC}"
133
134 SLOT="0"
135 IUSE="dedicated opengl"
136 RESTRICT="mirror strip"
137
138 DEPEND="app-arch/unzip"
139 RDEPEND="dedicated? ( $(games-mods_get_rdepend --ded) )
140         opengl? ( $(games-mods_get_rdepend) )
141         !dedicated? ( !opengl? ( $(games-mods_get_rdepend) ) )"
142
143 S=${WORKDIR}
144
145 INS_DIR=${GAMES_DATADIR}/${GAME}
146
147 games-mods_use_opengl() {
148         [[ -z ${MOD_DIR} ]] && return 1
149
150         if use opengl || ! use dedicated ; then
151                 # Use opengl by default
152                 return 0
153         fi
154
155         return 1
156 }
157
158 games-mods_use_dedicated() {
159         [[ -z ${MOD_DIR} ]] && return 1
160
161         use dedicated && return 0 || return 1
162 }
163
164 games-mods_dosyms() {
165         # We are installing everything for these mods into ${INS_DIR},
166         # ${GAMES_DATADIR}/${GAME} in most cases, and symlinking it
167         # into ${GAMES_PREFIX_OPT}/${GAME} for each game.  This should
168         # allow us to support both binary and source-based games easily.
169         local dir
170         for dir in "${GAME_DIRS[@]}" ; do
171                 [[ -z ${dir} || ${INS_DIR} == ${dir} ]] && continue
172                 pushd "${D}/${INS_DIR}" > /dev/null || die "pushd failed"
173                 local i
174                 for i in * ; do
175                         if [[ -d ${i} ]] ; then
176                                 if [[ ${i} == ${MOD_DIR} ]] ; then
177                                         dosym "${INS_DIR}/${i}" "${dir}/${i}" \
178                                                 || die "dosym ${i} failed"
179                                 else
180                                         local f
181                                         while read f ; do
182                                                 dosym "${INS_DIR}/${f}" "${dir}/${f}" \
183                                                         || die "dosym ${f} failed"
184                                         done < <(find "${i}" -type f)
185                                 fi
186                         elif [[ -f ${i} ]] ; then
187                                 dosym "${INS_DIR}/${i}" "${dir}/${i}" \
188                                         || die "dosym ${i} failed"
189                         else
190                                 die "${i} shouldn't be there"
191                         fi
192                 done
193                 popd > /dev/null || die "popd failed"
194         done
195 }
196
197 games-mods_make_initd() {
198         cat <<EOF > "${T}"/${MOD_DED_BIN}
199 #!/sbin/openrc-run
200 # Copyright 1999-2016 Gentoo Foundation
201 # Distributed under the terms of the GNU General Public License v2
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 }