Browse Source

code tidy

pull/743/head
Daniel Gibbs 9 years ago
parent
commit
190fa02532
  1. 83
      functions/core_dl.sh

83
functions/core_dl.sh

@ -6,10 +6,17 @@ lgsm_version="050216"
# Description: Deals with all downloads for LGSM. # Description: Deals with all downloads for LGSM.
# Downloads can be defined in code like so # fileurl: The URL of the file: http://example.com/dl/File.tar.bz2
# fn_dl "dl_filename" "dl_filepath" "dl_url" "dl_md5" # filedir: location the file is to be saved: /home/server/lgsm/tmp
# fn_dl "file.tar.bz2" "/home/gameserver" "http://example.com/file.tar/bz2" "10cd7353aa9d758a075c600a6dd193fd" # filename: name of file (this can be different from the url name): file.tar.bz2
# executecmd: Optional, set to "executecmd" to make file executable using chmod +x
# run: Optional, set to run to execute the file
# force: Optional, force re-download of file even if exists
# md5: Optional, Checks file against an md5 sum
#
# Downloads can be defined in code like so:
# fn_fetch_file "${fileurl}" "${filedir}" "${filename}" "${executecmd}" "${run}" "${force}" "${md5}"
# fn_fetch_file "http://example.com/file.tar.bz2" "/some/dir" "file.tar.bz2" "executecmd" "run" "force" "10cd7353aa9d758a075c600a6dd193fd"
fn_dl_md5(){ fn_dl_md5(){
# Runs MD5 Check if available # Runs MD5 Check if available
@ -34,12 +41,17 @@ if [ -n "${md5}" ]; then
fi fi
} }
# Extracts bzip2 or gzip files
# Extracts can be defined in code like so:
# fn_dl_extract "${filedir}" "${filename}" "${extractdir}"
# fn_dl_extract "/home/gameserver/lgsm/tmp" "file.tar.bz2" "/home/gamserver/serverfiles"
fn_dl_extract(){ fn_dl_extract(){
filedir=${1} filedir=${1}
filename=${2} filename=${2}
extractdir=${3} extractdir=${3}
# extracts archives # extracts archives
echo -ne "extracting ${filename}..." echo -ne "extracting ${filename}..."
fn_scriptlog "extracting download"
mime=$(file -b --mime-type "${filedir}/${filename}") mime=$(file -b --mime-type "${filedir}/${filename}")
if [ "${mime}" == "application/gzip" ]; then if [ "${mime}" == "application/gzip" ]; then
@ -50,7 +62,8 @@ fi
local exitcode=$? local exitcode=$?
if [ ${exitcode} -ne 0 ]; then if [ ${exitcode} -ne 0 ]; then
fn_printfaileolnl fn_printfaileolnl
echo "${tarcmd}" fn_scriptlog "extracting download: FAIL"
echo "${tarcmd}" | tee -a "${scriptlog}"
exit ${exitcode} exit ${exitcode}
else else
fn_printokeolnl fn_printokeolnl
@ -60,14 +73,14 @@ fi
# Trap to remove file download if canceled before completed # Trap to remove file download if canceled before completed
fn_fetch_trap() { fn_fetch_trap() {
echo "" echo ""
fn_printinfomationnl "Cancelling download" fn_printinfomationnl "cancelling download"
fn_scriptlog "canceling download"
sleep 1 sleep 1
fn_printinfomation "Removing ${filename}" fn_printinfomation "removing ${filename}"
rm -f "${filedir}/${filename}" fn_scriptlog "removing ${filename}"
rm -f "${filedir}/${filename}" | tee -a "${scriptlog}"
} }
# Downloads file using curl and run it if required
# fn_fetch_file "fileurl" "filedir" "filename" "executecmd" "run" "force" "md5"
fn_fetch_file(){ fn_fetch_file(){
fileurl=${1} fileurl=${1}
filedir=${2} filedir=${2}
@ -98,27 +111,32 @@ if [ ! -f "${filedir}/${filename}" ]; then
# if larger file shows progress bar # if larger file shows progress bar
if [ ${filename##*.} == "bz2" ]; then if [ ${filename##*.} == "bz2" ]; then
echo -ne "downloading ${filename}..." echo -ne "downloading ${filename}..."
fn_scriptlog "downloading ${filename}"
sleep 1 sleep 1
curlcmd=$(${curlcmd} --progress-bar --fail -o "${filedir}/${filename}" "${fileurl}") curlcmd=$(${curlcmd} --progress-bar --fail -o "${filedir}/${filename}" "${fileurl}")
echo -ne "downloading ${filename}..." echo -ne "downloading ${filename}..."
else else
echo -ne " fetching ${filename}...\c" echo -ne " fetching ${filename}...\c"
fn_scriptlog "fetching ${filename}"
curlcmd=$(${curlcmd} -s --fail -o "${filedir}/${filename}" "${fileurl}" 2>&1) curlcmd=$(${curlcmd} -s --fail -o "${filedir}/${filename}" "${fileurl}" 2>&1)
fi fi
local exitcode=$? local exitcode=$?
if [ ${exitcode} -ne 0 ]; then if [ ${exitcode} -ne 0 ]; then
fn_printfaileolnl fn_printfaileolnl
echo "${curlcmd}" fn_scriptlog "downloading ${filename}: FAIL"
echo -e "${fileurl}\n" echo "${curlcmd}" | tee -a "${scriptlog}"
echo -e "${fileurl}\n" | tee -a "${scriptlog}"
exit ${exitcode} exit ${exitcode}
else else
fn_printokeolnl fn_printokeolnl
fn_scriptlog "downloading ${filename}: OK"
fi fi
# remove trap # remove trap
trap - INT trap - INT
else else
fn_printfaileolnl fn_printfaileolnl
echo "Curl is not installed!" echo "Curl is not installed!"
fn_scriptlog "Curl is not installed!"
echo -e "" echo -e ""
exit 1 exit 1
fi fi
@ -138,36 +156,43 @@ fi
} }
# fn_fetch_file_github
# Parameters: # fileurl: The directory the file is located in teh GitHub repo
# github_file_url_dir: The directory the file is located in teh GitHub repo # filedir: name of file
# github_file_url_name: name of file # filename: location file to be saved
# filepath: location file to be saved
# executecmd: set to "executecmd" to make file executecmd # executecmd: set to "executecmd" to make file executecmd
# run: Optional, set to run to execute the file # run: Optional, set to run to execute the file
# force: force download of file even if exists # force: force download of file even if exists
# md5: Checks fail against an md5 sum
# Fetches files from the github repo
fn_fetch_file_github(){ fn_fetch_file_github(){
github_file_url_dir=${1} github_file_url_dir="${1}"
github_file_url_name=${2} github_file_url_name="${2}"
filepath=${3}
filename="${github_file_url_name}"
executecmd=${4:-0}
run=${5:-0}
force=${6:-0}
githuburl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}" githuburl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
fn_fetch_file "${githuburl}" "${filepath}" "${filename}" "${executecmd}" "${run}" "${force}" fileurl="${githuburl}"
filedir="${3}"
filename="${github_file_url_name}"
executecmd="${4:-0}"
run="${5:-0}"
force="${6:-0}"
md5="${7:-0}"
fn_fetch_file "${fileurl}" "${filedir}" "${filename}" "${executecmd}" "${run}" "${force}" "${md5}"
} }
# Fetches functions # Fetches functions
fn_fetch_function(){ fn_fetch_function(){
github_file_url_dir="functions" # github dir containing the file github_file_url_dir="functions" # github dir containing the file
github_file_url_name="${functionfile}" # name of the github file github_file_url_name="${functionfile}" # name of the github file
githuburl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}" githuburl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
filedir="${functionsdir}" # local dir that will contain the file fileurl="${githuburl}"
filename="${github_file_url_name}" # name of the local file filedir="${functionsdir}"
filename="${github_file_url_name}"
executecmd="executecmd" executecmd="executecmd"
run="run" run="run"
fn_fetch_file "${githuburl}" "${filedir}" "${filename}" "${executecmd}" "${run}" force="noforce"
md5=""
fn_fetch_file "${fileurl}" "${filedir}" "${filename}" "${executecmd}" "${run}" "${force}" "${md5}"
} }
Loading…
Cancel
Save