command_mods_update.sh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #!/bin/bash
  2. # LinuxGSM command_mods_update.sh module
  3. # Author: Daniel Gibbs
  4. # Contributors: https://linuxgsm.com/contrib
  5. # Website: https://linuxgsm.com
  6. # Description: Updates installed mods along with mods_list.sh and mods_core.sh.
  7. commandname="MODS-UPDATE"
  8. commandaction="Updating mods"
  9. moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  10. fn_firstcommand_set
  11. check.sh
  12. mods_core.sh
  13. # Prevents specific files being overwritten upon update (set by ${modkeepfiles}).
  14. # For that matter, remove cfg files after extraction before copying them to destination.
  15. fn_remove_cfg_files() {
  16. if [ "${modkeepfiles}" != "OVERWRITE" ] && [ "${modkeepfiles}" != "NOUPDATE" ]; then
  17. echo -e "the following files/directories will be preserved:"
  18. fn_sleep_time_1
  19. # Count how many files there are to remove.
  20. filestopreserve=$(echo -e "${modkeepfiles}" | awk -F ';' '{ print NF }')
  21. # Test all subvalues of "modkeepfiles" using the ";" separator.
  22. for ((preservefilesindex = 1; preservefilesindex < filestopreserve; preservefilesindex++)); do
  23. # Put the current file we are looking for into a variable.
  24. filetopreserve=$(echo -e "${modkeepfiles}" | awk -F ';' -v x=${preservefilesindex} '{ print $x }')
  25. echo -e " * serverfiles/${filetopreserve}"
  26. # If it matches an existing file that have been extracted delete the file.
  27. if [ -f "${extractdest}/${filetopreserve}" ] || [ -d "${extractdest}/${filetopreserve}" ]; then
  28. rm -r "${extractdest:?}/${filetopreserve}"
  29. # Write the file path in a tmp file, to rebuild a full file list as it is rebuilt upon update.
  30. if [ ! -f "${modsdir}/.removedfiles.tmp" ]; then
  31. touch "${modsdir}/.removedfiles.tmp"
  32. fi
  33. echo -e "${filetopreserve}" >> "${modsdir}/.removedfiles.tmp"
  34. fi
  35. done
  36. fi
  37. }
  38. fn_print_dots "Update addons/mods"
  39. fn_mods_check_installed
  40. fn_print_info_nl "Update addons/mods: ${installedmodscount} addons/mods will be updated"
  41. fn_script_log_info "${installedmodscount} mods or addons will be updated"
  42. fn_mods_installed_list
  43. # Go through all available commands, get details and display them to the user.
  44. for ((ulindex = 0; ulindex < ${#installedmodslist[@]}; ulindex++)); do
  45. # Current mod is the "ulindex" value of the array we're going through.
  46. currentmod="${installedmodslist[ulindex]}"
  47. fn_mod_get_info
  48. # Display installed mods and the update policy.
  49. if [ -z "${modkeepfiles}" ]; then
  50. # If modkeepfiles is not set for some reason, that's a problem.
  51. fn_script_log_error "Could not find update policy for ${modprettyname}"
  52. fn_print_error_nl "Could not find update policy for ${modprettyname}"
  53. exitcode="1"
  54. core_exit.sh
  55. # If the mod won't get updated.
  56. elif [ "${modkeepfiles}" == "NOUPDATE" ]; then
  57. echo -e " * ${red}{modprettyname}${default} (won't be updated)"
  58. # If the mode is just overwritten.
  59. elif [ "${modkeepfiles}" == "OVERWRITE" ]; then
  60. echo -e " * ${modprettyname} (overwrite)"
  61. else
  62. echo -e " * ${yellow}${modprettyname}${default} (retain common custom files)"
  63. fi
  64. done
  65. ## Update
  66. # List all installed mods and apply update.
  67. # Reset line value.
  68. installedmodsline="1"
  69. while [ "${installedmodsline}" -le "${installedmodscount}" ]; do
  70. currentmod=$(sed "${installedmodsline}q;d" "${modsinstalledlistfullpath}")
  71. if [ "${currentmod}" ]; then
  72. fn_mod_get_info
  73. # Don not update mod if the policy is set to "NOUPDATE".
  74. if [ "${modkeepfiles}" == "NOUPDATE" ]; then
  75. fn_print_info "${modprettyname} will not be updated to preserve custom files"
  76. fn_script_log_info "${modprettyname} will not be updated to preserve custom files"
  77. else
  78. echo -e ""
  79. echo -e "==> Updating ${modprettyname}"
  80. fn_create_mods_dir
  81. fn_mods_clear_tmp_dir
  82. fn_mods_create_tmp_dir
  83. fn_mod_install_files
  84. fn_mod_lowercase
  85. fn_remove_cfg_files
  86. fn_mod_create_filelist
  87. fn_mod_copy_destination
  88. fn_mod_add_list
  89. fn_mod_tidy_files_list
  90. fn_mods_clear_tmp_dir
  91. fi
  92. ((installedmodsline++))
  93. else
  94. fn_print_fail "No mod was selected"
  95. fn_script_log_fail "No mod was selected"
  96. exitcode="1"
  97. core_exit.sh
  98. fi
  99. done
  100. echo -e ""
  101. fn_print_ok_nl "Mods update complete"
  102. fn_script_log_info "Mods update complete"
  103. core_exit.sh