#Install CentOS 7 Minimal in a virtual machine
#do not forget to turn on the networking and note the IP.

#once installed and rebooted, connect via SSH so you can copy/paste

#update server to current
yum -y update;reboot;

#install required packages
yum -y install cmake gcc gcc-c++ boost boost-devel wget mariadb mariadb-server mariadb-devel libicu libicu-devel perl perl-devel perl-URI-Escape-XS perl-URI perl-libwww-perl lua-devel compat-lua compat-lua-devel luabind luabind-devel zlib-devel community-mysql-server community-mysql-devel sudo tar unzip git subversion; reboot;

#optional utilities that I cannot live without
yum -y install tmux nano

# add epel if desired for other things. I do not use it for a base install like this.
#yum -y install epel-release

#setup the mariadb (mysql) server. need to sudo for this so just do it now before we setup the non root user.
systemctl enable mysqld
systemctl start mysqld
mysqladmin -u root password
#enter the password for the mysql root user

#same for the firewall, do it now since it needs sudo if not.
#these are the ports used by the game itself.
firewall-cmd --permanent --zone=public --add-port=7000-7100/udp --add-port=7778/udp --add-port=9000/udp 
#if you are going to connect to mysql from another machine, open that too
firewall-cmd --permanent --zone=public --add-port=3306/tcp
firewall-cmd --reload

#create non-root account
useradd eqadmin
passwd eqadmin
#enter the password for the new user

# grant it sudo access
usermod eqadmin -a -G wheel

#log out and log back in as the non-root account

#create the folders to be used to build the server in your home folder
cd ~;mkdir eqemu;mkdir eqemu/source;mkdir eqemu/source/peqdb;mkdir eqemu/server;

#download the source from github
cd ~/eqemu/source;git clone git://github.com/EQEmu/Server.git;
#download the quests from github
cd ~/eqemu/source;git clone git://github.com/ProjectEQ/projecteqquests.git;
#download the maps from github
cd ~/eqemu/source;git clone git://github.com/Akkadius/EQEmuMaps.git
#download the peq database. note: the filename changes daily (http://edit.peqtgc.com/weekly/) and this may NOT be a stable database.
cd ~/eqemu/source/peqdb;curl -O http://edit.peqtgc.com/weekly/peqbeta_2015-06-12-02:01.tar.gz;
#rename the file
cd ~/eqemu/source/peqdb;mv peqbeta_*.tar.gz peqbeta.tar.gz
#unpack the file
cd ~/eqemu/source/peqdb;tar -xvzf peqbeta.tar.gz;
#remove the file
cd ~/eqemu/source/peqdb;rm -f peqbeta.tar.gz
#rename the internal files
cd ~/eqemu/source/peqdb;mv data_tables_*.sql data_tables.sql;mv player_tables_*.sql player_tables.sql;mv peqbeta_*.sql peqbeta.sql

#change the to Server folder and review the cmake dependencies
cd ~/eqemu/source/Server;cmake -G "Unix Makefiles";

#build the server, go get caffeine, it will take a bit.
cd ~/eqemu/source/Server;make;

#link the various files to the server directory so we do not have to copy them all the time.
#note I run this build for dev purposes and expect to overwrite the executables all the time.
#you may want to add some copy/backup functionality for a production server

cd ~/eqemu/server;
ln -s ~/eqemu/source/Server/utils/defaults/logs .
ln -s ~/eqemu/source/Server/utils/defaults/shared .
ln -s ~/eqemu/source/Server/utils/defaults/eqemu_config.xml .
ln -s ~/eqemu/source/Server/utils/patches/* .
ln -s ~/eqemu/source/Server/bin/* .
ln -s ~/eqemu/source/projecteqquests/plugins .
ln -s ~/eqemu/source/projecteqquests/lua_modules .
ln -s ~/eqemu/source/projecteqquests quests
ln -s ~/eqemu/source/EQEmuMaps/maps .
ln -s ~/eqemu/source/peqdb/eqtime.cfg .

# update eqeqmu_config.xml
cd ~/eqemu/server;nano eqemu_config.xml

#setup the peq database
mysql -u root -p
create database peq;
use peq;

#create a non-root user 
grant all privileges on peq.* to 'peqdbusr'@'%' identified by 'yourpassword' with grant option;

#exit from mysql

#change the location of the sql files
cd ~/eqemu/source/peqdb

#log on with the regular user and source in the the database
mysql -upeqdbuser -p peq

source peqbeta.sql;
source player_tables.sql;
#by default the peq database launches 40 dynamic zones. I only run 5 on my test server.
update launcher set dynamics = 5 where `name` = 'peq';

#time to launch the server
#always switch to the server folder before starting tmux otherwise you have to switch each session
cd ~/eqemu/server
#launch a tmux session to keep everything running 
tmux new-session -s eqemu
#split the screen
CTRL+b then %
CTRL+b then left arrow to move back to the left side 
#create three more sessions on the left side
CTRL+b then "
CTRL+b then "
CTRL+b then "

#Launch the server
#use CTRL+b then arrow key up / down /left /right to select between shells.
#In the right shell launch the world server
CTRL+b then right arrow
#In the bottom left shell launch the zone server
CTRL+b the left arrow (and then CTRL+b down arrow as needed)
./eqlaunch peq
#In the third one down launch ucs
CTRL+b and up arrow
#In the second one down launch queryserv
CTRL+b and up arrow

#I use the top one to run any commands potentially needed.

#Disconnect from tmux with
CTRL+b then d

#Reconnect to the running session
tmux attach-session -t eqemu