#!/bin/bash
# Init
FILE="/tmp/out.$$"
GREP="/bin/grep"
#....
# Make sure only root can run our script
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
exit 1
fi
# Script start
clear
echo "Welcome to the CentOS"
echo "+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+"
echo "|H|e|r|c|u|l|e|s| |r|A|t|h|e|n|a|"
echo "+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+"
echo " Installation Script | By CyDs86 - Josevill"
echo
echo "Please select which emulator you wish to use:"
echo "1.- rAthena"
echo "2.- Hercules"
echo
read emulator
na=3
if [ "$emulator" -ge "$na" ]
then
echo "Invalid Selection, aborting!"
exit
else
echo "Please input an username for the non-root user this script will create for running and managing the emulator: "
read nonroot
echo "Please enter the name of the folder in home-dir (main directory) where the emulator will be downloaded: "
read folder
read -p "Press [Enter] key to start installation..."
clear
echo "╔═══════════════════════════════════════════════╗"
echo "║Updating distro and downloading necessary files║"
echo "╚═══════════════════════════════════════════════╝"
# System updating
yum update -y
# Installin emulator dependancies
yum install -y git make gcc mysql mysql-server mysql-devel zlib-devel pcre-devel screen
service mysqld start
chkconfig mysqld on
echo
read -p "Press [Enter] key to continue..."
clear
# Creating the user that will manage the emulator
echo "╔════════════════════════════════════════════╗"
echo "║Input the password for the new non root user║"
echo "╚════════════════════════════════════════════╝"
useradd -m -s /bin/bash $nonroot
passwd $nonroot
clear
# Giving MySQL Root user a Password and Creating Databases
echo
echo "╔═════════════════════════════════╗"
echo "║MySQL User and Databases creation║"
echo "╚═════════════════════════════════╝"
echo
echo -n "Enter the MySQL root password: "
read rootpw
echo
echo "You will be prompted to enter a password, just click intro and carry on with the rest of the script."
mysqladmin -u root password $rootpw
clear
echo -n "Name for the ragnarok main DB (example: ragnarok_db): "
read rodb
echo -n "Name for the ragnarok logs DB (example: ragnarok_log): "
read rolog
echo -n "Enter database username (example: ragnarok): "
read dbuser
echo -n "Enter the password for the database user (example: laliulelo): "
read dbupw
mysql -u root -p'$rootpw' -e "CREATE USER '$dbuser'@'localhost' IDENTIFIED BY '$dbupw';"
mysql -u root -p'$rootpw' -e "CREATE DATABASE IF NOT EXISTS $rodb;"
mysql -u root -p'$rootpw' -e "GRANT SELECT,INSERT,UPDATE,DELETE ON $rodb.* TO '$dbuser'@'localhost';"
mysql -u root -p'$rootpw' -e "CREATE DATABASE IF NOT EXISTS $rolog;"
mysql -u root -p'$rootpw' -e "GRANT SELECT,INSERT ON $rolog.* TO '$dbuser'@'localhost';"
if [ $? != "0" ]; then
echo "[Error]: Database / User creation failed"
exit 1
else
echo "╔════════════════════════════════════════╗"
echo "║Databases have been created successfully║"
echo "╚════════════════════════════════════════╝"
echo " DB Info: "
echo ""
echo " ROOT PW: $rootpw <-- Save this pw in a safe place"
echo " RO DB : $rodb <--- Ragnarok main DB"
echo " Logs DB : $rolog <--- Ragnarok logs DB"
echo " DB User : $dbuser <--- can access to both db"
echo " DB Pass : $dbupw <--- to access both db"
echo ""
echo " -------------------------------------------------------"
echo "| Remember this user has been created to work locally, |"
echo "| if you need to access your data remotely, you will |"
echo "| need to create a new user with 'remote host' to do so.|"
echo " -------------------------------------------------------"
fi
read -p "Press [Enter] key to continue..."
# Testing SQL
echo
echo "Main database test: enter password for the mysql user (not root)"
echo "When the MySQL promt appears, type "exit" to end the test"
mysql -u $dbuser -p -D $rodb
if [ $? != "0" ]; then
echo "[Error]: Database connection failed"
exit 1
else
echo
echo "Logs database test: enter password for the mysql user (not root)"
echo "When the MySQL promt appears, type "exit" to end the test"
mysql -u $dbuser -p -D $rolog
echo
fi
echo "Entering user's folder..."
cd /home/$nonroot
read -p "Press [Enter] key to continue..."
clear
echo "Downloading emulator"
if [ "$emulator" = 1 ]
then git clone https://github.com/rathena/rathena.git /home/$nonroot/$folder
else git clone https://github.com/HerculesWS/Hercules.git /home/$nonroot/$folder
fi
cd /home/$nonroot/$folder/sql-files
clear
echo "The following database proccedure will require your ROOT MySQL passwd."
read -p "Press [Enter] key to continue..."
clear
echo "Setting up databases"
mysql -u root -p'$rootpw' $rodb < main.sql
mysql -u root -p'$rootpw' $rodb < item_db.sql
mysql -u root -p'$rootpw' $rodb < item_db2.sql
mysql -u root -p'$rootpw' $rodb < mob_db.sql
clear
echo "Setting up databases."
mysql -u root -p'$rootpw' $rodb < mob_db2.sql
mysql -u root -p'$rootpw' $rodb < mob_skill_db.sql
mysql -u root -p'$rootpw' $rodb < mob_skill_db2.sql
clear
echo "Setting up databases.."
mysql -u root -p'$rootpw' $rolog < logs.sql
mysql -u root -p'$rootpw' $rodb < item_db_re.sql
mysql -u root -p'$rootpw' $rodb < item_db2_re.sql
clear
echo "Setting up databases..."
mysql -u root -p'$rootpw' $rodb < mob_db_re.sql
mysql -u root -p'$rootpw' $rodb < mob_skill_db_re.sql
read -p "Press [Enter] key to continue..."
clear
echo "Giving $nonroot emulator's ownership"
cd /home/$nonroot
chown -R $nonroot *
read -p "Press [Enter] key to continue..."
clear
echo "╔══════════════════════╗"
echo "║Installation Finished.║"
echo "╚══════════════════════╝"
echo
echo "Please don't forget to configure the emulator in the config folder"
echo "Your emulator installation is located in /home/$nonroot/$folder"
echo "Thanks for using this script! - CyDs86"
echo -e "CentOS translation and Typos by Josevill\a"
fi
exit