nalc-server/install.sh

488 lines
13 KiB
Bash
Raw Normal View History

2018-03-15 23:49:09 +01:00
#!/bin/bash
2019-04-28 22:43:57 +02:00
# Author: Sys4
# License: GPLv3
2018-03-15 23:49:09 +01:00
2022-05-27 14:36:02 +02:00
usage() {
echo "Usage: ./install_nalc.sh [options] [--] [destination]"
echo
echo "Description:"
echo -e "\tDownload and install the files needed by the NALC server to the"
echo -e "\toptional [destination] folder. Otherwise the current directory"
echo -e "\twill be used."
echo
echo -e "\tBut, be carefull!"
echo -e "\tBy default the ~/.minetest dir will also be used and modified."
echo -e "\tSo, make a backup of your ~/.minetest directory before the"
echo -e "\tinstallation of this server or specify a new directory with the"
echo -e "\t --minetest-dir or -d option."
echo
echo "Requirements:"
echo -e "\t- Official Minetest server must be already installed on your system."
echo -e "\tIf it is not the case, please refers to the https://minetest.net"
echo -e "\twebsite for the installation instructions."
echo -e "\t- You'll also need git, cmake, make."
echo
echo "Options:"
echo -e "\t-h --help\t This help."
echo -e "\t-b --minetest-bin <path> Minetest server executable path."
echo -e "\t-d --minetest-dir <dir> Minetest directory to use for installing nalc mods, nalc game, minetest.conf…"
echo -e "\t-m --map-backend <sqlite3|leveldb|redis|postgresql|dummy>"
echo -e "\t-p --player-backend <sqlite3|leveldb|postgresql|dummy|files>"
echo -e "\t-a --auth-backend <sqlite3|leveldb|postgresql|files>"
2022-08-22 12:34:55 +02:00
echo -e "\t-s --mod-storage-backend <sqlite3|files|dummy>"
2022-05-27 14:36:02 +02:00
echo -e "\t-M --minetest-mapper Install with minetest mapper support."
echo -e "\t-w --world <name> Custom world name."
echo -e "\t-P --port <port> Custom server port number."
echo -e "\t--url <URL> Custom remote URL."
echo -e "\t--branch <stable|dev> Choose the version to install (stable by default)"
2022-05-27 14:36:02 +02:00
echo
echo "License: GPLv3. Please read the LICENSE file."
echo "Author: Sys4 - https://sys4.fr#contact"
exit 0
}
strip() {
echo $1 | cut -d \' -f 2
}
2018-03-15 23:49:09 +01:00
verif() {
2022-05-27 14:36:02 +02:00
if [[ $? -gt 0 ]]; then
echo "Error! Exit."
2020-07-18 12:37:59 +02:00
exit 1
fi
2018-03-15 23:49:09 +01:00
}
2018-03-16 23:47:34 +01:00
error() {
2022-05-27 14:36:02 +02:00
if [[ -n $1 ]]; then
echo "ERROR: $1"! >&2
else
echo "ERROR: Invalid option!" >&2
echo "Use -h option for help." >&2
fi
exit 1
2018-03-16 23:47:34 +01:00
}
2022-05-27 14:36:02 +02:00
save_conf() {
local conf="$1"
echo "MINETEST_BIN=$MINETEST_BIN" > "$conf"
echo "MINETEST_DIR=$MINETEST_DIR" >> "$conf"
echo "MAP_BACKEND=$MAP_BACKEND" >> "$conf"
echo "PLAYER_BACKEND=$PLAYER_BACKEND" >> "$conf"
echo "AUTH_BACKEND=$AUTH_BACKEND" >> "$conf"
2022-08-22 12:34:55 +02:00
echo "MOD_STORAGE_BACKEND=$MOD_STORAGE_BACKEND" >> "$conf"
2022-05-27 14:36:02 +02:00
echo "MINETEST_MAPPER=$MINETEST_MAPPER" >> "$conf"
echo "URL=$URL" >> "$conf"
echo "BRANCH=$BRANCH" >> "$conf"
echo "WORLD_NAME=$WORLD_NAME" >> "$conf"
echo "PORT=$PORT" >> "$conf"
2018-03-16 23:47:34 +01:00
}
2022-05-27 14:36:02 +02:00
load_conf() {
. "$1"
}
2022-05-27 14:36:02 +02:00
install_game() {
local game_dir="$MINETEST_DIR/games/nalc_game"
if [ -d "$game_dir" ]; then
echo "Previous installation of NALC game detected."
read -p "Upgrade? ([Y]es/[n]o/clean/[c]ancel) " continue
case "$continue" in
[yY][eE][sS]|[yY]|"")
echo "Upgrading…"
pushd "$game_dir"
git pull
verif
popd;;
[cC][lL][eE][aA][nN])
echo "Cleaning…"
rm -rf "$game_dir"
verif;;
[cC][aA][nN][cC][eE][lL]|[cC])
echo "Upgrade cancelled."
exit 0;;
esac
2020-07-18 12:37:59 +02:00
fi
2022-05-27 14:36:02 +02:00
if [ ! -d "$game_dir" ]; then
local git_branch
local depth
case $BRANCH in
"stable")
git_branch="-b master"
depth="--depth 1"
;;
"dev")
git_branch="-b dev"
;;
esac
[ -z "$git_branch" ] && error "Invalid branch"
echo "Cloning $URL""nalc/nalc_game.git to $game_dir"
mkdir -p "$MINETEST_DIR/games"
2020-07-18 12:37:59 +02:00
verif
2022-05-27 14:36:02 +02:00
git clone $depth $git_branch $URL"nalc/nalc_game.git" "$game_dir"
2020-07-18 12:37:59 +02:00
verif
fi
2022-05-27 14:36:02 +02:00
echo "NALC game installed/upgraded."
2018-03-22 10:07:56 +01:00
}
install_mods() {
2022-05-27 14:36:02 +02:00
local mods_dir="$MINETEST_DIR/mods"
if [ -d "$mods_dir" ]; then
echo "Mods directory is not empty."
read -p "Upgrade? ([Y]es/[n]o/clean/[c]ancel) " continue
case "$continue" in
[yY][eE][sS]|[yY]|"")
echo "Upgrading…"
"$SCRIPT_DIR"/upgrade.sh -m -s "$DESTINATION"
verif
;;
[cC][lL][eE][aA][nN])
echo "Cleaning…"
rm -rf "$mods_dir"
verif
;;
[cC][aA][nN][cC][eE][lL]|[cC])
echo "Upgrade cancelled."
exit 0;;
esac
2020-07-18 12:37:59 +02:00
fi
2022-05-27 14:36:02 +02:00
if [ ! -d "$mods_dir" ]; then
local git_branch
local depth
case $BRANCH in
"stable")
git_branch="-b master"
depth="--depth 1"
;;
"dev")
git_branch="-b dev"
;;
esac
[ -z $git_branch ] && error "Invalid branch"
echo "Cloning $URL/nalc-server-mods.git to $mods_dir"
git clone $depth $git_branch $URL"nalc/nalc-server-mods.git" "$mods_dir"
2020-07-18 12:37:59 +02:00
verif
2022-05-27 14:36:02 +02:00
pushd "$mods_dir"
git submodule update --init --recursive
verif
popd
2020-07-18 12:37:59 +02:00
fi
2022-05-27 14:36:02 +02:00
echo "Mods for NALC installed/upgraded."
}
2018-03-31 16:13:41 +02:00
install_world() {
2022-05-27 14:36:02 +02:00
local world_dir="$MINETEST_DIR/worlds/$WORLD_NAME"
if [ -d "$world_dir" ]; then
echo "World $WORLD_NAME already exist."
2022-05-27 14:36:02 +02:00
read -p "Upgrade $WORLD_NAME? ([Y]es/[n]o/[c]lean) " continue
case "$continue" in
[yY][eE][sS]|[yY]|"")
echo "Upgrading…"
[ -d "$world_dir"_old ] && rm -rf "$world_dir"_old
cp -r "$world_dir" "$world_dir"_old
verif
;;
[cC][lL][eE][aA][nN]|[cC])
echo "Cleaning…"
[ -d "$world_dir"_old ] && rm -rf "$world_dir"_old
mv "$world_dir" "$world_dir"_old
verif
;;
[nN][oO]|[nN])
echo "Skiping…"
return
;;
2022-05-27 14:36:02 +02:00
esac
2020-07-18 12:37:59 +02:00
fi
2022-05-27 14:36:02 +02:00
if [ ! -d "$world_dir" ]; then
mkdir -p "$world_dir/runes"
verif
2020-07-18 12:37:59 +02:00
2022-05-27 14:36:02 +02:00
local mt=$world_dir/world.mt
echo "Generating $mt""…"
2020-07-18 12:37:59 +02:00
2022-05-27 14:36:02 +02:00
echo "gameid = nalc_game" > $mt
echo "enable_damage = true" >> $mt
echo "creative_mod = false" >> $mt
case $MAP_BACKEND in
"sqlite3"|"")
echo "backend = sqlite3" >> $mt
;;
"leveldb")
echo "backend = leveldb" >> $mt
;;
"redis")
echo "backend = redis" >> $mt
read -p "redis_address? " val
echo "redis_address = $val" >> $mt
read -p "redis_hash? " val
echo "redis_hash = $val" >> $mt
read -p "redis_port? (6379) " val
[ -z $val ] && val=6379
echo "redis_port = $val" >> $mt
;;
"postgresql")
echo "backend = postgresql" >> $mt
read -p "DB address? " val
read -p "DB user? " val2
read -p "DB password? " val3
read -p "DB name? " val4
read -p "DB port? (5432) " val5
[ -z $val5 ] && val5=5432
echo "pgsql_connection = host=$val port=$val5 user=$val2 password=$val3 dbname=$val4" >> $mt
;;
"dummy")
echo "backend = dummy" >> $mt
;;
*)
error "$MAP_BACKEND is not valid";;
esac
case $PLAYER_BACKEND in
"sqlite3"|"")
echo "player_backend = sqlite3" >> $mt
;;
"leveldb")
echo "player_backend = leveldb" >> $mt
;;
"postgresql")
echo "player_backend = postgresql" >> $mt
read -p "DB address? ($val) " vval
read -p "DB user? ($val2) " vval2
read -p "DB password? ($val3) " vval3
read -p "DB name? (players-$val4) " vval4
read -p "DB port? ($val5) " vval5
[ -z $vval ] && vval=$val
[ -z $vval2 ] && vval2=$val2
[ -z $vval3 ] && vval3=$val3
[ -z $vval4 ] && vval4="players-$val4"
[ -z $vval5 ] && vval5=$val5
echo "pgsql_player_connection = host=$vval port=$vval5 user=$vval2 password=$vval3 dbname=$vval4" >> $mt
;;
"dummy")
echo "player_backend = dummy" >> $mt
;;
"files")
echo "player_backend = files" >> $mt
;;
*)
error "$PLAYER_BACKEND is not valid";;
esac
case $AUTH_BACKEND in
"sqlite3"|"")
echo "auth_backend = sqlite3" >> $mt
;;
"leveldb")
echo "auth_backend = leveldb" >> $mt
;;
"postgresql")
echo "auth_backend = postgresql" >> $mt
read -p "DB address? ($val) " vval
read -p "DB user? ($val2) " vval2
read -p "DB password? ($val3) " vval3
read -p "DB name? (auth-$val4) " vval4
read -p "DB port? ($val5) " vval5
[ -z $vval ] && vval=$val
[ -z $vval2 ] && vval2=$val2
[ -z $vval3 ] && vval3=$val3
[ -z $vval4 ] && vval4="auth-$val4"
[ -z $vval5 ] && vval5=$val5
echo "pgsql_auth_connection = host=$vval port=$vval5 user=$vval2 password=$vval3 dbname=$vval4" >> $mt
;;
"files")
echo "auth_backend = files" >> $mt
;;
*)
error "$AUTH_BACKEND is not valid";;
esac
2022-08-22 12:34:55 +02:00
case $MOD_STORAGE_BACKEND in
"sqlite3"|"")
echo "mod_storage_backend = sqlite3" >> $mt
;;
"files")
echo "mod_storage_backend = files" >> $mt
;;
"dummy")
echo "mod_storage_backend = dummy" >> $mt
;;
*)
error "$MOD_STORAGE_BACKEND is not valid";;
esac
2022-05-27 14:36:02 +02:00
read -p "Server announce? (y/N) " val
if [[ $val =~ [yY][eE][sS]|[yY] ]]; then
echo "server_announce = true" >> $mt
else
echo "server_announce = false" >> $mt
fi
2020-07-18 12:37:59 +02:00
fi
2018-03-24 14:06:26 +01:00
}
install_mtmapper() {
2022-05-27 14:36:02 +02:00
if [ ! -d "$DESTINATION/minetestmapper" ]; then
echo "Clone Minetest mapper…"
git clone --depth 1 $URL"/minetest/minetestmapper.git" "$DESTINATION/minetestmapper"
verif
else
2022-05-27 14:36:02 +02:00
echo "Upgrading Minetest mapper…"
pushd "$DESTINATION/minetestmapper"
git pull
verif
2022-05-27 14:36:02 +02:00
popd
fi
2022-05-27 14:36:02 +02:00
pushd "$DESTINATION/minetestmapper"
cmake .
2022-05-27 14:36:02 +02:00
[ $? -eq 1 ] && error "CMake failed. Check dependencies"
make -j$(nproc)
verif
popd
2022-05-27 14:36:02 +02:00
if [ ! -d "$DESTINATION/leaftest" ]; then
echo "Installing leaftest…"
git clone --recursive --depth 1 $URL"nalc/leaftest.git" "$DESTINATION/leaftest"
verif
else
2022-05-27 14:36:02 +02:00
echo "Upgrading leaftest…"
pushd "$DESTINATION/leaftest"
git pull
verif
2022-05-27 14:36:02 +02:00
popd
fi
}
2022-05-27 14:36:02 +02:00
post_install() {
if [[ "$DESTINATION" != "$SCRIPT_DIR" ]]; then
cp "$SCRIPT_DIR/mods_disabled.txt" "$DESTINATION"
2020-07-18 12:37:59 +02:00
fi
2022-05-27 14:36:02 +02:00
"$SCRIPT_DIR"/upgrade.sh -w $WORLD_NAME -b $BRANCH -s "$DESTINATION"
"$SCRIPT_DIR"/upgrade.sh -w $WORLD_NAME -b $BRANCH -f mtconf -s "$DESTINATION"
2022-05-27 14:36:02 +02:00
echo "Please, review and edit the following files:"
echo -e "\t- $MINETEST_DIR/minetest.conf"
echo -e "\t- $DESTINATION/mods_disabled.txt"
echo
if [[ $MINETEST_MAPPER -eq 1 ]]; then
2022-05-27 14:36:02 +02:00
echo "The use of Minetest Mapper needs ImageMagick with fontconfig and truetype dependencies."
echo "You also need to allow read and write permissions for PS. The configuration can be found in the file that manage the security policy of ImageMagick."
fi
}
2022-05-27 14:36:02 +02:00
action() {
echo "Preparing the installation…"
2022-06-25 16:07:34 +02:00
SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" &> /dev/null && pwd)
2022-05-27 14:36:02 +02:00
DESTINATION="$1"
2022-06-25 16:07:34 +02:00
[ -z $DESTINATION ] && DESTINATION="$SCRIPT_DIR"
2022-05-27 14:36:02 +02:00
GIT=$(which git)
CMAKE=$(which cmake)
MAKE=$(which make)
[ ! -e "$GIT" ] && error "git not found"
[ ! -e "$CMAKE" ] && error "cmake not found"
[ ! -e "$MAKE" ] && error "make not found"
if [ -e "$DESTINATION/nalc.conf" ]; then
read -p "Previous installation is detected. Do you want to load previous settings? [Y/n] " continue
if [ -z $continue ] || [[ "$continue" =~ ^[yY][eE][sS]|[yY]$ ]]; then
load_conf "$DESTINATION/nalc.conf"
fi
fi
2022-05-27 14:36:02 +02:00
[ -z $MINETEST_BIN ] && MINETEST_BIN=$(which minetestserver)
[ -z $MINETEST_DIR ] && MINETEST_DIR=~/.minetest
[ -z $MAP_BACKEND ] && MAP_BACKEND=sqlite3
[ -z $PLAYER_BACKEND ] && PLAYER_BACKEND=sqlite3
[ -z $AUTH_BACKEND ] && AUTH_BACKEND=sqlite3
2022-08-22 12:34:55 +02:00
[ -z $MOD_STORAGE_BACKEND ] && MOD_STORAGE_BACKEND=sqlite3
2022-05-27 14:36:02 +02:00
[ -z $MINETEST_MAPPER ] && MINETEST_MAPPER=0
[ -z $PORT ] && PORT=30000
[ -z $URL ] && URL="https://sys4.fr/gitea/"
[[ $URL =~ ^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]+$ ]] && URL="$URL:"
[ -z $BRANCH ] && BRANCH="stable"
[ -z $WORLD_NAME ] && WORLD_NAME="nalc"
[ ! -e $MINETEST_BIN ] && error "$MINETEST_BIN not found"
[ ! -d $MINETEST_DIR ] && error "$MINETEST_DIR not found"
2022-05-27 14:36:02 +02:00
echo "Installation summary:"
echo -e "\t- Server files will be downloaded from $URL"
echo -e "\t- NALC server will be installed to $DESTINATION."
echo -e "\t- World of NALC will be installed to $MINETEST_DIR/worlds/$WORLD_NAME"
echo -e "\t- The minetestserver executable $MINETEST_BIN will be used."
echo -e "\t- The $MINETEST_DIR directory will be used."
echo -e "\t- $MAP_BACKEND will be used as map backend."
echo -e "\t- $PLAYER_BACKEND will be used as player backend."
echo -e "\t- $AUTH_BACKEND will be used as auth backend."
2022-08-22 12:34:55 +02:00
echo -e "\t- $MOD_STORAGE_BACKEND will be used as mod storage backend."
2022-05-27 14:36:02 +02:00
echo -e "\t- Port used: $PORT."
[ $MINETEST_MAPPER -eq 1 ] && echo -e "\t- Minetest mapper support added."
echo
read -p "Installation of NALC server will begin. Continue? [Y/n] " continue
if [ -z $continue ] || [[ "$continue" =~ ^[yY][eE][sS]|[yY]$ ]]; then
save_conf "$DESTINATION/nalc.conf"
install_game
install_mods
install_world
[ $MINETEST_MAPPER -eq 1 ] && install_mtmapper
post_install
2020-07-18 12:37:59 +02:00
fi
2022-05-27 14:36:02 +02:00
echo "End of the NALC server installation. Good Bye!"
exit 0
}
2022-05-27 14:36:02 +02:00
[ $# -lt 0 ] && usage
2022-08-22 12:34:55 +02:00
OPT=$(getopt -o h,b:,d:,m:,p:,a:,s:,M,w:,P: -l help,minetest-bin:,minetest-dir:,map-backend:,player-backend:,auth-backend:,mod-storage-backend:,minetest-mapper,url:,branch:,world:,port: -- "$@")
set -- $OPT
while true; do
case "$1" in
-h|--help)
usage;;
2022-05-27 14:36:02 +02:00
-b|--minetest-bin)
MINETEST_BIN=$(strip "$2")
2020-07-18 12:37:59 +02:00
shift 2;;
2022-05-27 14:36:02 +02:00
-d|--minetest-dir)
MINETEST_DIR=$(strip "$2")
shift 2;;
2022-05-27 14:36:02 +02:00
-m|--map-backend)
MAP_BACKEND=$(strip "$2")
shift 2;;
-p|--player-backend)
PLAYER_BACKEND=$(strip "$2")
shift 2;;
-a|--auth-backend)
AUTH_BACKEND=$(strip "$2")
shift 2;;
2022-08-22 12:34:55 +02:00
-s|--mod-storage-backend)
MOD_STORAGE_BACKEND=$(strip "$2")
shift 2;;
2022-05-27 14:36:02 +02:00
-M|--minetest-mapper)
MINETEST_MAPPER=1
shift 1;;
-w|--world)
WORLD_NAME=$(strip "$2")
2020-07-18 12:37:59 +02:00
shift 2;;
2022-05-27 14:36:02 +02:00
-P|--port)
PORT=$(strip $2)
shift 2;;
2022-05-27 14:36:02 +02:00
--url)
URL=$(strip "$2")
shift 2;;
--branch)
BRANCH=$(strip "$2")
shift 2;;
2022-05-27 14:36:02 +02:00
--)
shift;;
*)
2022-05-27 14:36:02 +02:00
action $(strip "$1")
shift;;
esac
done
2022-05-27 14:36:02 +02:00