command_fastdl.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. #!/bin/bash
  2. # LinuxGSM command_fastdl.sh function
  3. # Author: Daniel Gibbs
  4. # Contributor: UltimateByte
  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. check.sh
  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. # Check if bzip2 is installed.
  22. if [ ! "$(command -v bzip2 2>/dev/null)" ]; then
  23. fn_print_fail "bzip2 is not installed"
  24. fn_script_log_fatal "bzip2 is not installed"
  25. core_exit.sh
  26. fi
  27. # Header
  28. fn_print_header
  29. echo -e "More info: https://docs.linuxgsm.com/commands/fastdl"
  30. echo -e ""
  31. # Prompts user for FastDL creation settings.
  32. echo -e "${commandaction} setup"
  33. echo -e "================================="
  34. # Prompt for clearing old files if directory was already here.
  35. if [ -d "${fastdldir}" ]; then
  36. fn_print_warning_nl "FastDL directory already exists."
  37. echo -e "${fastdldir}"
  38. echo -e ""
  39. if fn_prompt_yn "Overwrite existing directory?" Y; then
  40. fn_script_log_info "Overwrite existing directory: YES"
  41. else
  42. core_exit.sh
  43. fi
  44. fi
  45. # Garry's Mod Specific.
  46. if [ "${shortname}" == "gmod" ]; then
  47. # Prompt for download enforcer, which is using a .lua addfile resource generator.
  48. if fn_prompt_yn "Force clients to download files?" Y; then
  49. luaresource="on"
  50. fn_script_log_info "Force clients to download files: YES"
  51. else
  52. luaresource="off"
  53. fn_script_log_info "Force clients to download filesr: NO"
  54. fi
  55. fi
  56. # Clears any fastdl directory content.
  57. fn_clear_old_fastdl(){
  58. # Clearing old FastDL.
  59. if [ -d "${fastdldir}" ]; then
  60. echo -en "clearing existing FastDL directory ${fastdldir}..."
  61. rm -fR "${fastdldir:?}"
  62. exitcode=$?
  63. if [ ${exitcode} -ne 0 ]; then
  64. fn_print_fail_eol_nl
  65. fn_script_log_fatal "Clearing existing FastDL directory ${fastdldir}"
  66. core_exit.sh
  67. else
  68. fn_print_ok_eol_nl
  69. fn_script_log_pass "Clearing existing FastDL directory ${fastdldir}"
  70. fi
  71. fn_sleep_time
  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} -ne 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. fn_sleep_time
  89. fi
  90. if [ ! -d "${fastdldir}" ]; then
  91. echo -en "creating fastdl directory ${fastdldir}..."
  92. mkdir -p "${fastdldir}"
  93. exitcode=$?
  94. if [ ${exitcode} -ne 0 ]; then
  95. fn_print_fail_eol_nl
  96. fn_script_log_fatal "Creating fastdl directory ${fastdldir}"
  97. core_exit.sh
  98. else
  99. fn_print_ok_eol_nl
  100. fn_script_log_pass "Creating fastdl directory ${fastdldir}"
  101. fi
  102. fn_sleep_time
  103. fi
  104. }
  105. # Using this gist https://gist.github.com/agunnerson-ibm/efca449565a3e7356906
  106. fn_human_readable_file_size(){
  107. local abbrevs=(
  108. $((1 << 60)):ZB
  109. $((1 << 50)):EB
  110. $((1 << 40)):TB
  111. $((1 << 30)):GB
  112. $((1 << 20)):MB
  113. $((1 << 10)):KB
  114. $((1)):bytes
  115. )
  116. local bytes="${1}"
  117. local precision="${2}"
  118. if [[ "${bytes}" == "1" ]]; then
  119. echo -e "1 byte"
  120. else
  121. for item in "${abbrevs[@]}"; do
  122. local factor="${item%:*}"
  123. local abbrev="${item#*:}"
  124. if [[ "${bytes}" -ge "${factor}" ]]; then
  125. size=$(bc -l <<< "${bytes} / ${factor}")
  126. printf "%.*f %s\n" "${precision}" "${size}" "${abbrev}"
  127. break
  128. fi
  129. done
  130. fi
  131. }
  132. # Provides info about the fastdl directory content and prompts for confirmation.
  133. fn_fastdl_preview(){
  134. # Remove any file list.
  135. if [ -f "${tmpdir}/fastdl_files_to_compress.txt" ]; then
  136. rm -f "${tmpdir:?}/fastdl_files_to_compress.txt"
  137. fi
  138. echo -e "analysing required files"
  139. fn_script_log_info "Analysing required files"
  140. # Garry's Mod
  141. if [ "${shortname}" == "gmod" ]; then
  142. cd "${systemdir}" || exit
  143. allowed_extentions_array=( "*.ain" "*.bsp" "*.mdl" "*.mp3" "*.ogg" "*.otf" "*.pcf" "*.phy" "*.png" "*.svg" "*.vtf" "*.vmt" "*.vtx" "*.vvd" "*.ttf" "*.wav" )
  144. for allowed_extention in "${allowed_extentions_array[@]}"; do
  145. fileswc=0
  146. tput sc
  147. while read -r ext; do
  148. ((fileswc++))
  149. tput rc; tput el
  150. echo -e "gathering ${allowed_extention} : ${fileswc}..."
  151. echo -e "${ext}" >> "${tmpdir}/fastdl_files_to_compress.txt"
  152. done < <(find . -type f -iname "${allowed_extention}")
  153. if [ ${fileswc} != 0 ]; then
  154. fn_print_ok_eol_nl
  155. else
  156. fn_print_info_eol_nl
  157. fi
  158. done
  159. # Source engine
  160. else
  161. fastdl_directories_array=( "maps" "materials" "models" "particles" "sound" "resources" )
  162. for directory in "${fastdl_directories_array[@]}"; do
  163. if [ -d "${systemdir}/${directory}" ]; then
  164. if [ "${directory}" == "maps" ]; then
  165. local allowed_extentions_array=( "*.bsp" "*.ain" "*.nav" "*.jpg" "*.txt" )
  166. elif [ "${directory}" == "materials" ]; then
  167. local allowed_extentions_array=( "*.vtf" "*.vmt" "*.vbf" "*.png" "*.svg" )
  168. elif [ "${directory}" == "models" ]; then
  169. local allowed_extentions_array=( "*.vtx" "*.vvd" "*.mdl" "*.phy" "*.jpg" "*.png" )
  170. elif [ "${directory}" == "particles" ]; then
  171. local allowed_extentions_array=( "*.pcf" )
  172. elif [ "${directory}" == "sound" ]; then
  173. local allowed_extentions_array=( "*.wav" "*.mp3" "*.ogg" )
  174. fi
  175. for allowed_extention in "${allowed_extentions_array[@]}"; do
  176. fileswc=0
  177. tput sc
  178. while read -r ext; do
  179. ((fileswc++))
  180. tput rc; 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; tput el
  185. echo -e "gathering ${directory} ${allowed_extention} : ${fileswc}..."
  186. if [ ${fileswc} != 0 ]; then
  187. fn_print_ok_eol_nl
  188. else
  189. fn_print_info_eol_nl
  190. fi
  191. done
  192. fi
  193. done
  194. fi
  195. if [ -f "${tmpdir}/fastdl_files_to_compress.txt" ]; then
  196. echo -e "calculating total file size..."
  197. fn_sleep_time
  198. totalfiles=$(wc -l < "${tmpdir}/fastdl_files_to_compress.txt")
  199. # Calculates total file size.
  200. while read -r dufile; do
  201. filesize=$(stat -c %s "${dufile}")
  202. filesizetotal=$(( filesizetotal+filesize ))
  203. exitcode=$?
  204. if [ "${exitcode}" != 0 ]; then
  205. fn_print_fail_eol_nl
  206. fn_script_log_fatal "Calculating total file size."
  207. core_exit.sh
  208. fi
  209. done <"${tmpdir}/fastdl_files_to_compress.txt"
  210. else
  211. fn_print_fail_eol_nl "generating file list"
  212. fn_script_log_fatal "Generating file list."
  213. core_exit.sh
  214. fi
  215. echo -e "about to compress ${totalfiles} files, total size $(fn_human_readable_file_size ${filesizetotal} 0)"
  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. fn_script_log "User exited"
  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[@]}"
  227. do
  228. fileswc=0
  229. tput sc
  230. while read -r fastdlfile; do
  231. ((fileswc++))
  232. tput rc; tput el
  233. echo -e "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_fatal "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. echo -en "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_fatal "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. echo -en "clearing addons dir from fastdl dir..."
  263. fn_sleep_time
  264. rm -fR "${fastdldir:?}/addons"
  265. exitcode=$?
  266. if [ ${exitcode} -ne 0 ]; then
  267. fn_print_fail_eol_nl
  268. fn_script_log_fatal "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. echo -en "correcting DarkRP files..."
  278. fn_sleep_time
  279. cp -Rf "${fastdldir}/lua/"* "${fastdldir}"
  280. exitcode=$?
  281. if [ ${exitcode} -ne 0 ]; then
  282. fn_print_fail_eol_nl
  283. fn_script_log_fatal "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[@]}"
  301. 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[@]}"
  315. do
  316. fileswc=0
  317. tput sc
  318. while read -r fastdlfile; do
  319. ((fileswc++))
  320. tput rc; 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} -ne 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} -ne 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} -ne 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} -ne 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