command_fastdl.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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; 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" "*.vmt" "*.vtf" )
  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[@]}"; 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}" != 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}" != 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}" != 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}" != 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[@]}"; do
  298. if [ -d "${systemdir}/${directory}" ]; then
  299. if [ "${directory}" == "maps" ]; then
  300. local allowed_extentions_array=( "*.bsp" "*.ain" "*.nav" "*.jpg" "*.txt" )
  301. elif [ "${directory}" == "materials" ]; then
  302. local allowed_extentions_array=( "*.vtf" "*.vmt" "*.vbf" "*.png" "*.svg" )
  303. elif [ "${directory}" == "models" ]; then
  304. local allowed_extentions_array=( "*.vtx" "*.vvd" "*.mdl" "*.phy" "*.jpg" "*.png" )
  305. elif [ "${directory}" == "particles" ]; then
  306. local allowed_extentions_array=( "*.pcf" )
  307. elif [ "${directory}" == "sound" ]; then
  308. local allowed_extentions_array=( "*.wav" "*.mp3" "*.ogg" )
  309. fi
  310. for allowed_extention in "${allowed_extentions_array[@]}"; do
  311. fileswc=0
  312. tput sc
  313. while read -r fastdlfile; do
  314. ((fileswc++))
  315. tput rc; tput el
  316. echo -e "copying ${directory} ${allowed_extention} : ${fileswc}..."
  317. fn_sleep_time
  318. # get relative path of file in the dir
  319. tmprelfilepath="${fastdlfile#"${systemdir}/"}"
  320. copytodir="${tmprelfilepath%/*}"
  321. # create relative path for fastdl
  322. if [ ! -d "${fastdldir}/${copytodir}" ]; then
  323. mkdir -p "${fastdldir}/${copytodir}"
  324. fi
  325. cp "${fastdlfile}" "${fastdldir}/${copytodir}"
  326. exitcode=$?
  327. if [ "${exitcode}" != 0 ]; then
  328. fn_print_fail_eol_nl
  329. fn_script_log_fatal "Copying ${fastdlfile} > ${fastdldir}/${copytodir}"
  330. core_exit.sh
  331. else
  332. fn_script_log_pass "Copying ${fastdlfile} > ${fastdldir}/${copytodir}"
  333. fi
  334. done < <(find "${systemdir}/${directory}" -type f -iname "${allowed_extention}")
  335. if [ ${fileswc} != 0 ]; then
  336. fn_print_ok_eol_nl
  337. fi
  338. done
  339. fi
  340. done
  341. }
  342. # Builds the fastdl directory content.
  343. fn_fastdl_build(){
  344. # Copy all needed files for FastDL.
  345. echo -e "copying files to ${fastdldir}"
  346. fn_script_log_info "Copying files to ${fastdldir}"
  347. if [ "${shortname}" == "gmod" ]; then
  348. fn_fastdl_gmod
  349. fn_fastdl_gmod_dl_enforcer
  350. else
  351. fn_fastdl_source
  352. fi
  353. }
  354. # Generate lua file that will force download any file into the FastDL directory.
  355. fn_fastdl_gmod_dl_enforcer(){
  356. # Clear old lua file.
  357. if [ -f "${luafastdlfullpath}" ]; then
  358. echo -en "removing existing download enforcer: ${luafastdlfile}..."
  359. rm -f "${luafastdlfullpath:?}"
  360. exitcode=$?
  361. if [ "${exitcode}" != 0 ]; then
  362. fn_print_fail_eol_nl
  363. fn_script_log_fatal "Removing existing download enforcer ${luafastdlfullpath}"
  364. core_exit.sh
  365. else
  366. fn_print_ok_eol_nl
  367. fn_script_log_pass "Removing existing download enforcer ${luafastdlfullpath}"
  368. fi
  369. fi
  370. # Generate new one if user said yes.
  371. if [ "${luaresource}" == "on" ]; then
  372. echo -en "creating new download enforcer: ${luafastdlfile}..."
  373. touch "${luafastdlfullpath}"
  374. # Read all filenames and put them into a lua file at the right path.
  375. while read -r line; do
  376. echo -e "resource.AddFile( \"${line}\" )" >> "${luafastdlfullpath}"
  377. done < <(find "${fastdldir:?}" \( -type f ! -name "*.bz2" \) -printf '%P\n')
  378. exitcode=$?
  379. if [ "${exitcode}" != 0 ]; then
  380. fn_print_fail_eol_nl
  381. fn_script_log_fatal "Creating new download enforcer ${luafastdlfullpath}"
  382. core_exit.sh
  383. else
  384. fn_print_ok_eol_nl
  385. fn_script_log_pass "Creating new download enforcer ${luafastdlfullpath}"
  386. fi
  387. fi
  388. }
  389. # Compresses FastDL files using bzip2.
  390. fn_fastdl_bzip2(){
  391. while read -r filetocompress; do
  392. echo -en "\r\033[Kcompressing ${filetocompress}..."
  393. bzip2 -f "${filetocompress}"
  394. exitcode=$?
  395. if [ "${exitcode}" != 0 ]; then
  396. fn_print_fail_eol_nl
  397. fn_script_log_fatal "Compressing ${filetocompress}"
  398. core_exit.sh
  399. else
  400. fn_script_log_pass "Compressing ${filetocompress}"
  401. fi
  402. done < <(find "${fastdldir:?}" \( -type f ! -name "*.bz2" \))
  403. fn_print_ok_eol_nl
  404. }
  405. # Run functions.
  406. fn_fastdl_preview
  407. fn_clear_old_fastdl
  408. fn_fastdl_dirs
  409. fn_fastdl_build
  410. fn_fastdl_bzip2
  411. # Finished message.
  412. echo -e "FastDL files are located in:"
  413. echo -e "${fastdldir}"
  414. echo -e "FastDL completed"
  415. fn_script_log_info "FastDL completed"
  416. core_exit.sh