#!/bin/bash
#
# Description:
# This file is part of UnReal World RPG version 3.40
# It launches UrW and set up configs
#
# License:
# Copyright (c) 2015 Tuukka Pasanen <tuukka.pasanen@ilmi.fi>>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#


IFS=$'\n'
URW_OS=$(uname)
URW_BIN="urw3-bin"
URW_VERSION="3.40"

if [ "${URW_OS}" == "Linux" ]; then
   echo "Running under Linux.. OK!"
   URW_HOME="${HOME}/.config/urw3-$(uname)"
   if [ -d /usr/share/urw3 ]; then
     URW_DATA="/usr/share/urw3"
     URW_BIN_DIR="/usr/bin"
   else
     echo "You probably are UBUNTU PPA user!"
     URW_DATA="/opt/urw3"
     URW_BIN_DIR="/opt/urw3"
   fi
fi

if [ "${URW_OS}" == "Darwin" ]; then
   echo "Running under Mac OS X.. OK!"
   URW_HOME="${HOME}/Library/Application Support/urw3-$(uname)"
   URW_APP_LOCATION="$(dirname $0)/.."
   if [ "${URW_APP_LOCATION}" == "./.." ]; then
      URW_APP_LOCATION="$(pwd)/.."
   fi


   URW_DATA="${URW_APP_LOCATION}/Resources"
   URW_BIN_DIR="${URW_APP_LOCATION}/Resources"
fi


URW_HOME_GFX="${URW_HOME}/truegfx"
URW_HOME_TILE="${URW_HOME}/truetile"
URW_HOME_AUDIO="${URW_HOME}/audio"
URW_HOME_MESSAGES="${URW_HOME}/messages"
URW_HOME_GAMEINFO="${URW_HOME}/gameinfo"

# echo "${URW_HOME_AUDIO}"
# echo "${URW_HOME_GFX}"

echo "Entering The UnReal World RPG"
echo "Data dir is: ${URW_DATA}"
echo "Home dir is: ${URW_HOME}"

#
# Files that are absolutly needed
#
URW_FILES="${URW_DATA}/truegfx/*.jpg\n\
${URW_DATA}/truegfx/*.png\n\
${URW_DATA}/truetile/*.png\n\
${URW_DATA}/truetile/*.txt\n\
${URW_DATA}/audio/*.wav\n\
${URW_DATA}/audio/*.ogg\n\
${URW_DATA}/gameinfo/*.txt\n\
${URW_DATA}/messages/*.txt\n\
${URW_DATA}/*.RIT\n\
${URW_DATA}/*.arr\n\
${URW_DATA}/*.txt\n\
${URW_DATA}/*.TXT\n\
${URW_DATA}/*.FNT\n\
${URW_DATA}/*.NFO\n\
${URW_DATA}/*.STD\n\
${URW_DATA}/RNDCAMP.*\n\
${URW_DATA}/*.dat\n\
${URW_DATA}/*.nfo"

#
# Files that are needed to be copied (for some weird reason?)
#
URW_FILE_AUDIO=(${URW_HOME_AUDIO}/throw.wav ${URW_HOME_AUDIO}/hitplr.wav \
${URW_HOME_AUDIO}/smear.wav ${URW_HOME_AUDIO}/bird_peippo.wav)

for dir in "${URW_HOME}" "${URW_HOME_GFX}" "${URW_HOME_TILE}" "${URW_HOME_AUDIO}" "${URW_HOME_MESSAGES}" "${URW_HOME_GAMEINFO}"; do
    if [ ! -d "${dir}" ]; then
      mkdir -p "${dir}"
    fi
done

# Then go to home dir
cd "${URW_HOME}"

#
# We have to sanitaze old links that
# URW doesn't crash on start
#
for FILE in $(find -L "${URW_HOME}" -type l); do
  echo "'Can't linked file ${FILE}': I remove it!"
  rm -f "${FILE}"
done

for file in $(echo -e ${URW_FILES}); do
  URW_LINK_FILE=$(echo ${file} | sed -e "s#${URW_DATA}#${URW_HOME}#")

  # File doesn't exists link/copy it
  if [ ! -e  "${URW_LINK_FILE}" ]; then
     #
     # Some files we have to copy
     # Some files we have to link
     # Don't ask why but that's the way it's going to be
     #
     if [[ ${URW_FILE_AUDIO[*]} =~ "${URW_LINK_FILE}" ]]; then
        cp "${file}" "${URW_LINK_FILE}"
     else
        ln -sf "${file}" "${URW_LINK_FILE}"
     fi
   else
     #
     # Be sure that files that neededs to be copied are copied
     #
     if [[ ${URW_FILE_AUDIO[*]} =~ "${URW_LINK_FILE}" ]]; then
        if [ -L  "${URW_LINK_FILE}" ]; then
           rm -f "${URW_LINK_FILE}"
           cp "${file}" "${URW_LINK_FILE}"
        fi
     fi
    fi
done

echo "Entering The UnReal World RPG Version ${URW_VERSION}"

# Launch urw!
"${URW_BIN_DIR}"/"${URW_BIN}"

