command_fastdl.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. #!/bin/bash
  2. # LinuxGSM command_fastdl.sh module
  3. # Author: Daniel Gibbs
  4. # Contributors: https://linuxgsm.com/contrib
  5. # Website: https://linuxgsm.com
  6. # Description: Creates a Fastdl directory.
  7. commandname="FASTDL"
  8. commandaction="Fastdl"
  9. moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  10. fn_firstcommand_set
  11. # Directories.
  12. if [ -z "${webdir}" ]; then
  13. webdir="${rootdir}/public_html"
  14. fi
  15. fastdldir="${webdir}/fastdl"
  16. addonsdir="${systemdir}/addons"
  17. # Server lua autorun dir, used to autorun lua on client connect to the server.
  18. luasvautorundir="${systemdir}/lua/autorun/server"
  19. luafastdlfile="lgsm_cl_force_fastdl.lua"
  20. luafastdlfullpath="${luasvautorundir}/${luafastdlfile}"
  21. # Header
  22. fn_print_header
  23. fn_print_nl "More info: ${italic}https://docs.linuxgsm.com/commands/fastdl"
  24. fn_print_nl ""
  25. # Prompts user for Fastdl creation settings.
  26. fn_print_nl "${bold}${lightyellow}${commandaction} Setup"
  27. fn_messages_separator
  28. # Prompt for clearing old files if directory was already here.
  29. if [ -d "${fastdldir}" ]; then
  30. fn_print_warning_nl "Fastdl directory already exists."
  31. fn_print_nl "${fastdldir}"
  32. fn_print_nl ""
  33. if fn_prompt_yn "Overwrite existing directory?" Y; then
  34. fn_script_log_info "Overwrite existing directory: YES"
  35. else
  36. core_exit.sh
  37. fi
  38. fi
  39. # Garry's Mod Specific.
  40. if [ "${shortname}" == "gmod" ]; then
  41. # Prompt for download enforcer, which is using a .lua addfile resource generator.
  42. if fn_prompt_yn "Force clients to download files?" Y; then
  43. luaresource="on"
  44. fn_script_log_info "Force clients to download files: YES"
  45. else
  46. luaresource="off"
  47. fn_script_log_info "Force clients to download filesr: NO"
  48. fi
  49. fi
  50. # Clears any fastdl directory content.
  51. fn_clear_old_fastdl() {
  52. # Clearing old Fastdl.
  53. if [ -d "${fastdldir}" ]; then
  54. fn_print "clearing existing Fastdl directory ${fastdldir}"
  55. rm -rf "${fastdldir:?}"
  56. exitcode=$?
  57. if [ "${exitcode}" -ne 0 ]; then
  58. fn_print_fail_eol_nl
  59. fn_script_log_fail "Clearing existing Fastdl directory ${fastdldir}"
  60. core_exit.sh
  61. else
  62. fn_print_ok_eol_nl
  63. fn_script_log_pass "Clearing existing Fastdl directory ${fastdldir}"
  64. fi
  65. fi
  66. }
  67. fn_fastdl_dirs() {
  68. # Check and create directories.
  69. if [ ! -d "${webdir}" ]; then
  70. fn_print "creating web directory ${webdir}"
  71. mkdir -p "${webdir}"
  72. exitcode=$?
  73. if [ "${exitcode}" -ne 0 ]; then
  74. fn_print_fail_eol_nl
  75. fn_script_log_fail "Creating web directory ${webdir}"
  76. core_exit.sh
  77. else
  78. fn_print_ok_eol_nl
  79. fn_script_log_pass "Creating web directory ${webdir}"
  80. fi
  81. fi
  82. if [ ! -d "${fastdldir}" ]; then
  83. fn_print "creating fastdl directory ${fastdldir}"
  84. mkdir -p "${fastdldir}"
  85. exitcode=$?
  86. if [ "${exitcode}" -ne 0 ]; then
  87. fn_print_fail_eol_nl
  88. fn_script_log_fail "Creating fastdl directory ${fastdldir}"
  89. core_exit.sh
  90. else
  91. fn_print_ok_eol_nl
  92. fn_script_log_pass "Creating fastdl directory ${fastdldir}"
  93. fi
  94. fi
  95. }
  96. # Using this gist https://gist.github.com/agunnerson-ibm/efca449565a3e7356906
  97. fn_human_readable_file_size() {
  98. local abbrevs=(
  99. "1152921504606846976:ZB"
  100. "1125899906842624:EB"
  101. "1099511627776:TB"
  102. "1073741824:GB"
  103. "1048576:MB"
  104. "1024:KB"
  105. "1:bytes"
  106. )
  107. local bytes="${1}"
  108. local precision="${2}"
  109. if [[ "${bytes}" == "1" ]]; then
  110. fn_print_nl "1 byte"
  111. else
  112. for item in "${abbrevs[@]}"; do
  113. local factor="${item%:*}"
  114. local abbrev="${item#*:}"
  115. if [[ "${bytes}" -ge "${factor}" ]]; then
  116. size=$(bc -l <<< "${bytes} / ${factor}")
  117. printf "%.*f %s\n" "${precision}" "${size}" "${abbrev}"
  118. break
  119. fi
  120. done
  121. fi
  122. }
  123. # Provides info about the fastdl directory content and prompts for confirmation.
  124. fn_fastdl_preview() {
  125. # Remove any file list.
  126. if [ -f "${tmpdir}/fastdl_files_to_compress.txt" ]; then
  127. rm -f "${tmpdir:?}/fastdl_files_to_compress.txt"
  128. fi
  129. fn_print_nl "analysing required files"
  130. fn_script_log_info "Analysing required files"
  131. # Garry's Mod
  132. if [ "${shortname}" == "gmod" ]; then
  133. cd "${systemdir}" || exit
  134. allowed_extentions_array=("*.ain" "*.bsp" "*.mdl" "*.mp3" "*.ogg" "*.otf" "*.pcf" "*.phy" "*.png" "*.svg" "*.vtf" "*.vmt" "*.vtx" "*.vvd" "*.ttf" "*.wav")
  135. for allowed_extention in "${allowed_extentions_array[@]}"; do
  136. fileswc=0
  137. tput sc
  138. while read -r ext; do
  139. ((fileswc++))
  140. tput rc
  141. tput el
  142. fn_print "gathering ${allowed_extention} : ${fileswc}"
  143. echo -e "${ext}" >> "${tmpdir}/fastdl_files_to_compress.txt"
  144. done < <(find . -type f -iname "${allowed_extention}")
  145. if [ ${fileswc} != 0 ]; then
  146. fn_print_ok_eol_nl
  147. else
  148. fn_print_skip_eol_nl
  149. fi
  150. done
  151. # Source engine
  152. else
  153. fastdl_directories_array=("maps" "materials" "models" "particles" "sound" "resources")
  154. for directory in "${fastdl_directories_array[@]}"; do
  155. if [ -d "${systemdir}/${directory}" ]; then
  156. if [ "${directory}" == "maps" ]; then
  157. local allowed_extentions_array=("*.bsp" "*.ain" "*.nav" "*.jpg" "*.txt")
  158. elif [ "${directory}" == "materials" ]; then
  159. local allowed_extentions_array=("*.vtf" "*.vmt" "*.vbf" "*.png" "*.svg")
  160. elif [ "${directory}" == "models" ]; then
  161. local allowed_extentions_array=("*.vtx" "*.vvd" "*.mdl" "*.phy" "*.jpg" "*.png" "*.vmt" "*.vtf")
  162. elif [ "${directory}" == "particles" ]; then
  163. local allowed_extentions_array=("*.pcf")
  164. elif [ "${directory}" == "sound" ]; then
  165. local allowed_extentions_array=("*.wav" "*.mp3" "*.ogg")
  166. fi
  167. for allowed_extention in "${allowed_extentions_array[@]}"; do
  168. fileswc=0
  169. tput sc
  170. while read -r ext; do
  171. ((fileswc++))
  172. tput rc
  173. tput el
  174. fn_print "gathering ${directory} ${allowed_extention} : ${fileswc}"
  175. echo -e "${ext}" >> "${tmpdir}/fastdl_files_to_compress.txt"
  176. done < <(find "${systemdir}/${directory}" -type f -iname "${allowed_extention}")
  177. tput rc
  178. tput el
  179. fn_print "gathering ${directory} ${allowed_extention} : ${fileswc}"
  180. if [ ${fileswc} != 0 ]; then
  181. fn_print_ok_eol_nl
  182. else
  183. fn_print_info_eol_nl
  184. fi
  185. done
  186. fi
  187. done
  188. fi
  189. if [ -f "${tmpdir}/fastdl_files_to_compress.txt" ]; then
  190. fn_print_nl "calculating total file size"
  191. fn_sleep_time_1
  192. totalfiles=$(wc -l < "${tmpdir}/fastdl_files_to_compress.txt")
  193. # Calculates total file size.
  194. while read -r dufile; do
  195. filesize=$(stat -c %s "${dufile}")
  196. filesizetotal=$((filesizetotal + filesize))
  197. exitcode=$?
  198. if [ "${exitcode}" -ne 0 ]; then
  199. fn_print_fail_eol_nl
  200. fn_script_log_fail "Calculating total file size"
  201. core_exit.sh
  202. fi
  203. done < "${tmpdir}/fastdl_files_to_compress.txt"
  204. else
  205. fn_print_fail_eol_nl "generating file list"
  206. fn_script_log_fail "Generating file list."
  207. core_exit.sh
  208. fi
  209. if [ "${engine}" == "source" ]; then
  210. echo -e "about to compress ${totalfiles} files, total size $(fn_human_readable_file_size "${filesizetotal}" 0)"
  211. elif [ "${engine}" == "goldsrc" ]; then
  212. echo -e "about to copy ${totalfiles} files, total size $(fn_human_readable_file_size "${filesizetotal}" 0)"
  213. fi
  214. fn_script_log_info "${totalfiles} files, total size $(fn_human_readable_file_size "${filesizetotal}" 0)"
  215. rm -f "${tmpdir:?}/fastdl_files_to_compress.txt"
  216. if ! fn_prompt_yn "Continue?" Y; then
  217. exitcode=0
  218. core_exit.sh
  219. fi
  220. }
  221. # Builds Garry's Mod fastdl directory content.
  222. fn_fastdl_gmod() {
  223. cd "${systemdir}" || exit
  224. for allowed_extention in "${allowed_extentions_array[@]}"; do
  225. fileswc=0
  226. tput sc
  227. while read -r fastdlfile; do
  228. ((fileswc++))
  229. tput rc
  230. tput el
  231. fn_print "copying ${allowed_extention} : ${fileswc}"
  232. cp --parents "${fastdlfile}" "${fastdldir}"
  233. exitcode=$?
  234. if [ "${exitcode}" -ne 0 ]; then
  235. fn_print_fail_eol_nl
  236. fn_script_log_fail "Copying ${fastdlfile} > ${fastdldir}"
  237. core_exit.sh
  238. else
  239. fn_script_log_pass "Copying ${fastdlfile} > ${fastdldir}"
  240. fi
  241. done < <(find . -type f -iname "${allowed_extention}")
  242. if [ ${fileswc} != 0 ]; then
  243. fn_print_ok_eol_nl
  244. fi
  245. done
  246. # Correct addons directory structure for Fastdl.
  247. if [ -d "${fastdldir}/addons" ]; then
  248. fn_print "updating addons file structure..."
  249. cp -Rf "${fastdldir}"/addons/*/* "${fastdldir}"
  250. exitcode=$?
  251. if [ "${exitcode}" -ne 0 ]; then
  252. fn_print_fail_eol_nl
  253. fn_script_log_fail "Updating addons file structure"
  254. core_exit.sh
  255. else
  256. fn_print_ok_eol_nl
  257. fn_script_log_pass "Updating addons file structure"
  258. fi
  259. # Clear addons directory in fastdl.
  260. fn_print "clearing addons dir from fastdl dir..."
  261. fn_sleep_time_1
  262. rm -rf "${fastdldir:?}/addons"
  263. exitcode=$?
  264. if [ "${exitcode}" -ne 0 ]; then
  265. fn_print_fail_eol_nl
  266. fn_script_log_fail "Clearing addons dir from fastdl dir"
  267. core_exit.sh
  268. else
  269. fn_print_ok_eol_nl
  270. fn_script_log_pass "Clearing addons dir from fastdl dir"
  271. fi
  272. fi
  273. # Correct content that may be into a lua directory by mistake like some darkrpmodification addons.
  274. if [ -d "${fastdldir}/lua" ]; then
  275. fn_print "correcting DarkRP files..."
  276. fn_sleep_time_1
  277. cp -Rf "${fastdldir}/lua/"* "${fastdldir}"
  278. exitcode=$?
  279. if [ "${exitcode}" -ne 0 ]; then
  280. fn_print_fail_eol_nl
  281. fn_script_log_fail "Correcting DarkRP files"
  282. core_exit.sh
  283. else
  284. fn_print_ok_eol_nl
  285. fn_script_log_pass "Correcting DarkRP files"
  286. fi
  287. fi
  288. if [ -f "${tmpdir}/fastdl_files_to_compress.txt" ]; then
  289. totalfiles=$(wc -l < "${tmpdir}/fastdl_files_to_compress.txt")
  290. # Calculates total file size.
  291. while read -r dufile; do
  292. filesize=$(du -b "${dufile}" | awk '{ print $1 }')
  293. filesizetotal=$((filesizetotal + filesize))
  294. done < "${tmpdir}/fastdl_files_to_compress.txt"
  295. fi
  296. }
  297. fn_fastdl_source() {
  298. for directory in "${fastdl_directories_array[@]}"; do
  299. if [ -d "${systemdir}/${directory}" ]; then
  300. if [ "${directory}" == "maps" ]; then
  301. local allowed_extentions_array=("*.bsp" "*.ain" "*.nav" "*.jpg" "*.txt")
  302. elif [ "${directory}" == "materials" ]; then
  303. local allowed_extentions_array=("*.vtf" "*.vmt" "*.vbf" "*.png" "*.svg")
  304. elif [ "${directory}" == "models" ]; then
  305. local allowed_extentions_array=("*.vtx" "*.vvd" "*.mdl" "*.phy" "*.jpg" "*.png")
  306. elif [ "${directory}" == "particles" ]; then
  307. local allowed_extentions_array=("*.pcf")
  308. elif [ "${directory}" == "sound" ]; then
  309. local allowed_extentions_array=("*.wav" "*.mp3" "*.ogg")
  310. fi
  311. for allowed_extention in "${allowed_extentions_array[@]}"; do
  312. fileswc=0
  313. tput sc
  314. while read -r fastdlfile; do
  315. ((fileswc++))
  316. tput rc
  317. tput el
  318. fn_print "copying ${directory} ${allowed_extention} : ${fileswc}"
  319. fn_sleep_time
  320. # get relative path of file in the dir
  321. tmprelfilepath="${fastdlfile#"${systemdir}/"}"
  322. copytodir="${tmprelfilepath%/*}"
  323. # create relative path for fastdl
  324. if [ ! -d "${fastdldir}/${copytodir}" ]; then
  325. mkdir -p "${fastdldir}/${copytodir}"
  326. fi
  327. cp "${fastdlfile}" "${fastdldir}/${copytodir}"
  328. exitcode=$?
  329. if [ "${exitcode}" -ne 0 ]; then
  330. fn_print_fail_eol_nl
  331. fn_script_log_fail "Copying ${fastdlfile} > ${fastdldir}/${copytodir}"
  332. core_exit.sh
  333. else
  334. fn_script_log_pass "Copying ${fastdlfile} > ${fastdldir}/${copytodir}"
  335. fi
  336. done < <(find "${systemdir}/${directory}" -type f -iname "${allowed_extention}")
  337. if [ ${fileswc} != 0 ]; then
  338. fn_print_ok_eol_nl
  339. fi
  340. done
  341. fi
  342. done
  343. }
  344. # Builds the fastdl directory content.
  345. fn_fastdl_build() {
  346. # Copy all needed files for Fastdl.
  347. fn_print_nl "copying files to ${fastdldir}"
  348. fn_script_log_info "Copying files to ${fastdldir}"
  349. if [ "${shortname}" == "gmod" ]; then
  350. fn_fastdl_gmod
  351. fn_fastdl_gmod_dl_enforcer
  352. else
  353. fn_fastdl_source
  354. fi
  355. }
  356. # Generate lua file that will force download any file into the Fastdl directory.
  357. fn_fastdl_gmod_dl_enforcer() {
  358. # Clear old lua file.
  359. if [ -f "${luafastdlfullpath}" ]; then
  360. fn_print "removing existing download enforcer: ${luafastdlfile}"
  361. rm -f "${luafastdlfullpath:?}"
  362. exitcode=$?
  363. if [ "${exitcode}" -ne 0 ]; then
  364. fn_print_fail_eol_nl
  365. fn_script_log_fail "Removing existing download enforcer ${luafastdlfullpath}"
  366. core_exit.sh
  367. else
  368. fn_print_ok_eol_nl
  369. fn_script_log_pass "Removing existing download enforcer ${luafastdlfullpath}"
  370. fi
  371. fi
  372. # Generate new one if user said yes.
  373. if [ "${luaresource}" == "on" ]; then
  374. fn_print "creating new download enforcer: ${luafastdlfile}"
  375. touch "${luafastdlfullpath}"
  376. # Read all filenames and put them into a lua file at the right path.
  377. while read -r line; do
  378. echo -e "resource.AddFile( \"${line}\" )" >> "${luafastdlfullpath}"
  379. done < <(find "${fastdldir:?}" \( -type f ! -name "*.bz2" \) -printf '%P\n')
  380. exitcode=$?
  381. if [ "${exitcode}" -ne 0 ]; then
  382. fn_print_fail_eol_nl
  383. fn_script_log_fail "Creating new download enforcer ${luafastdlfullpath}"
  384. core_exit.sh
  385. else
  386. fn_print_ok_eol_nl
  387. fn_script_log_pass "Creating new download enforcer ${luafastdlfullpath}"
  388. fi
  389. fi
  390. }
  391. # Compresses Fastdl files using bzip2.
  392. fn_fastdl_bzip2() {
  393. while read -r filetocompress; do
  394. fn_print "compressing ${filetocompress}"
  395. bzip2 -f "${filetocompress}"
  396. exitcode=$?
  397. if [ "${exitcode}" -ne 0 ]; then
  398. fn_print_fail_eol_nl
  399. fn_script_log_fail "Compressing ${filetocompress}"
  400. core_exit.sh
  401. else
  402. fn_print_ok_eol_nl
  403. fn_script_log_pass "Compressing ${filetocompress}"
  404. fi
  405. done < <(find "${fastdldir:?}" \( -type f ! -name "*.bz2" \))
  406. }
  407. check.sh
  408. # Run functions.
  409. fn_fastdl_preview
  410. fn_clear_old_fastdl
  411. fn_fastdl_dirs
  412. fn_fastdl_build
  413. if [ "${engine}" == "source" ]; then
  414. fn_fastdl_bzip2
  415. fi
  416. # Finished message.
  417. fn_print_nl "Fastdl files are located in:"
  418. fn_print_nl "${fastdldir}"
  419. fn_print_nl "Fastdl completed"
  420. fn_script_log_info "Fastdl completed"
  421. core_exit.sh