--- /dev/null
+#!/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__"
+moo2UserDir="${HOME}/.moo2"
+configFile="${moo2UserDir}/moo2rc"
+. "${moo2Dir}/utils.sh"
+
+#############################################################################
+# 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() {
+ getOrCreateUsableDir "${moo2UserDir}" || die
+ cd "${moo2UserDir}" || die
+ ln -s "${moo2Dir}/cd" . || die
+ cp "${moo2Dir}/moo2rc" . || die
+
+ 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."
+
+ # Write the default dosbox config file
+ dosbox -conf /dev/null \
+ -c "${userMsg}" \
+ -c "@pause" \
+ -c "mount c ${moo2UserDir}" \
+ -c "c:" \
+ -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
+ local src="${moo2Dir}/MPS/ORION2"
+ local dest="${moo2UserDir}/MPS/ORION2"
+
+ getOrCreateUsableDir "${dest}" || die
+ pushd "${src}" || die
+
+ # Some files cannot be read only and must be copied
+ cp -L SOUND.LBX SR_R* *.INI "${dest}"
+
+ # 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 (and it's slow)
+ if [ -e "${dest}/MOO2V140.EXE" ]; then
+ dosbox \
+ -c "mount c ${moo2UserDir}" \
+ -c "c:" \
+ -c "cd MPS/ORION2" \
+ -c "MOO2V140.EXE" \
+ -c "exit" || die "dosbox exited with error"
+ fi
+}
+
+#############################################################################
+# Function : main
+# Arguments : none
+# Description : Main function
+#############################################################################
+main() {
+ local postRunMsg=""
+ local postRunCmd=""
+
+ # 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/ORION2.EXE" ]; then
+ # DOS text management is a pain
+ 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
+
+ # 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"
+}
+
+main
+
+# vim:ts=4
\ No newline at end of file