#!/usr/bin/env bash source .env # 1 = json of the game download_and_install_game() { download_url=$(curl -X GET --silent "$API_URL/packages/${1}/" | jq ".url" | tr -d '"') cd games || exit wget -O game.zip "${download_url}" unzip -oq game.zip rm game.zip cd ../ # data } mkdir -p data/games cd data || exit # All mods with their dependencies calculated_dependencies=() #Checking for input if [[ $SERVER_MODS == "" || $SERVER_GAME == "" || $WORLD_NAME == "" ]]; then echo "Please, fill .env file before launching this script!" exit 1 fi #Parsing mode names oIFS="$IFS" IFS=";" SERVER_MODS=("$SERVER_MODS") IFS="$oIFS" #Downloading game oIFS="$IFS" IFS=$'/' read -r -a servergame <<< "$SERVER_GAME" IFS="$oIFS" gameid="${SERVER_GAME}" game_name="${servergame[1]}" mods_path="games/${game_name}/mods" download_and_install_game "${gameid}" # 1 = mod id 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 } # 1 = mod id download_and_install_mod () { #Parse mod name oIFS="$IFS" IFS=$'/' read -r -a modid <<< "$1" IFS="$oIFS" modauthor="${modid[0]}" modname="${modid[1]}" mod_url=$(curl --silent -X GET "$API_URL/packages/${1}/" | jq ".url" | tr -d '"') mod_file="${mods_path}/${modname}.zip" echo "Writing to ${mod_file} content from url ${mod_url}" wget -O "${mod_file}" "${mod_url}" 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 rm "${mod_file}" } # 1 = mod id get_dependencies () { calculated_dependencies+=("$1") 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[@]}" if [ "$amount_of_deps" -eq 0 ]; then 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 if [ "${current_candidate}" = "" ] || [ "${current_candidate}" = "null" ]; then 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 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 if (( possible_candidates_amount == 0 )); then continue fi 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 chosen=$(echo "$possible_candidates" | jq ".[${chose}]" | tr -d '"') if [[ ${calculated_dependencies[*]} =~ $chosen ]]; then continue else calculated_dependencies+=("$chosen") fi done } #Downloading mods #Array to store mods that was required by user mods_to_install=("") oIFS="$IFS" IFS=";" read -r -a mods_to_install <<< "$SERVER_MODS" IFS="$oIFS" for i in "${mods_to_install[@]}"; do get_dependencies "${i}" done echo "Following mods will be installed: ${calculated_dependencies[*]}" for i in "${calculated_dependencies[@]}"; do echo "Installing ${i}" download_and_install_mod $i done