minetest-server-docker/install.sh

229 lines
7.1 KiB
Bash
Raw Permalink Normal View History

2024-09-21 20:14:52 +03:00
#!/usr/bin/env bash
2024-09-29 17:49:23 +03:00
source .env
# 1 = json of the game
download_and_install_game() {
2024-09-29 17:49:23 +03:00
download_url=$(curl -X GET --silent "$API_URL/packages/${1}/" | jq ".url" | tr -d '"')
cd games || exit
wget -O game.zip "${download_url}"
2024-09-29 18:57:11 +03:00
unzip -oq game.zip
2024-09-28 16:35:28 +03:00
rm game.zip
2024-09-22 03:25:18 +03:00
cd ../ # data
}
mkdir -p data/games
2024-09-28 16:35:28 +03:00
cd data || exit
# All mods with their dependencies
calculated_dependencies=()
#Checking for input
2024-09-29 18:29:50 +03:00
if [[ $SERVER_MODS == "" || $SERVER_GAME == "" || $WORLD_NAME == "" ]]; then
echo "Please, fill .env file before launching this script!"
2024-09-28 16:35:28 +03:00
exit 1
fi
#Parsing mode names
oIFS="$IFS"
IFS=";"
2024-09-28 16:35:28 +03:00
SERVER_MODS=("$SERVER_MODS")
IFS="$oIFS"
#Downloading game
oIFS="$IFS"
2024-09-29 17:49:23 +03:00
IFS=$'/'
read -r -a servergame <<< "$SERVER_GAME"
IFS="$oIFS"
2024-09-29 17:49:23 +03:00
gameid="${SERVER_GAME}"
game_name="${servergame[1]}"
2024-09-28 17:45:44 +03:00
mods_path="games/${game_name}/mods"
2024-09-28 16:35:28 +03:00
2024-09-29 17:49:23 +03:00
download_and_install_game "${gameid}"
2024-09-28 17:45:44 +03:00
# 1 = mod id
2024-09-28 16:35:28 +03:00
is_game () {
type=$(curl --silent -X GET "$API_URL/packages/{$1}/" | jq ".type" | tr -d '"')
if [[ $type = "mod" ]]; then
echo "false"
else
echo "true"
fi
}
2024-09-28 17:45:44 +03:00
# 1 = mod id
download_and_install_mod () {
2024-09-29 20:05:12 +03:00
#Parse mod name
oIFS="$IFS"
IFS=$'/'
read -r -a modid <<< "$1"
IFS="$oIFS"
modauthor="${modid[0]}"
modname="${modid[1]}"
2024-09-28 17:45:44 +03:00
mod_url=$(curl --silent -X GET "$API_URL/packages/${1}/" | jq ".url" | tr -d '"')
2024-09-29 20:05:12 +03:00
mod_file="${mods_path}/${modname}.zip"
2024-09-28 17:45:44 +03:00
echo "Writing to ${mod_file} content from url ${mod_url}"
wget -O "${mod_file}" "${mod_url}"
2024-09-29 20:05:12 +03:00
unzip -l "${mod_file}" | grep -q mod.conf
if [[ "$?" == "0" ]]; then # mod.conf found, this is a normal mod
unzip -oq "${mod_file}" -d "${mods_path}"
else # mod.conf not found, mod is deprecated
echo "Warning! mod ${modname} is deprecated!"
deprecated_mod_path="${mods_path}/${modname}"
echo "Installing it to ${deprecated_mod_path}"
mkdir -p "${deprecated_mod_path}"
unzip -oq "${mod_file}" -d "${deprecated_mod_path}"
fi
2024-09-28 17:45:44 +03:00
rm "${mod_file}"
}
2024-09-28 16:35:28 +03:00
# 1 = mod id
get_dependencies () {
2024-09-29 18:19:05 +03:00
calculated_dependencies+=("$1")
2024-09-28 16:35:28 +03:00
echo Checking deps for "$1"
deps=$(curl --silent -X GET "$API_URL/packages/${1}/dependencies/")
deps=$(echo "$deps" | jq ".\"${1}\"")
mapfile -t deps_array < <(echo "$deps" | jq ".[] | length")
amount_of_deps="${#deps_array[@]}"
2024-09-29 17:49:23 +03:00
if [ "$amount_of_deps" -eq 0 ]; then
2024-09-28 16:35:28 +03:00
echo "No deps for mod"
return
fi
for (( i = 0 ; i < amount_of_deps-1 ; i++ )); do
#Parse info about dependency
dependency=$(echo "$deps" | jq ".[${i}]")
dependency_name=$(echo "$dependency" | jq ".name")
is_optional=$(echo "$dependency" | jq ".is_optional")
possible_candidates=$(echo "${dependency}" | jq ".packages")
possible_candidates_amount=$(echo "${possible_candidates}" | jq "length")
#Checking if the dependency was already satisfied
calculated_dependencies_amount=${#calculated_dependencies[@]}
dependency_satisfied_with_game=false
for (( j = 0 ; j < possible_candidates_amount-1 ; j++ )); do
current_candidate=$(echo "$possible_candidates" | jq ".[${j}]" | tr -d '"')
if [[ $possible_candidates_amount -eq 1 ]]; then
break
fi
#Check if candidate is the game that is installed
if [[ "$current_candidate" == "$gameid" ]]; then
# echo Dependencies satisfied with game
dependency_satisfied_with_game=true
break
fi
#Check if candidate is game that is not installed
if [ "$(is_game "$current_candidate")" = "true" ] && [ "$current_candidate" != "$gameid" ]; then
2024-09-29 17:49:23 +03:00
if [ "${current_candidate}" = "" ] || [ "${current_candidate}" = "null" ]; then
2024-09-28 16:35:28 +03:00
continue;
fi
possible_candidates=$(echo "$possible_candidates" | jq "del(.[] | select( . == \"${current_candidate}\"))")
j=$((j-1))
fi
done
possible_candidates_amount=$(echo "${possible_candidates}" | jq "length")
if [[ $possible_candidates_amount = "" ]]; then
possible_candidates_amount=0
fi
2024-09-29 17:49:23 +03:00
2024-09-28 16:35:28 +03:00
if [[ "$dependency_satisfied_with_game" = true ]]; then
continue
fi
# Check whether one of possible candidates as a dependency was already calculated
if (( "$calculated_dependencies_amount" > 0 )); then
for k in $(seq 0 $((calculated_dependencies_amount-1))); do
for h in $(seq 0 $((possible_candidates_amount-1))); do
if [[ "${calculated_dependencies[$k]}" == "${possible_candidates[$h]}" ]]; then
continue
fi
done
done
# If there's only one candidate for that dependency, then install it without user's confirmation
fi
2024-09-29 17:49:23 +03:00
2024-09-28 16:35:28 +03:00
if (( possible_candidates_amount == 0 )); then
continue
fi
2024-09-29 17:49:23 +03:00
2024-09-28 16:35:28 +03:00
if (( possible_candidates_amount == 1)); then
candidate=$(echo "${possible_candidates[0]}" | jq ".[0]" | tr -d '"')
calculated_dependencies+=("${candidate}")
continue
fi
got_appropriate_answer=false
while [ $got_appropriate_answer != true ]; do
# If dep. is optional you can refuse it
if [[ "$is_optional" = false ]]; then
echo "Mod ${1} requires dependency ${dependency_name}."
else
echo "Mod ${1} asks for an optional dependency ${dependency_name}."
echo "(-1) TO CANCEL THIS DEPENDENCY"
fi
for j in $(seq 0 $((possible_candidates_amount-1))); do
echo \("${j}"\) ["$(echo "$possible_candidates" | jq ".[${j}]")"]
done
printf "> "
read -r chose
if [ "$is_optional" = false ]; then
if [ "$chose" != $((chose)) ] || [ "$chose" -lt 0 ] || [ "$chose" -gt "$possible_candidates_amount" ]; then
echo "Incorrect answer. Please, try again"
continue
else
got_appropriate_answer=true
fi
else
if [ "$chose" != $((chose)) ]; then
echo "Incorrect answer. Please, try again"
else
got_appropriate_answer=true
fi
fi
done
if [ "$chose" = "-1" ] && [ "$is_optional" = true ]; then
continue
fi
2024-09-28 17:45:44 +03:00
chosen=$(echo "$possible_candidates" | jq ".[${chose}]" | tr -d '"')
2024-09-28 16:35:28 +03:00
if [[ ${calculated_dependencies[*]} =~ $chosen ]]; then
continue
else
calculated_dependencies+=("$chosen")
fi
done
}
2024-09-22 03:25:18 +03:00
#Downloading mods
2024-09-28 16:35:28 +03:00
#Array to store mods that was required by user
2024-09-22 03:25:18 +03:00
mods_to_install=("")
oIFS="$IFS"
IFS=";"
2024-09-28 16:35:28 +03:00
read -r -a mods_to_install <<< "$SERVER_MODS"
2024-09-22 03:25:18 +03:00
IFS="$oIFS"
2024-09-28 17:45:44 +03:00
for i in "${mods_to_install[@]}"; do
get_dependencies "${i}"
2024-09-28 16:35:28 +03:00
done
2024-09-28 17:45:44 +03:00
echo "Following mods will be installed: ${calculated_dependencies[*]}"
for i in "${calculated_dependencies[@]}"; do
echo "Installing ${i}"
download_and_install_mod $i
done