]> Pileus Git - ~andy/sunrise/blobdiff - games-strategy/moo2/files/1.40.24/moo2
sunrise/ games-strategy: New ebuild for bug #341859
[~andy/sunrise] / games-strategy / moo2 / files / 1.40.24 / moo2
diff --git a/games-strategy/moo2/files/1.40.24/moo2 b/games-strategy/moo2/files/1.40.24/moo2
new file mode 100755 (executable)
index 0000000..089b48a
--- /dev/null
@@ -0,0 +1,213 @@
+#!/bin/bash
+
+# moo2 - Master of Orion II lanuch & user setup script
+# Copyright (C) 2010 Daniel Santos <daniel.santos@pobox.com>
+# $Header: $
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+
+moo2Dir="__MOO2DIR__"
+. "${moo2Dir}/utils.sh"
+. "${moo2Dir}/backup.sh"
+moo2UserDir="${HOME}/.moo2"
+configFile="${moo2UserDir}/moo2rc"
+postRunMsg=""
+postRunCmd=""
+
+#############################################################################
+# Function    : getSeed
+# Arguments   : none
+# Description : Generates a signed 32-bit C-style random number.
+#############################################################################
+getSeed() {
+       # $RANDOM is a 15 bit random number so let's use a bunch of them to create
+       # a nasty 32 bit number
+       typeset -i i seed
+       #typeset -i seed
+       for (( i = 0; i < 18; ++i )); do
+               (( seed ^= ${RANDOM} << i ))
+       done
+
+       # Since bash doesn't treat this as a 32-bit C signed int, we have to
+       # convert it.  However, we must first make sure the resulting value will
+       # be within INT_MIN and INT_MAX from <limit.h> and compatibile with all C
+       # standards.
+       (( seed ^ 0x7fffffff )) && ((seed &= 0xfffffffe ))
+
+       # Add sign if bit 31 is set
+       (( seed & 0x80000000 )) && echo -e "-\c"
+       echo $(( seed & 0x7fffffff ))
+}
+
+#############################################################################
+# Function    : userInstall
+# Arguments   : none
+# Description : Performs user-level install.  This is only run once (for each
+#                              user) and does the following:
+#                              * Creates ${HOME}/.moo2 directory, both contain your config
+#                                files & serve as the dosbox C drive (jail).
+#                              * Copies default moo2rc from /opt/moo2/
+#                              * Generates dosboxrc, tweaks sound values and enables full
+#                                screen
+#                              * Creates symlink to /opt/moo2/cd, which will contain a copy
+#                                of the CD if you installed with USE=nocd, or will yet
+#                                another symlink to your CD-ROM drive where you had the
+#                                Orion2 CD when you ran the ebuild).
+#                              * Performs psudo-install into ${HOME}/.moo2/MPS/ORION2
+#                              * If you specified USE=lordbrazen, it will also run the
+#                                unofficial patch which is pretty slow.
+#############################################################################
+userInstall() {
+       local src="${moo2Dir}/MPS/ORION2"
+       local dest="${moo2UserDir}/MPS/ORION2"
+       local -i lbpatch=0$(test -e "${moo2Dir}/MPS/ORION2/MOO2V140.EXE" && echo 1)
+
+       getOrCreateUsableDir "${dest}" || die
+
+       cd "${moo2UserDir}" || die
+       ln -s "${moo2Dir}/cd" . || die
+       cp "${moo2Dir}/moo2rc" . || die
+
+       # DOS text management is a pain
+       local userMsg="@echo \
+Since this is your first time running Master of Orion II, we need to do some    \
+user-level setup. Master of Orion II uses dosbox.  See the Welcome box above for\
+help with that. Your dosbox config file is ${moo2UserDir}/dosboxrc."
+
+       # Write the default dosbox config file
+       dosbox -conf /dev/null \
+               -c "${userMsg}" \
+               -c "@pause" \
+               -c "config -writeconf dosboxrc" \
+               -c "exit"
+
+       sed -i -e 's/fullscreen=.*/fullscreen=true/g' \
+               -e 's/oplrate=.*/oplrate=22050/g' \
+               "${moo2UserDir}/dosboxrc" || die "sed failed"
+
+
+       # Setup the user's game directory
+       pushd "${src}" || die
+
+       # Some files cannot be read only and must be copied
+       cp -L SOUND.LBX SR_R* *.INI "${dest}" || die
+
+       # All the rest can be read-only symlinks
+       for f in *; do
+               test -e "${dest}/$f" || ln -s "${src}/$f" "${dest}" || die
+       done
+
+       popd
+
+       # if lordbrazen patch is installed, then we have to run it
+       if ((lbpatch)); then
+               # If you don't force dosbox to use 100% cpu, this can take a long time
+               echo -e "[cpu]\ncycles=max" >> dosboxrc.patch
+               dosbox -conf dosboxrc.patch \
+                       -c "@mount c ${moo2UserDir}" \
+                       -c "@c:" \
+                       -c "@cd MPS/ORION2" \
+                       -c "MOO2V140.EXE" \
+                       -c "exit" || die "dosbox exited with error"
+                       rm dosboxrc.patch
+               fi
+}
+
+#############################################################################
+# Function    : verifyEnv
+# Arguments   : none
+# Description : Performs sanity checks:
+#                              * Initates user install if needed
+#                              * Corrects moo2exe if lordbrazen patch not installed
+#                              * Warns if CD is not present
+#############################################################################
+verifyEnv() {
+       # If this is the 1st time to run, do user setup
+       if [ ! -e "${moo2UserDir}/MPS/ORION2/ORION2.EXE" ]; then
+               userInstall
+       fi
+       . "${configFile}"
+
+       # If we have the unofficial patch specified in the config, but not
+       # installed, use the offical version
+       if [ ! -e "${moo2UserDir}/MPS/ORION2/M2V140.EXE" ]; then
+               moo2exe=ORION2.EXE
+       fi
+
+       # Make sure the CD is still in
+       if [ ! -e "${moo2UserDir}/cd/ORION95.EXE" ]; then
+               postRunMsg="\
+You're Moo2 CD does not appear to be mounted.  You can either put it in,        \
+reinstall with the nocd USE option (while the CD in) or change the symlink      \
+${moo2UserDir}/cd."
+       fi
+
+       if ((${#postRunMsg})); then
+               postRunCmd="@echo ${postRunMsg}"
+               echo
+               warn ${postRunMsg}
+               echo
+       fi
+}
+
+#############################################################################
+# Function    : runGame
+# Arguments   : none
+# Description : (Attempts to) run the game.  Function will not return.
+#############################################################################
+runGame() {
+       # Run the game
+       exec dosbox -conf "${moo2UserDir}/dosboxrc" \
+               -c "mount c ${moo2UserDir}" \
+               -c "c:" \
+               -c "cd MPS/ORION2" \
+               -c "${moo2exe} ${moo2args} /seed=$(getSeed)" \
+               -c "${postRunCmd}" \
+               -c "@pause" \
+               -c "exit" || warn "dosbox exited with error"
+}
+
+showUsage() {
+       echo "USAGE: $0 [backup|restore [file_name]]"
+       exit
+}
+
+#############################################################################
+# Function    : main
+# Arguments   : none
+# Description : Main function
+#############################################################################
+main() {
+       verifyEnv
+
+       # I should probably just make a separate script for this, but screw it.
+       if [[ "$1" == "backup" ]]; then
+               shift
+               (($# <= 1)) || showUsage
+               doBackup "$@"
+       elif [[ "$1" == "restore" ]]; then
+               shift
+               (($# <= 1)) || showUsage
+               echo "Restore not yet supported, but your backups are in" \
+                       "${moo2UserDir}/${backupBaseDir}"
+       else
+               (($#)) && showUsage
+               runGame
+       fi
+}
+
+main "$@"
+
+# vim:ts=4
\ No newline at end of file