Skip to content

Instantly share code, notes, and snippets.

@nkoneko
Created February 20, 2014 18:34
Show Gist options
  • Save nkoneko/9120268 to your computer and use it in GitHub Desktop.
Save nkoneko/9120268 to your computer and use it in GitHub Desktop.
#!/bin/sh
usage() {
cat 1>&2 <<EOS
SYNOPSIS
${SCRIPT_NAME} TARGET VERSION
TARGET either of the followings:
gmp, mpfr, mpc, isl, cloog, lzip, gcc, binutils
EOS
};
exit_iferror() {
EXIT_CODE=$?
ERROR_MSG=$@
[ ${EXIT_CODE} -eq 0 ] ||
cat 1>&2 <<EOS
${ERROR_MSG}
Exit code=${EXIT_CODE}
EOS
[ ${EXIT_CODE} -eq 0 ] || exit ${EXIT_CODE}
};
use_inflater_for() {
case "${1}" in
"tar.gz" )
INFLATER="gzip -cdq"
PREREQUISITES="gzip "${PREREQUISITES}
;;
"tar.bz2" )
INFLATER="bzip2 -cdkq"
PREREQUISITES="bzip2 "${PREREQUISITES}
;;
"tar.lz" )
INFLATER="lzip -cdkq"
PREREQUISITES="lzip "${PREREQUISITES}
;;
* )
cat 1>&2 <<EOS
Error: No decompression algorithm available for ${1}
EOS
exit 1
;;
esac
};
download() {
URL=${1}
case "${DOWNLOADER}" in
"wget" )
wget -q ${URL} -O -
;;
"curl" )
curl -L ${URL}
;;
esac
}
# Constants
# Some of these are determined at runtime. They should not be touched.
SCRIPT_NAME=${0}
TOOLS_ROOT=$(readlink -f $(dirname ${0})/..)
# Arguments
TARGET_NAME=${1}
TARGET_VERSION=${2}
[ -z "${TARGET_NAME}" ] && usage && exit 1
[ -z "${TARGET_VERSION}" ] && usage && exit 1
# Variables
INFLATER="gzip -cdq"
DOWNLOADER="curl"
ARCHIVE_LOC_SCHEME="http"
ARCHIVE_LOC_HOST=
ARCHIVE_LOC_PATH=
ARCHIVE_SUFFIX=
PREREQUISITES="wget"
case "${TARGET_NAME}" in
"gmp" )
ARCHIVE_LOC_SCHEME="https"
ARCHIVE_LOC_HOST="gmplib.org"
ARCHIVE_LOC_PATH="/download/gmp/"
ARCHIVE_SUFFIX="tar.lz"
;;
"mpfr" )
ARCHIVE_LOC_SCHEME="http"
ARCHIVE_LOC_HOST="www.mpfr.org"
ARCHIVE_LOC_PATH="/mpfr-current/"
ARCHIVE_SUFFIX="tar.bz2"
;;
"mpc" )
ARCHIVE_LOC_SCHEME="ftp"
ARCHIVE_LOC_HOST="ftp.gnu.org"
ARCHIVE_LOC_PATH="/gnu/mpc/"
ARCHIVE_SUFFIX="tar.gz"
;;
"isl" )
ARCHIVE_LOC_SCHEME="ftp"
ARCHIVE_LOC_HOST="gcc.gnu.org"
ARCHIVE_LOC_PATH="/pub/gcc/infrastructure/"
ARCHIVE_SUFFIX="tar.bz2"
;;
"cloog" )
ARCHIVE_LOC_SCHEME="ftp"
ARCHIVE_LOC_HOST="gcc.gnu.org"
ARCHIVE_LOC_PATH="/pub/gcc/infrastructure/"
ARCHIVE_SUFFIX="tar.gz"
;;
"lzip" )
ARCHIVE_LOC_SCHEME="http"
ARCHIVE_LOC_HOST="download.savannah.gnu.org"
ARCHIVE_LOC_PATH="/releases/lzip/"
ARCHIVE_SUFFIX="tar.gz"
;;
"gcc" )
ARCHIVE_LOC_SCHEME="http"
ARCHIVE_LOC_HOST="ftp.tsukuba.wide.ad.jp"
ARCHIVE_LOC_PATH="/software/gcc/releases/gcc-${TARGET_VERSION}/"
ARCHIVE_SUFFIX="tar.gz"
;;
"curl" )
ARCHIVE_LOC_SCHEME="http"
ARCHIVE_LOC_HOST="curl.haxx.se"
ARCHIVE_LOC_PATH="/download/"
DOWNLOADER="wget"
ARCHIVE_SUFFIX="tar.gz"
;;
"binutils" )
ARCHIVE_LOC_SCHEME="http"
ARCHIVE_LOC_HOST="ftp.gnu.org"
ARCHIVE_LOC_PATH="/gnu/binutils/"
ARCHIVE_SUFFIX="tar.bz2"
;;
* )
cat 1>&2 <<EOS
[Error] ${TARGET_NAME} not available.
EOS
usage
exit 1
;;
esac
use_inflater_for ${ARCHIVE_SUFFIX}
# Checking if prerequisite tools are installed
for PREREQ in ${PREREQUISITES}
do
echo "Searching ${PREREQ}..." 1>&2
PREREQ_PATH=$(which ${PREREQ})
exit_iferror "[Error] ${PREREQ} not found."
echo "[Success] ${PREREQ} found at "${PREREQ_PATH}"\n" 1>&2
done
ARCHIVE_FILENAME="${TARGET_NAME}-${TARGET_VERSION}.${ARCHIVE_SUFFIX}"
ARCHIVE_URL="${ARCHIVE_LOC_SCHEME}://${ARCHIVE_LOC_HOST}${ARCHIVE_LOC_PATH}${ARCHIVE_FILENAME}"
download ${ARCHIVE_URL} | ${INFLATER}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment