command_fastdl.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. 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; tput el
  148. echo -e "gathering ${allowed_extention} : ${fileswc}..."
  149. echo -e "${ext}" >> "${tmpdir}/fastdl_files_to_compress.txt"
  150. done < <(find . -type f -iname "${allowed_extention}")
  151. if [ ${fileswc} != 0 ]; then
  152. fn_print_ok_eol_nl
  153. else
  154. fn_print_info_eol_nl
  155. fi
  156. done
  157. # Source engine
  158. else
  159. fastdl_directories_array=( "maps" "materials" "models" "particles" "sound" "resources" )
  160. for directory in "${fastdl_directories_array[@]}"; do
  161. if [ -d "${systemdir}/${directory}" ]; then
  162. if [ "${directory}" == "maps" ]; then
  163. local allowed_extentions_array=( "*.bsp" "*.ain" "*.nav" "*.jpg" "*.txt" )
  164. elif [ "${directory}" == "materials" ]; then
  165. local allowed_extentions_array=( "*.vtf" "*.vmt" "*.vbf" "*.png" "*.svg" )
  166. elif [ "${directory}" == "models" ]; then
  167. local allowed_extentions_array=( "*.vtx" "*.vvd" "*.mdl" "*.phy" "*.jpg" "*.png" )
  168. elif [ "${directory}" == "particles" ]; then
  169. local allowed_extentions_array=( "*.pcf" )
  170. elif [ "${directory}" == "sound" ]; then
  171. local allowed_extentions_array=( "*.wav" "*.mp3" "*.ogg" )
  172. fi
  173. for allowed_extention in "${allowed_extentions_array[@]}"; do
  174. fileswc=0
  175. tput sc
  176. while read -r ext; do
  177. ((fileswc++))
  178. tput rc; tput el
  179. echo -e "gathering ${directory} ${allowed_extention} : ${fileswc}..."
  180. echo -e "${ext}" >> "${tmpdir}/fastdl_files_to_compress.txt"
  181. done < <(find "${systemdir}/${directory}" -type f -iname "${allowed_extention}")
  182. tput rc; tput el
  183. echo -e "gathering ${directory} ${allowed_extention} : ${fileswc}..."
  184. if [ ${fileswc} != 0 ]; then
  185. fn_print_ok_eol_nl
  186. else
  187. fn_print_info_eol_nl
  188. fi
  189. done
  190. fi
  191. done
  192. fi
  193. if [ -f "${tmpdir}/fastdl_files_to_compress.txt" ]; then
  194. echo -e "calculating total file size..."
  195. fn_sleep_time
  196. totalfiles=$(wc -l < "${tmpdir}/fastdl_files_to_compress.txt")
  197. # Calculates total file size.
  198. while read -r dufile; do
  199. filesize=$(stat -c %s "${dufile}")
  200. filesizetotal=$(( filesizetotal+filesize ))
  201. exitcode=$?
  202. if [ "${exitcode}" != 0 ]; then
  203. fn_print_fail_eol_nl
  204. fn_script_log_fatal "Calculating total file size."
  205. core_exit.sh
  206. fi
  207. done <"${tmpdir}/fastdl_files_to_compress.txt"
  208. else
  209. fn_print_fail_eol_nl "generating file list"
  210. fn_script_log_fatal "Generating file list."
  211. core_exit.sh
  212. fi
  213. echo -e "about to compress ${totalfiles} files, total size $(fn_human_readable_file_size ${filesizetotal} 0)"
  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. fn_script_log "User exited"
  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[@]}"
  225. do
  226. fileswc=0
  227. tput sc
  228. while read -r fastdlfile; do
  229. ((fileswc++))
  230. tput rc; tput el
  231. echo -e "copying ${allowed_extention} : ${fileswc}..."
  232. cp --parents "${fastdlfile}" "${fastdldir}"
  233. exitcode=$?
  234. if [ "${exitcode}" != 0 ]; then
  235. fn_print_fail_eol_nl
  236. fn_script_log_fatal "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. echo -en "updating addons file structure..."
  249. cp -Rf "${fastdldir}"/addons/*/* "${fastdldir}"
  250. exitcode=$?
  251. if [ "${exitcode}" != 0 ]; then
  252. fn_print_fail_eol_nl
  253. fn_script_log_fatal "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. echo -en "clearing addons dir from fastdl dir..."
  261. fn_sleep_time
  262. rm -fR "${fastdldir:?}/addons"
  263. exitcode=$?
  264. if [ "${exitcode}" != 0 ]; then
  265. fn_print_fail_eol_nl
  266. fn_script_log_fatal "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. echo -en "correcting DarkRP files..."
  276. fn_sleep_time
  277. cp -Rf "${fastdldir}/lua/"* "${fastdldir}"
  278. exitcode=$?
  279. if [ "${exitcode}" != 0 ]; then
  280. fn_print_fail_eol_nl
  281. fn_script_log_fatal "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[@]}"
  299. do
  300. if [ -d "${systemdir}/${directory}" ]; then
  301. if [ "${directory}" == "maps" ]; then
  302. local allowed_extentions_array=( "*.bsp" "*.ain" "*.nav" "*.jpg" "*.txt" )
  303. elif [ "${directory}" == "materials" ]; then
  304. local allowed_extentions_array=( "*.vtf" "*.vmt" "*.vbf" "*.png" "*.svg" )
  305. elif [ "${directory}" == "models" ]; then
  306. local allowed_extentions_array=( "*.vtx" "*.vvd" "*.mdl" "*.phy" "*.jpg" "*.png" )
  307. elif [ "${directory}" == "particles" ]; then
  308. local allowed_extentions_array=( "*.pcf" )
  309. elif [ "${directory}" == "sound" ]; then
  310. local allowed_extentions_array=( "*.wav" "*.mp3" "*.ogg" )
  311. fi
  312. for allowed_extention in "${allowed_extentions_array[@]}"
  313. do
  314. fileswc=0
  315. tput sc
  316. while read -r fastdlfile; do
  317. ((fileswc++))
  318. tput rc; tput el
  319. echo -e "copying ${directory} ${allowed_extention} : ${fileswc}..."
  320. fn_sleep_time
  321. # get relative path of file in the dir
  322. tmprelfilepath="${fastdlfile#"${systemdir}/"}"
  323. copytodir="${tmprelfilepath%/*}"
  324. # create relative path for fastdl
  325. if [ ! -d "${fastdldir}/${copytodir}" ]; then
  326. mkdir -p "${fastdldir}/${copytodir}"
  327. fi
  328. cp "${fastdlfile}" "${fastdldir}/${copytodir}"
  329. exitcode=$?
  330. if [ "${exitcode}" != 0 ]; then
  331. fn_print_fail_eol_nl
  332. fn_script_log_fatal "Copying ${fastdlfile} > ${fastdldir}/${copytodir}"
  333. core_exit.sh
  334. else
  335. fn_script_log_pass "Copying ${fastdlfile} > ${fastdldir}/${copytodir}"
  336. fi
  337. done < <(find "${systemdir}/${directory}" -type f -iname "${allowed_extention}")
  338. if [ ${fileswc} != 0 ]; then
  339. fn_print_ok_eol_nl
  340. fi
  341. done
  342. fi
  343. done
  344. }
  345. # Builds the fastdl directory content.
  346. fn_fastdl_build(){
  347. # Copy all needed files for FastDL.
  348. echo -e "copying files to ${fastdldir}"
  349. fn_script_log_info "Copying files to ${fastdldir}"
  350. if [ "${shortname}" == "gmod" ]; then
  351. fn_fastdl_gmod
  352. fn_fastdl_gmod_dl_enforcer
  353. else
  354. fn_fastdl_source
  355. fi
  356. }
  357. # Generate lua file that will force download any file into the FastDL directory.
  358. fn_fastdl_gmod_dl_enforcer(){
  359. # Clear old lua file.
  360. if [ -f "${luafastdlfullpath}" ]; then
  361. echo -en "removing existing download enforcer: ${luafastdlfile}..."
  362. rm -f "${luafastdlfullpath:?}"
  363. exitcode=$?
  364. if [ "${exitcode}" != 0 ]; then
  365. fn_print_fail_eol_nl
  366. fn_script_log_fatal "Removing existing download enforcer ${luafastdlfullpath}"
  367. core_exit.sh
  368. else
  369. fn_print_ok_eol_nl
  370. fn_script_log_pass "Removing existing download enforcer ${luafastdlfullpath}"
  371. fi
  372. fi
  373. # Generate new one if user said yes.
  374. if [ "${luaresource}" == "on" ]; then
  375. echo -en "creating new download enforcer: ${luafastdlfile}..."
  376. touch "${luafastdlfullpath}"
  377. # Read all filenames and put them into a lua file at the right path.
  378. while read -r line; do
  379. echo -e "resource.AddFile( \"${line}\" )" >> "${luafastdlfullpath}"
  380. done < <(find "${fastdldir:?}" \( -type f ! -name "*.bz2" \) -printf '%P\n')
  381. exitcode=$?
  382. if [ "${exitcode}" != 0 ]; then
  383. fn_print_fail_eol_nl
  384. fn_script_log_fatal "Creating new download enforcer ${luafastdlfullpath}"
  385. core_exit.sh
  386. else
  387. fn_print_ok_eol_nl
  388. fn_script_log_pass "Creating new download enforcer ${luafastdlfullpath}"
  389. fi
  390. fi
  391. }
  392. # Compresses FastDL files using bzip2.
  393. fn_fastdl_bzip2(){
  394. while read -r filetocompress; do
  395. echo -en "\r\033[Kcompressing ${filetocompress}..."
  396. bzip2 -f "${filetocompress}"
  397. exitcode=$?
  398. if [ "${exitcode}" != 0 ]; then
  399. fn_print_fail_eol_nl
  400. fn_script_log_fatal "Compressing ${filetocompress}"
  401. core_exit.sh
  402. else
  403. fn_script_log_pass "Compressing ${filetocompress}"
  404. fi
  405. done < <(find "${fastdldir:?}" \( -type f ! -name "*.bz2" \))
  406. fn_print_ok_eol_nl
  407. }
  408. # Run functions.
  409. fn_fastdl_preview
  410. fn_clear_old_fastdl
  411. fn_fastdl_dirs
  412. fn_fastdl_build
  413. fn_fastdl_bzip2
  414. # Finished message.
  415. echo -e "FastDL files are located in:"
  416. echo -e "${fastdldir}"
  417. echo -e "FastDL completed"
  418. fn_script_log_info "FastDL completed"
  419. core_exit.sh