command_fastdl.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. fi
  72. }
  73. fn_fastdl_dirs(){
  74. # Check and create directories.
  75. if [ ! -d "${webdir}" ]; then
  76. echo -en "creating web directory ${webdir}..."
  77. mkdir -p "${webdir}"
  78. exitcode=$?
  79. if [ ${exitcode} -ne 0 ]; then
  80. fn_print_fail_eol_nl
  81. fn_script_log_fatal "Creating web directory ${webdir}"
  82. core_exit.sh
  83. else
  84. fn_print_ok_eol_nl
  85. fn_script_log_pass "Creating web directory ${webdir}"
  86. fi
  87. fi
  88. if [ ! -d "${fastdldir}" ]; then
  89. echo -en "creating fastdl directory ${fastdldir}..."
  90. mkdir -p "${fastdldir}"
  91. exitcode=$?
  92. if [ ${exitcode} -ne 0 ]; then
  93. fn_print_fail_eol_nl
  94. fn_script_log_fatal "Creating fastdl directory ${fastdldir}"
  95. core_exit.sh
  96. else
  97. fn_print_ok_eol_nl
  98. fn_script_log_pass "Creating fastdl directory ${fastdldir}"
  99. fi
  100. fi
  101. }
  102. # Using this gist https://gist.github.com/agunnerson-ibm/efca449565a3e7356906
  103. fn_human_readable_file_size(){
  104. local abbrevs=(
  105. $((1 << 60)):ZB
  106. $((1 << 50)):EB
  107. $((1 << 40)):TB
  108. $((1 << 30)):GB
  109. $((1 << 20)):MB
  110. $((1 << 10)):KB
  111. $((1)):bytes
  112. )
  113. local bytes="${1}"
  114. local precision="${2}"
  115. if [[ "${bytes}" == "1" ]]; then
  116. echo -e "1 byte"
  117. else
  118. for item in "${abbrevs[@]}"; do
  119. local factor="${item%:*}"
  120. local abbrev="${item#*:}"
  121. if [[ "${bytes}" -ge "${factor}" ]]; then
  122. size=$(bc -l <<< "${bytes} / ${factor}")
  123. printf "%.*f %s\n" "${precision}" "${size}" "${abbrev}"
  124. break
  125. fi
  126. done
  127. fi
  128. }
  129. # Provides info about the fastdl directory content and prompts for confirmation.
  130. fn_fastdl_preview(){
  131. # Remove any file list.
  132. if [ -f "${tmpdir}/fastdl_files_to_compress.txt" ]; then
  133. rm -f "${tmpdir:?}/fastdl_files_to_compress.txt"
  134. fi
  135. echo -e "analysing required files"
  136. fn_script_log_info "Analysing required files"
  137. # Garry's Mod
  138. if [ "${shortname}" == "gmod" ]; then
  139. cd "${systemdir}" || exit
  140. allowed_extentions_array=( "*.ain" "*.bsp" "*.mdl" "*.mp3" "*.ogg" "*.otf" "*.pcf" "*.phy" "*.png" "*.svg" "*.vtf" "*.vmt" "*.vtx" "*.vvd" "*.ttf" "*.wav" )
  141. for allowed_extention in "${allowed_extentions_array[@]}"; do
  142. fileswc=0
  143. tput sc
  144. while read -r ext; do
  145. ((fileswc++))
  146. tput rc; tput el
  147. echo -e "gathering ${allowed_extention} : ${fileswc}..."
  148. echo -e "${ext}" >> "${tmpdir}/fastdl_files_to_compress.txt"
  149. done < <(find . -type f -iname "${allowed_extention}")
  150. if [ ${fileswc} != 0 ]; then
  151. fn_print_ok_eol_nl
  152. else
  153. fn_print_info_eol_nl
  154. fi
  155. done
  156. # Source engine
  157. else
  158. fastdl_directories_array=( "maps" "materials" "models" "particles" "sound" "resources" )
  159. for directory in "${fastdl_directories_array[@]}"; do
  160. if [ -d "${systemdir}/${directory}" ]; then
  161. if [ "${directory}" == "maps" ]; then
  162. local allowed_extentions_array=( "*.bsp" "*.ain" "*.nav" "*.jpg" "*.txt" )
  163. elif [ "${directory}" == "materials" ]; then
  164. local allowed_extentions_array=( "*.vtf" "*.vmt" "*.vbf" "*.png" "*.svg" )
  165. elif [ "${directory}" == "models" ]; then
  166. local allowed_extentions_array=( "*.vtx" "*.vvd" "*.mdl" "*.phy" "*.jpg" "*.png" )
  167. elif [ "${directory}" == "particles" ]; then
  168. local allowed_extentions_array=( "*.pcf" )
  169. elif [ "${directory}" == "sound" ]; then
  170. local allowed_extentions_array=( "*.wav" "*.mp3" "*.ogg" )
  171. fi
  172. for allowed_extention in "${allowed_extentions_array[@]}"; do
  173. fileswc=0
  174. tput sc
  175. while read -r ext; do
  176. ((fileswc++))
  177. tput rc; tput el
  178. echo -e "gathering ${directory} ${allowed_extention} : ${fileswc}..."
  179. echo -e "${ext}" >> "${tmpdir}/fastdl_files_to_compress.txt"
  180. done < <(find "${systemdir}/${directory}" -type f -iname "${allowed_extention}")
  181. tput rc; tput el
  182. echo -e "gathering ${directory} ${allowed_extention} : ${fileswc}..."
  183. if [ ${fileswc} != 0 ]; then
  184. fn_print_ok_eol_nl
  185. else
  186. fn_print_info_eol_nl
  187. fi
  188. done
  189. fi
  190. done
  191. fi
  192. if [ -f "${tmpdir}/fastdl_files_to_compress.txt" ]; then
  193. echo -e "calculating total file size..."
  194. fn_sleep_time
  195. totalfiles=$(wc -l < "${tmpdir}/fastdl_files_to_compress.txt")
  196. # Calculates total file size.
  197. while read -r dufile; do
  198. filesize=$(stat -c %s "${dufile}")
  199. filesizetotal=$(( filesizetotal+filesize ))
  200. exitcode=$?
  201. if [ "${exitcode}" != 0 ]; then
  202. fn_print_fail_eol_nl
  203. fn_script_log_fatal "Calculating total file size."
  204. core_exit.sh
  205. fi
  206. done <"${tmpdir}/fastdl_files_to_compress.txt"
  207. else
  208. fn_print_fail_eol_nl "generating file list"
  209. fn_script_log_fatal "Generating file list."
  210. core_exit.sh
  211. fi
  212. echo -e "about to compress ${totalfiles} files, total size $(fn_human_readable_file_size ${filesizetotal} 0)"
  213. fn_script_log_info "${totalfiles} files, total size $(fn_human_readable_file_size ${filesizetotal} 0)"
  214. rm -f "${tmpdir:?}/fastdl_files_to_compress.txt"
  215. if ! fn_prompt_yn "Continue?" Y; then
  216. fn_script_log "User exited"
  217. core_exit.sh
  218. fi
  219. }
  220. # Builds Garry's Mod fastdl directory content.
  221. fn_fastdl_gmod(){
  222. cd "${systemdir}" || exit
  223. for allowed_extention in "${allowed_extentions_array[@]}"
  224. do
  225. fileswc=0
  226. tput sc
  227. while read -r fastdlfile; do
  228. ((fileswc++))
  229. tput rc; tput el
  230. echo -e "copying ${allowed_extention} : ${fileswc}..."
  231. cp --parents "${fastdlfile}" "${fastdldir}"
  232. exitcode=$?
  233. if [ ${exitcode} -ne 0 ]; then
  234. fn_print_fail_eol_nl
  235. fn_script_log_fatal "Copying ${fastdlfile} > ${fastdldir}"
  236. core_exit.sh
  237. else
  238. fn_script_log_pass "Copying ${fastdlfile} > ${fastdldir}"
  239. fi
  240. done < <(find . -type f -iname "${allowed_extention}")
  241. if [ ${fileswc} != 0 ]; then
  242. fn_print_ok_eol_nl
  243. fi
  244. done
  245. # Correct addons directory structure for FastDL.
  246. if [ -d "${fastdldir}/addons" ]; then
  247. echo -en "updating addons file structure..."
  248. cp -Rf "${fastdldir}"/addons/*/* "${fastdldir}"
  249. exitcode=$?
  250. if [ ${exitcode} -ne 0 ]; then
  251. fn_print_fail_eol_nl
  252. fn_script_log_fatal "Updating addons file structure"
  253. core_exit.sh
  254. else
  255. fn_print_ok_eol_nl
  256. fn_script_log_pass "Updating addons file structure"
  257. fi
  258. # Clear addons directory in fastdl.
  259. echo -en "clearing addons dir from fastdl dir..."
  260. fn_sleep_time
  261. rm -fR "${fastdldir:?}/addons"
  262. exitcode=$?
  263. if [ ${exitcode} -ne 0 ]; then
  264. fn_print_fail_eol_nl
  265. fn_script_log_fatal "Clearing addons dir from fastdl dir"
  266. core_exit.sh
  267. else
  268. fn_print_ok_eol_nl
  269. fn_script_log_pass "Clearing addons dir from fastdl dir"
  270. fi
  271. fi
  272. # Correct content that may be into a lua directory by mistake like some darkrpmodification addons.
  273. if [ -d "${fastdldir}/lua" ]; then
  274. echo -en "correcting DarkRP files..."
  275. fn_sleep_time
  276. cp -Rf "${fastdldir}/lua/"* "${fastdldir}"
  277. exitcode=$?
  278. if [ ${exitcode} -ne 0 ]; then
  279. fn_print_fail_eol_nl
  280. fn_script_log_fatal "Correcting DarkRP files"
  281. core_exit.sh
  282. else
  283. fn_print_ok_eol_nl
  284. fn_script_log_pass "Correcting DarkRP files"
  285. fi
  286. fi
  287. if [ -f "${tmpdir}/fastdl_files_to_compress.txt" ]; then
  288. totalfiles=$(wc -l < "${tmpdir}/fastdl_files_to_compress.txt")
  289. # Calculates total file size.
  290. while read -r dufile; do
  291. filesize=$(du -b "${dufile}" | awk '{ print $1 }')
  292. filesizetotal=$((filesizetotal + filesize))
  293. done <"${tmpdir}/fastdl_files_to_compress.txt"
  294. fi
  295. }
  296. fn_fastdl_source(){
  297. for directory in "${fastdl_directories_array[@]}"
  298. 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[@]}"
  312. do
  313. fileswc=0
  314. tput sc
  315. while read -r fastdlfile; do
  316. ((fileswc++))
  317. tput rc; tput el
  318. echo -e "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_fatal "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. echo -e "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. echo -en "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_fatal "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. echo -en "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_fatal "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. echo -en "\r\033[Kcompressing ${filetocompress}..."
  395. bzip2 -f "${filetocompress}"
  396. exitcode=$?
  397. if [ ${exitcode} -ne 0 ]; then
  398. fn_print_fail_eol_nl
  399. fn_script_log_fatal "Compressing ${filetocompress}"
  400. core_exit.sh
  401. else
  402. fn_script_log_pass "Compressing ${filetocompress}"
  403. fi
  404. done < <(find "${fastdldir:?}" \( -type f ! -name "*.bz2" \))
  405. fn_print_ok_eol_nl
  406. }
  407. # Run functions.
  408. fn_fastdl_preview
  409. fn_clear_old_fastdl
  410. fn_fastdl_dirs
  411. fn_fastdl_build
  412. fn_fastdl_bzip2
  413. # Finished message.
  414. echo -e "FastDL files are located in:"
  415. echo -e "${fastdldir}"
  416. echo -e "FastDL completed"
  417. fn_script_log_info "FastDL completed"
  418. core_exit.sh