rework DOWNLOAD_METHOD, download_file, open_file, readIniFile

This commit is contained in:
TotallyLeGIT 2020-08-23 23:52:19 +02:00
parent cfce521919
commit 0b88b94937

View file

@ -42,9 +42,9 @@ ESR=false
# Download method priority: curl -> wget # Download method priority: curl -> wget
DOWNLOAD_METHOD='' DOWNLOAD_METHOD=''
if [[ $(command -v 'curl') ]]; then if [[ $(command -v 'curl') ]]; then
DOWNLOAD_METHOD='curl' DOWNLOAD_METHOD='curl --max-redirs 3 -so'
elif [[ $(command -v 'wget') ]]; then elif [[ $(command -v 'wget') ]]; then
DOWNLOAD_METHOD='wget' DOWNLOAD_METHOD='wget --max-redirect 3 --quiet -O'
else else
echo -e "${RED}This script requires curl or wget.\nProcess aborted${NC}" echo -e "${RED}This script requires curl or wget.\nProcess aborted${NC}"
exit 0 exit 0
@ -104,24 +104,16 @@ Optional Arguments:
######################### #########################
# Download files # Download files
download_file () { download_file () { # expects URL as argument ($1)
declare -r url=$1
declare -r tf=$(mktemp) declare -r tf=$(mktemp)
local dlcmd=''
if [ $DOWNLOAD_METHOD = 'curl' ]; then $DOWNLOAD_METHOD "${tf}" "$1" && echo "$tf" || exit 1 # return the temp-filename (or empty string on error)
dlcmd="curl -o $tf"
else
dlcmd="wget -O $tf"
fi
$dlcmd "${url}" &>/dev/null && echo "$tf" || echo '' # return the temp-filename (or empty string on error)
} }
open_file () { #expects one argument: file_path open_file () { #expects one argument: file_path
if [ "$(uname)" == 'Darwin' ]; then if [ "$(uname)" == 'Darwin' ]; then
open "$1" open "$1"
elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then elif [ "$(uname -s | cut -c -5)" == "Linux" ]; then
xdg-open "$1" xdg-open "$1"
else else
echo -e "${RED}Error: Sorry, opening files is not supported for your OS.${NC}" echo -e "${RED}Error: Sorry, opening files is not supported for your OS.${NC}"
@ -130,36 +122,28 @@ open_file () { #expects one argument: file_path
readIniFile () { # expects one argument: absolute path of profiles.ini readIniFile () { # expects one argument: absolute path of profiles.ini
declare -r inifile="$1" declare -r inifile="$1"
declare -r tfile=$(mktemp)
if [ $(grep '^\[Profile' "$inifile" | wc -l) == "1" ]; then ### only 1 profile found # tempIni will contain: [ProfileX], Name=, IsRelative= and Path= of the only (if) or the selected (else) profile
grep '^\[Profile' -A 4 "$inifile" | grep -v '^\[Profile' > $tfile if [ $(grep -c '^\[Profile' "${inifile}") == "1" ]; then ### only 1 profile found
tempIni="$(grep '^\[Profile' -A 4 "${inifile}")"
else else
grep -E -v '^\[General\]|^StartWithLastProfile=|^IsRelative=' "$inifile" echo 'Profiles found:'
echo '' echo ''
grep --color=never -E 'Default=[^1]|\[Profile[0-9]*\]|Name=|Path=|^$' "${inifile}"
echo
read -p 'Select the profile number ( 0 for Profile0, 1 for Profile1, etc ) : ' -r read -p 'Select the profile number ( 0 for Profile0, 1 for Profile1, etc ) : ' -r
echo -e "\n" echo -e "\n"
if [[ $REPLY =~ ^(0|[1-9][0-9]*)$ ]]; then # if Profile$REPLY does not exist, exit with error message
grep '^\[Profile'${REPLY} -A 4 "$inifile" | grep -v '^\[Profile'${REPLY} > $tfile tempIni="$(grep "^\[Profile${REPLY}" -A 4 "${inifile}")" || { echo -e "${RED}Profile${REPLY} does not exist!${NC}" && exit 1 ; }
if [[ "$?" != "0" ]]; then
echo "Profile${REPLY} does not exist!" && exit 1
fi
else
echo "Invalid selection!" && exit 1
fi
fi fi
declare -r profpath=$(grep '^Path=' $tfile) # extracting 0 or 1 from the "IsRelative=" line
declare -r pathisrel=$(grep '^IsRelative=' $tfile) declare -r pathisrel=$(echo "${tempIni}" | sed -n 's/^IsRelative=\([01]\)$/\1/p')
rm "$tfile" # extracting only the path itself, excluding "Path="
PROFILE_PATH=$(echo "${tempIni}" | sed -n 's/^Path=\(.*\)$/\1/p')
# update global variable # update global variable if path is relative
if [[ ${pathisrel#*=} == "1" ]]; then [[ ${pathisrel} == "1" ]] && PROFILE_PATH="$(dirname "${inifile}")/${profpath}"
PROFILE_PATH="$(dirname "$inifile")/${profpath#*=}"
else
PROFILE_PATH="${profpath#*=}"
fi
} }
getProfilePath () { getProfilePath () {