#!/bin/bash 
#
# 0adLinuxMapInstaller.sh
# Author: Theta14 of the 0ad Game Server 2014 aug 14th 
#
# A script to make installing 0ad addon maps easy for GNU Linux users.
# This script assumes that it's placed in the map archive.
#
# As with all software, from Itunes to Windows 7, the creator 
# makes no promise that this script won't hurt your system.
# 
# 
# Must run script as super user.
#
# See LinuxMapInstaller.README
# ==================================================================== 

#globals 
declare idRet=`id -u`
declare installDirGetLoop=true
declare installDirStruct="/mods/user/maps/skirmishes"
declare installDir=""

#Is this script running as root?
if  test $idRet -eq 0 ; then
	printf "\n\n0adLinuxMapInstaller.sh: Error: Must NOT run as root.\n" 
	printf "See LinuxMapInstaller.README.\n\n"
        exit 0
fi

#Are the map files present?
if test ! -e *.xml  ; then
   if test ! -e *.pmp ; then
      printf "\nCustom Maps not found!\n\n" 
      exit 0;
   fi
fi
printf "\nFiles matching custom maps found.\n"

#Where is 0ad's userland local for this user?
if test -d "/home/`whoami`/.local/share/0ad" ; then
installDir="/home/`whoami`/.local/share/0ad" 
installDirGetLoop=false
printf "\nNote: Local 0ad files found.\n"
	else
		printf "\n\n0adLinuxMapInstaller.sh:Error: .local/share/0ad does not exist." 
		exit 0;
fi



# does the file structure exist?

if test ! -d "$installDir$installDirStruct"; then
	printf "\nThe file structure is not present... Building.\n\n"
	mkdir -p -v "$installDir$installDirStruct" 	
fi

#install the files

cp -v *.xml "$installDir$installDirStruct" 
cp -v *.pmp "$installDir$installDirStruct" 

# end it

printf "\nMap install complete. Restart 0ad if it is running.\n"
exit 1
