command_mods_update.sh 4.0 KB

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