command_fastdl.sh 13 KB

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