#!/bin/sh
## dwarf fortress wrapper written and maintained by
# Robert Stancil <robert.stancil@mavs.uta.edu>
# 
# MIT License
#
# Copyright (c) 2019 Robert Stancil <robert.stancil@mavs.uta.edu>
#
# 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 (including the next 
# paragraph) 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.
##

usage() {
 printf "Usage:\n %s [-f]\noptions:\n -f\tforce/recreate user's game data directory. <Warning: this will erase all save data>" "${0}"
}

while getopts 'f' c
do
 case $c in
  f) _FORCE=yes ;;
  *) usage ;;
 esac
done

if [ $(id -u) -eq 0 ]; then
 echo "Error, can't be run as root" >&2
 usage
 exit
fi

if [ -z ${XDG_DATA_HOME:-$HOME/.local/share} ]; then
 echo "homedir unset"
 usage
 exit
fi

if [ ! -d ${XDG_DATA_HOME:-$HOME/.local/share}/dwarffortress ]; then
 _FORCE=yes
fi
if [ $_FORCE ]
then
 rm -rf ${XDG_DATA_HOME:-$HOME/.local/share}/dwarffortress
 cp /usr/share/dwarffortress ${XDG_DATA_HOME:-$HOME/.local/share} -r
 ln -s /usr/lib/dwarffortress/libs ${XDG_DATA_HOME:-$HOME/.local/share}/dwarffortress/
fi

${XDG_DATA_HOME:-$HOME/.local/share}/dwarffortress/df
