Skip to content

Instantly share code, notes, and snippets.

@l3laze
Created August 27, 2017 21:34
Show Gist options
  • Save l3laze/1534d6caf0642515e3dd1f3c32295bf5 to your computer and use it in GitHub Desktop.
Save l3laze/1534d6caf0642515e3dd1f3c32295bf5 to your computer and use it in GitHub Desktop.
My first attempt at a Bash download/install/cache tool. Alpha at best. Didn't need it's own repo.
#!/bin/bash
#
# Include source file and line number in xtrace messages
# From http://wiki.bash-hackers.org/scripting/debuggingtips#making_xtrace_more_useful
#
# export PS4='+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
#
# Enable xtrace for debugging.
#
# set -x
USAGEMESSAGE="Usage: getdependency [ ... ]\n\
Where options are (* = required):\n\t\
[-c | --cachedir <path>]\t\tWhere to cache the dependency.\n\t\
[-d | --downloadto <path>]\t\tThe directory to git clone or download the dependency to.\n\t\
[-f | --filename <path>]*\t\tThe full local path of the dependency including filename.\n\t\
[-h | --help]\t\t\t\tDisplay this message.\n\t\
[-i | --installdir <path>]\t\tThe directory to unpack/move the dependency to.\n\t\
[-k | --keepdownload]\t\t\tKeep the downloaded file(s) instead of deleting them after caching.\n\t\
[-p | --packedtype <args>]\t\tThe packed type of the dependency, if any; case insensitive.\n\t\t\t\t\t\t\
Options are: TAR, GZ, ZIP, TAR.GZ, and TAR.ZIP.\n\t\
[-q | --quiet]\t\t\t\tShaddap.\n\t\
[-r | --renew]\t\t\t\tGet a new copy of the dependency; ignore the cache.\n\t\
[-s | --source <URL>|<GIT URI>]*\tA URL to download from using 'curl' or a git URI to 'git clone'.\n\t\
[-t | --type URL|GIT]*\t\t\tThe dependency type, either 'URL' or 'GIT', case insensitive."
#
# Default true/false settings..
#
DEPOVERWRITEMODE=false # Do not overwrite.
DEPQUIET=false # Do not run in verbose mode.
DEPKEEPDL=false # Do not keep downloaded files.
DEPRENEW=false # Do not renew cached dependencies.
#echo "Running \"getdependency $@\""
if [[ $# -eq 0 ]]; then
# display usage..
echo -e "\t\t**********\tError! -- You need to supply some args!\t**********"
echo -e "$USAGEMESSAGE"
else
#
# Parse arguments
#
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-c|--cachedir)
DEPCACHEDIR="$2"
shift
;;
-d|--downloadto)
DEPDOWNLOADTO="$2"
shift
;;
-f|--filename)
DEPFILENAME="$2"
shift
;;
-h|--help)
echo -e "$USAGEMESSAGE"
exit 0;
;;
-i|--installdir)
DEPINSTALLDIR="$2"
shift
;;
-k|--keepdownload)
DEPKEEPDL=true
;;
-p|--packedtype)
DEPPACKEDTYPE="$2"
shift
;;
-q|--quiet)
DEPQUIET=true
;;
-r|--renew)
DEPRENEW=true
;;
-s|--source)
DEPSOURCE="$2"
shift
;;
-t|--type)
DEPTYPE="$2"
if [[ "$DEPQUIET" == true ]]; then
echo "Before translation: $DEPTYPE"
fi
DEPTYPE=$(echo "$DEPTYPE" | tr '[:upper:]' '[:lower:]')
if [[ "$DEPQUIET" == true ]]; then
echo "After translation: $DEPTYPE"
fi
case $DEPTYPE in
[Gg][Ii][Tt])
;;
[Uu][Rr][Ll])
;;
*)
echo -e "\n\nError! Invalid dependency type: $2"
echo -e "Valid dependency types are 'GIT' and 'URL', case insensitive.\n\n"
exit 255;
esac
shift
;;
*)
clear
echo -e "\n\nError! Unknown option: $1\n\n"
echo -e "$USAGEMESSAGE"
exit 255;
;;
esac
shift # To get past the -o/-option.
done
#
# Validate args and existence of directories; mkdir -p where needed.
#
if [[ "$DEPCACHEDIR" == "" ]]; then
echo "If you don't set a cache directory the dependency will be cached in the current directory."
fi
if [[ ! -e "$DEPCACHEDIR" ]]; then
echo "Cache directory doesn't exist. Creating it..."
mkdir -p $DEPCACHEDIR
fi
if [[ "$DEPDOWNLOADTO" == "" ]]; then
echo "If you don't set a directory to download to the dependency will be downloaded to the current directory."
fi
if [[ ! -e "$DEPDOWNLOADTO" ]]; then
echo "Dep download dir doesn't exist. Creating it..."
mkdir $DEPDOWNLOADTO
fi
if [[ "${DEPFILENAME}" == "" ]]; then
echo "Error! You must set a dependency file name."
exit 255;
fi
if [[ "$DEPINSTALLDIR" == "" ]]; then
echo "If you don't set an install directory the dependency will be installed to the current directory."
fi
if [[ ! -e "$DEPINSTALLDIR" ]]; then
echo "Dep install dir doesn't exist. Creating it..."
mkdir -p $DEPINSTALLDIR
fi
if [[ "$DEPKEEPDL" == true ]]; then
echo -e "**********\tRunning in keepdownload mode. The dependency will not be deleted after caching.\t\t**********"
fi
if [[ "$DEPPACKEDTYPE" == "" ]]; then
echo "If you don't set a packed type it will be assumed that the dependency doesn't need unpacking."
fi
if [[ "$DEPQUIET" == true ]]; then
echo -e "**********\tRunning in quiet mode. This means any prompts will be skipped!\t\t\t\t**********"
fi
if [[ "$DEPRENEW" == true ]]; then
echo -e "**********\tRunning in renew mode. Dependency will be downloaded even if there is a cached copy.\t**********"
fi
if [[ "$DEPSOURCE" == "" ]]; then
echo "You must set a source to get the dependency ${DEPSOURCE}."
exit 255;
fi
if [[ "$DEPTYPE" == "" ]]; then
echo "Error! You must set a dependency type to get the dependency ${DEPFILENAME}."
exit 255;
fi
if [[ "${DEPQUIET}" == false ]]; then
STATUS="\n"
STATUS+="FILE NAME\t=\t${DEPFILENAME}\n"
STATUS+="PACKED TYPE\t=\t${DEPPACKEDTYPE}\n"
STATUS+="TYPE\t\t=\t${DEPTYPE}\n"
STATUS+="SOURCE\t\t=\t${DEPSOURCE}\n"
STATUS+="DOWNLOAD DIR\t=\t${DEPDOWNLOADTO}\n"
STATUS+="INSTALL DIR\t=\t${DEPINSTALLDIR}\n"
STATUS+="CACHE DIR\t=\t${DEPCACHEDIR}\n"
STATUS+="KEEP DOWNLOAD\t=\t${DEPKEEPDL}\n"
STATUS+="QUIET MODE\t=\t${DEPQUIET}\n"
STATUS+="RENEW MODE\t=\t${DEPRENEW}\n\n"
echo -e "$STATUS"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment