Files
dotfiles/scripts/shell/mmm-iterate-modlist.sh

291 lines
7.8 KiB
Bash
Executable File

#!/bin/bash
show_help() {
echo "Usage: `basename $0` [--mods-names|-m mods] [--include-default-mods|-d true] [--interactive-mode|-i true]"
echo "Options:"
echo " --mods-names|-m (required) Specify mods names list, separation by comma (,)."
echo " Run \"--use-ids true\" to use ids instead of names for mod installations"
echo " --include-default-mods|-d (optional) Includes a list of default mods."
echo " Run \"--list-default-mods true\" to list the default mods."
echo " --interactive-mode|-i (optional) The script will ask for installation for each mod and show warnings."
}
if ! test -f modlist.json; then
echo "This script uses the mmm (meza/minecraft-mod-manager) tool to detect and install mods."
echo "Please ensure that you are inside a .minecraft folder that contains an initialized mmm setup."
echo "mmm's GitHub repository: https://github.com/meza/minecraft-mod-manager"
exit 1
fi
modsnames_csv=''
use_ids=false
should_include_default_mods=false
list_default_mods=false
interactive_mode=false
while [ "$#" -gt 0 ]; do
case "$1" in
--help|-h) show_help; exit ;;
--mods-names|-m) modsnames_csv="$2"; shift 2 ;;
--use-ids) use_ids=true; shift 2 ;;
--include-default-mods|-d) should_include_default_mods=true; shift 2 ;;
--list-default-mods) list_default_mods=true; shift 2 ;;
--interactive-mode|-i) interactive_mode=true; shift 2 ;;
*) shift ;;
esac
done
default_mods_ids=(
"P7dR8mSH", # Fabric API
)
if $list_default_mods; then
echo "Default mods to be installed:"
for mod_id in ${default_mods_ids[@]}; do
mod_title=(http --verify no "https://api.modrinth.com/v2/project/$mod_id" | jq -r ".title")
mod_url="https://modrinth.com/mod/$mod_id"
echo " $mod_title: $mod_url"
done
fi
if $should_include_default_mods; then
for mod_id in ${default_mods_ids[@]}; do
mmm add modrinth $mod_id
done
fi
modsnames=()
while [ -z "$modsnames_csv" ]; do
echo "Enter mods list, separation by comma (,)"
read modsnames_csv
done
modsnames+=(${modsnames_csv//,/ })
resource_packs=()
skipped_mods=()
mods_length="${#modsnames[@]}"
for (( i=0; i<${mods_length}; i++ )); do
mod=${modsnames[$i]}
clear
echo "Doing \"$mod\" now... ($((i+1))/$((mods_length+1)))"
if $use_ids; then
echo "yes n | mmm add modrinth $mod"
if [ $? -ne 0 ]; then
skipped_mods+=($mod)
fi
continue
fi
req_output=`http --verify no "https://api.modrinth.com/v2/search?query=$mod&index=relevance"`
echo $req_output | jq empty
if [ $? -ne 0 ]; then
if $interactive_mode; then
echo "The query for mod \"$mod\" resulted in a invalid JSON response."
mod_search_url=${mod// /+}
echo "Please search for it manually: https://modrinth.com/mods?q=$mod_search_url"
echo "Press any key to continue..."
read
continue
fi
skipped_mods+=($mod)
fi
mod_search_hits=$(jq -r '.hits' <<< "$req_output")
mod_search_count=$(jq length <<< "$mod_search_hits")
mod_id_selected=''
should_install_mod=false
should_skip_mod=false
should_continue_matches=false
if [ $mod_search_count -eq 0 ]; then
skipped_mods+=($mod)
continue
fi
for ((j=0; j < mod_search_count; j++)); do
mod_search_hit=$(jq -r ".[$j]" <<< "$mod_search_hits")
mod_id=$(jq -r ".project_id" <<< "$mod_search_hit")
mod_slug=$(jq -r ".slug" <<< "$mod_search_hit")
mod_project_type=$(jq -r ".project_type" <<< "$mod_search_hit")
if $interactive_mode; then
echo "We found a match for the \"$mod\" mod! ($((j+1))/$((mod_search_count+1)))"
echo ""
mod_url="https://modrinth.com/mod/${mod_id// /+}"
echo "Mod's URL: $mod_url"
echo ""
echo "Mod's info:"
echo "$mod_search_hit" | jq
echo ""
fi
if [ $mod_project_type == "resourcepack" ]; then
if $interactive_mode; then
echo ""
echo "The found match relates to a resource pack instead of a mod."
echo "Please input 'yes' if you want the script to remind you to install the resource pack separately and skip to the next mod."
echo "Please input 'no' if you want the script to continue looking for more matches for the mod."
while true; do
read -p "Please answer: (y/n): " yn
case $yn in
[Yy]* ) resource_packs+=($mod); should_skip_mod=true; break;;
[Nn]* ) clear; should_continue_matches=true; echo "Looking for more matches..."; sleep 1; echo ""; break;;
* ) echo "Please answer yes or no."; echo "";;
esac
done
fi
resource_packs+=($mod)
should_continue_matches=true
fi
if [ $mod_project_type != "mod" ]; then
if $interactive_mode; then
echo ""
echo "The found match relates to project type that is not a mod (\"$mod_project_type\")."
echo "Please input 'yes' if you want the script to remind you to install the project separately and skip to the next mod."
echo "Please input 'no' if you want the script to continue looking for more matches for the mod."
while true; do
read -p "Please answer: (y/n): " yn
case $yn in
[Yy]* ) resource_packs+=($mod); should_skip_mod=true; break;;
[Nn]* ) clear; should_continue_matches=true; echo "Looking for more matches..."; sleep 1; echo ""; break;;
* ) echo "Please answer yes or no."; echo "";;
esac
done
fi
should_continue_matches=true
fi
if $should_continue_matches; then
should_continue_matches=false
clear
continue
fi
if $should_skip_mod; then
break
fi
should_install_mod=true
if $interactive_mode; then
while true; do
read -p "Does this look right? (y/n/c): " yn
case $yn in
[Yy]* ) should_install_mod=true; break;;
[Nn]* ) clear; echo "Looking for more matches..."; sleep 1; echo ""; break;;
[Cc]* ) should_skip_mod=true; break;;
* ) echo "Please answer yes, no or cancel."; echo "";;
esac
done
fi
if $should_skip_mod; then
skipped_mods+=($mod)
fi
if $should_install_mod || $should_skip_mod; then
break
fi
done
if $should_install_mod; then
clear
grep --quiet $mod_slug modlist.json
if [ $? -eq 0 ]; then
if $interactive_mode; then
echo "The selected mod is already installed."
echo "Skipping..."
echo "Press any key to continue..."
read
fi
continue
fi
if $interactive_mode; then
echo "The following mod will be intalled: \"$mod_slug\""
fi
echo "yes n | mmm add modrinth $mod_id"
read
if [ $? -ne 0 ]; then
skipped_mods+=($mod)
if $interactive_mode; then
echo ""
echo "It seems that the mod installation was not successful."
echo "The script will log this mod as not installed after all mods are processed."
echo "Press any key to continue..."
read
continue
fi
fi
fi
if $should_skip_mod; then
continue
fi
if [ -z "$mod_info_selected" ]; then
skipped_mods+=($mod)
if $interactive_mode; then
echo ""
echo "No match was selected for installation."
echo "The mod \"$mod\" wasn't installed by the script."
echo "This will be logged when all mods are finished."
echo "Press any key to continue..."
read
fi
fi
done
if (( ${#resource_packs[@]} )); then
echo ""
echo "[INFO] There were resource packs found:"
for r in ${resource_packs[@]}; do
echo " $r"
done
fi
if (( ${#skipped_mods[@]} )); then
echo ""
echo "[INFO] There were mods that had their installations skipped:"
for mod in ${skipped_mods[@]}; do
echo " $mod"
done
fi