#!/usr/bin/env bash source ./.env # 1 = json of the game download_and_install_game() { download_url=$(echo "${1}" | jq ".url" | tr -d '"') cd games || exit wget -O game.zip "${download_url}" unzip 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 == "" ]]; then echo "Please, fill server.env file before launching this script!" exit 1 fi #Parsing mode names oIFS="$IFS" IFS=";" SERVER_MODS=("$SERVER_MODS") IFS="$oIFS" #Downloading game results=$(curl --silent -X GET "$API_URL/packages/?type=game&q=$SERVER_GAME") oIFS="$IFS" IFS=$'\n' mapfile -t authors < <(echo "$results" | jq '.[].author') IFS="$oIFS" result_amount="${#authors[@]}" for i in $(seq 0 $((result_amount-1))); do author=$(echo "$results" | jq ".[${i}].author" | tr -d '"') name=$(echo "$results" | jq ".[${i}].name" | tr -d '"') desc=$(echo "$results" | jq ".[${i}].short_description" | tr -d '"') echo "{${i}} [${author}] (${name}): ${desc}" done chosen="" while true do read -r answer chosen=$(echo "$results" | jq ".[${answer}]") if [[ "$chosen" = null ]]; then echo "Incorrect answer. Try again" else break fi done chosen_author=$(echo "$chosen" | jq ".author" | tr -d '"') chosen_packet_name=$(echo "$chosen" | jq ".name" | tr -d '"') request_url="${API_URL}/packages/${chosen_author}/${chosen_packet_name}/" chosen_game=$(curl --silent -X GET "$request_url") # download_and_install_game "${chosen_game}" gameid=$(echo "$chosen_game" | jq ".author" | tr -d '"') gameid="$gameid/$(echo "$chosen_game" | jq ".name" | tr -d '"')" # 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 get_dependencies () { # declare -a calculated_dependencies 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 in $(seq 0 $((possible_candidates_amount-1))); do for (( j = 0 ; j < possible_candidates_amount-1 ; j++ )); do current_candidate=$(echo "$possible_candidates" | jq ".[${j}]" | tr -d '"') # possible_candidates_amount=$(echo "${possible_candidates}" | jq "length") 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}]") 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" asked_mods_amount=${#mods_to_install[@]} for i in $(seq 0 $((asked_mods_amount-1))); do get_dependencies "${mods_to_install[${i}]}" done echo "Result: ${calculated_dependencies[*]}"