command_fastdl.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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" "resource")
  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. elif [ "${directory}" == "resource" ]; then
  167. local allowed_extentions_array=("*")
  168. fi
  169. for allowed_extention in "${allowed_extentions_array[@]}"; do
  170. fileswc=0
  171. tput sc
  172. while read -r ext; do
  173. ((fileswc++))
  174. tput rc
  175. tput el
  176. fn_print "gathering ${directory} ${allowed_extention} : ${fileswc}"
  177. echo -e "${ext}" >> "${tmpdir}/fastdl_files_to_compress.txt"
  178. done < <(find "${systemdir}/${directory}" -type f -iname "${allowed_extention}")
  179. tput rc
  180. tput el
  181. fn_print "gathering ${directory} ${allowed_extention} : ${fileswc}"
  182. if [ ${fileswc} != 0 ]; then
  183. fn_print_ok_eol_nl
  184. else
  185. fn_print_info_eol_nl
  186. fi
  187. done
  188. fi
  189. done
  190. fi
  191. if [ -f "${tmpdir}/fastdl_files_to_compress.txt" ]; then
  192. fn_print_nl "calculating total file size"
  193. fn_sleep_time_1
  194. totalfiles=$(wc -l < "${tmpdir}/fastdl_files_to_compress.txt")
  195. # Calculates total file size.
  196. while read -r dufile; do
  197. filesize=$(stat -c %s "${dufile}")
  198. filesizetotal=$((filesizetotal + filesize))
  199. exitcode=$?
  200. if [ "${exitcode}" -ne 0 ]; then
  201. fn_print_fail_eol_nl
  202. fn_script_log_fail "Calculating total file size"
  203. core_exit.sh
  204. fi
  205. done < "${tmpdir}/fastdl_files_to_compress.txt"
  206. else
  207. fn_print_fail_eol_nl "generating file list"
  208. fn_script_log_fail "Generating file list."
  209. core_exit.sh
  210. fi
  211. if [ "${engine}" == "source" ]; then
  212. echo -e "about to compress ${totalfiles} files, total size $(fn_human_readable_file_size "${filesizetotal}" 0)"
  213. elif [ "${engine}" == "goldsrc" ]; then
  214. echo -e "about to copy ${totalfiles} files, total size $(fn_human_readable_file_size "${filesizetotal}" 0)"
  215. fi
  216. fn_script_log_info "${totalfiles} files, total size $(fn_human_readable_file_size "${filesizetotal}" 0)"
  217. rm -f "${tmpdir:?}/fastdl_files_to_compress.txt"
  218. if ! fn_prompt_yn "Continue?" Y; then
  219. exitcode=0
  220. core_exit.sh
  221. fi
  222. }
  223. # Builds Garry's Mod fastdl directory content.
  224. fn_fastdl_gmod() {
  225. cd "${systemdir}" || exit
  226. for allowed_extention in "${allowed_extentions_array[@]}"; do
  227. fileswc=0
  228. tput sc
  229. while read -r fastdlfile; do
  230. ((fileswc++))
  231. tput rc
  232. tput el
  233. fn_print "copying ${allowed_extention} : ${fileswc}"
  234. cp --parents "${fastdlfile}" "${fastdldir}"
  235. exitcode=$?
  236. if [ "${exitcode}" -ne 0 ]; then
  237. fn_print_fail_eol_nl
  238. fn_script_log_fail "Copying ${fastdlfile} > ${fastdldir}"
  239. core_exit.sh
  240. else
  241. fn_script_log_pass "Copying ${fastdlfile} > ${fastdldir}"
  242. fi
  243. done < <(find . -type f -iname "${allowed_extention}")
  244. if [ ${fileswc} != 0 ]; then
  245. fn_print_ok_eol_nl
  246. fi
  247. done
  248. # Correct addons directory structure for Fastdl.
  249. if [ -d "${fastdldir}/addons" ]; then
  250. fn_print "updating addons file structure..."
  251. cp -Rf "${fastdldir}"/addons/*/* "${fastdldir}"
  252. exitcode=$?
  253. if [ "${exitcode}" -ne 0 ]; then
  254. fn_print_fail_eol_nl
  255. fn_script_log_fail "Updating addons file structure"
  256. core_exit.sh
  257. else
  258. fn_print_ok_eol_nl
  259. fn_script_log_pass "Updating addons file structure"
  260. fi
  261. # Clear addons directory in fastdl.
  262. fn_print "clearing addons dir from fastdl dir..."
  263. fn_sleep_time_1
  264. rm -rf "${fastdldir:?}/addons"
  265. exitcode=$?
  266. if [ "${exitcode}" -ne 0 ]; then
  267. fn_print_fail_eol_nl
  268. fn_script_log_fail "Clearing addons dir from fastdl dir"
  269. core_exit.sh
  270. else
  271. fn_print_ok_eol_nl
  272. fn_script_log_pass "Clearing addons dir from fastdl dir"
  273. fi
  274. fi
  275. # Correct content that may be into a lua directory by mistake like some darkrpmodification addons.
  276. if [ -d "${fastdldir}/lua" ]; then
  277. fn_print "correcting DarkRP files..."
  278. fn_sleep_time_1
  279. cp -Rf "${fastdldir}/lua/"* "${fastdldir}"
  280. exitcode=$?
  281. if [ "${exitcode}" -ne 0 ]; then
  282. fn_print_fail_eol_nl
  283. fn_script_log_fail "Correcting DarkRP files"
  284. core_exit.sh
  285. else
  286. fn_print_ok_eol_nl
  287. fn_script_log_pass "Correcting DarkRP files"
  288. fi
  289. fi
  290. if [ -f "${tmpdir}/fastdl_files_to_compress.txt" ]; then
  291. totalfiles=$(wc -l < "${tmpdir}/fastdl_files_to_compress.txt")
  292. # Calculates total file size.
  293. while read -r dufile; do
  294. filesize=$(du -b "${dufile}" | awk '{ print $1 }')
  295. filesizetotal=$((filesizetotal + filesize))
  296. done < "${tmpdir}/fastdl_files_to_compress.txt"
  297. fi
  298. }
  299. fn_fastdl_source() {
  300. for directory in "${fastdl_directories_array[@]}"; do
  301. if [ -d "${systemdir}/${directory}" ]; then
  302. if [ "${directory}" == "maps" ]; then
  303. local allowed_extentions_array=("*.bsp" "*.ain" "*.nav" "*.jpg" "*.txt")
  304. elif [ "${directory}" == "materials" ]; then
  305. local allowed_extentions_array=("*.vtf" "*.vmt" "*.vbf" "*.png" "*.svg")
  306. elif [ "${directory}" == "models" ]; then
  307. local allowed_extentions_array=("*.vtx" "*.vvd" "*.mdl" "*.phy" "*.jpg" "*.png")
  308. elif [ "${directory}" == "particles" ]; then
  309. local allowed_extentions_array=("*.pcf")
  310. elif [ "${directory}" == "sound" ]; then
  311. local allowed_extentions_array=("*.wav" "*.mp3" "*.ogg")
  312. elif [ "${directory}" == "resource" ]; then
  313. local allowed_extentions_array=("*")
  314. fi
  315. for allowed_extention in "${allowed_extentions_array[@]}"; do
  316. fileswc=0
  317. tput sc
  318. while read -r fastdlfile; do
  319. ((fileswc++))
  320. tput rc
  321. tput el
  322. fn_print "copying ${directory} ${allowed_extention} : ${fileswc}"
  323. fn_sleep_time
  324. # get relative path of file in the dir
  325. tmprelfilepath="${fastdlfile#"${systemdir}/"}"
  326. copytodir="${tmprelfilepath%/*}"
  327. # create relative path for fastdl
  328. if [ ! -d "${fastdldir}/${copytodir}" ]; then
  329. mkdir -p "${fastdldir}/${copytodir}"
  330. fi
  331. cp "${fastdlfile}" "${fastdldir}/${copytodir}"
  332. exitcode=$?
  333. if [ "${exitcode}" -ne 0 ]; then
  334. fn_print_fail_eol_nl
  335. fn_script_log_fail "Copying ${fastdlfile} > ${fastdldir}/${copytodir}"
  336. core_exit.sh
  337. else
  338. fn_script_log_pass "Copying ${fastdlfile} > ${fastdldir}/${copytodir}"
  339. fi
  340. done < <(find "${systemdir}/${directory}" -type f -iname "${allowed_extention}")
  341. if [ ${fileswc} != 0 ]; then
  342. fn_print_ok_eol_nl
  343. fi
  344. done
  345. fi
  346. done
  347. }
  348. # Builds the fastdl directory content.
  349. fn_fastdl_build() {
  350. # Copy all needed files for Fastdl.
  351. fn_print_nl "copying files to ${fastdldir}"
  352. fn_script_log_info "Copying files to ${fastdldir}"
  353. if [ "${shortname}" == "gmod" ]; then
  354. fn_fastdl_gmod
  355. fn_fastdl_gmod_dl_enforcer
  356. else
  357. fn_fastdl_source
  358. fi
  359. }
  360. # Generate lua file that will force download any file into the Fastdl directory.
  361. fn_fastdl_gmod_dl_enforcer() {
  362. # Clear old lua file.
  363. if [ -f "${luafastdlfullpath}" ]; then
  364. fn_print "removing existing download enforcer: ${luafastdlfile}"
  365. rm -f "${luafastdlfullpath:?}"
  366. exitcode=$?
  367. if [ "${exitcode}" -ne 0 ]; then
  368. fn_print_fail_eol_nl
  369. fn_script_log_fail "Removing existing download enforcer ${luafastdlfullpath}"
  370. core_exit.sh
  371. else
  372. fn_print_ok_eol_nl
  373. fn_script_log_pass "Removing existing download enforcer ${luafastdlfullpath}"
  374. fi
  375. fi
  376. # Generate new one if user said yes.
  377. if [ "${luaresource}" == "on" ]; then
  378. fn_print "creating new download enforcer: ${luafastdlfile}"
  379. touch "${luafastdlfullpath}"
  380. # Read all filenames and put them into a lua file at the right path.
  381. while read -r line; do
  382. echo -e "resource.AddFile( \"${line}\" )" >> "${luafastdlfullpath}"
  383. done < <(find "${fastdldir:?}" \( -type f ! -name "*.bz2" \) -printf '%P\n')
  384. exitcode=$?
  385. if [ "${exitcode}" -ne 0 ]; then
  386. fn_print_fail_eol_nl
  387. fn_script_log_fail "Creating new download enforcer ${luafastdlfullpath}"
  388. core_exit.sh
  389. else
  390. fn_print_ok_eol_nl
  391. fn_script_log_pass "Creating new download enforcer ${luafastdlfullpath}"
  392. fi
  393. fi
  394. }
  395. # Compresses Fastdl files using bzip2.
  396. fn_fastdl_bzip2() {
  397. while read -r filetocompress; do
  398. fn_print "compressing ${filetocompress}"
  399. bzip2 -f "${filetocompress}"
  400. exitcode=$?
  401. if [ "${exitcode}" -ne 0 ]; then
  402. fn_print_fail_eol_nl
  403. fn_script_log_fail "Compressing ${filetocompress}"
  404. core_exit.sh
  405. else
  406. fn_print_ok_eol_nl
  407. fn_script_log_pass "Compressing ${filetocompress}"
  408. fi
  409. done < <(find "${fastdldir:?}" \( -type f ! -name "*.bz2" \))
  410. }
  411. check.sh
  412. # Run functions.
  413. fn_fastdl_preview
  414. fn_clear_old_fastdl
  415. fn_fastdl_dirs
  416. fn_fastdl_build
  417. if [ "${engine}" == "source" ]; then
  418. fn_fastdl_bzip2
  419. fi
  420. # Finished message.
  421. fn_print_nl "Fastdl files are located in:"
  422. fn_print_nl "${fastdldir}"
  423. fn_print_nl "Fastdl completed"
  424. fn_script_log_info "Fastdl completed"
  425. core_exit.sh