Last active
April 4, 2016 13:40
-
-
Save MilhouseVH/c5208eddaa8f7e66331502d5789ab2ff to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
################################################################################ | |
# This file is part of LibreELEC - http://www.libreelec.tv | |
# Copyright (C) 2009-2016 Team LibreELEC | |
# | |
# LibreELEC is free software: you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation, either version 2 of the License, or | |
# (at your option) any later version. | |
# | |
# LibreELEC is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# | |
# You should have received a copy of the GNU General Public License | |
# along with LibreELEC. If not, see <http://www.gnu.org/licenses/>. | |
################################################################################ | |
set -e | |
LIBREELEC_DIR=$HOME/LibreELEC.tv | |
TARGET_DIR=/var/www/sources | |
DOWNLOAD_DIR=$HOME/download | |
REVISION= | |
PACKAGE= | |
DRYRUN=N | |
IGNORE_ERRORS=N | |
DOCLONE=N | |
PROGRESS=Y | |
ISMIRROR=N | |
VERBOSE=N | |
[ -z "${PROJECT}" ] && PROJECT=Generic | |
[ -z "${ARCH}" ] && ARCH=x86_64 | |
if command -v tput >/dev/null; then | |
TXRED="$(tput setaf 1 bold)" | |
TXGREEN="$(tput setaf 2 bold)" | |
TXYELLOW="$(tput setaf 3 bold)" | |
TXBLUE="$(tput setaf 4 bold)" | |
TXMAGENTA="$(tput setaf 5 bold)" | |
TXCYAN="$(tput setaf 6 bold)" | |
TXRESET="$(tput sgr0)" | |
fi | |
help() { | |
[ -n "$1" ] && echo -e "ERROR: Unknown argument [$1]\n" | |
cat <<EOF | |
Usage: $(basename $0) -d|--download <path> [-l|--libreelec <path>] [-t|--target <path>] [-a|-all] | |
[-p|--package <package_name> [-r|--revision <revision>]] [-m|--mirror] | |
[--dry-run] [--clone] [-v|--verbose] [--noprogress] [-h|--help] | |
Options: | |
-d, --download: Directory path into which new package files will be downloaded - default is $HOME/download[1] | |
-l, --libreelec: LibreELEC.tv repo, default is ${HOME}/LibreELEC.tv | |
-t, --target: Directory path for existing packages that are to be refreshed, default is /var/www/sources[2] | |
-a, --all: Ignore download failures, continue downloading all packages | |
-p, --package: Package to process, otherwise process all packages | |
-r, --revision: Version to use in place of PKG_VERSION, only applicable in conjunction with -p | |
-m, --mirror: Target is mirror not source - mirror uses a hierarchical folder structure | |
--dry-run: Don't actually download anything | |
--clone: Clone the LibreELEC.tv repository | |
-v, --verbose: Output more verbose sync information | |
--noprogress: Do not show progress | |
-h, --help: This message | |
Note#1. The download directory will have the LibreELEC version appended (eg. /devel) unless it is a mirror, in which case "/mirror" will be appended. | |
Note#2. The target directory will have the LibreELEC version appended (eg. /devel) unless it is a mirror, in which case "/mirror" will be appended. | |
EOF | |
} | |
get_libreelec_version() { | |
cd $LIBREELEC_DIR | |
. config/options | |
echo $LIBREELEC_VERSION | |
} | |
get_distro_source() { | |
local ismirror="$1" | |
cd $LIBREELEC_DIR | |
. config/options | |
echo ${DISTRO_SRC} | |
} | |
get_distro_mirror() { | |
local ismirror="$1" | |
cd $LIBREELEC_DIR | |
. config/options | |
echo ${DISTRO_MIRROR} | |
} | |
# 1: package.mk | |
# 2: package variable, eg. PKG_URL | |
get_pkg_option() { | |
local package="$1" variable="$2" | |
cd $LIBREELEC_DIR | |
. config/options ${package} 2>/dev/null | |
echo "${!variable}" | |
} | |
# Return success if the specified package is not found locally | |
need_package() { | |
local package_name="$1" | |
local package_source="$2" | |
if [ ${ISMIRROR} == N ]; then | |
[ -f ${TARGET_DIR}/${package_source} ] && return 1 | |
[ -f ${DOWNLOAD_DIR}/${package_source} ] && return 1 | |
else | |
[ -f ${TARGET_DIR}/${package_name}/${package_source} ] && return 1 | |
[ -f ${DOWNLOAD_DIR}/${package_name}/${package_source} ] && return 1 | |
fi | |
return 0 | |
} | |
remote_file_exists() { | |
[ "$(curl -sfIL --connect-timeout 15 --retry 3 -w "%{http_code}" -o /dev/null ${1} 2>/dev/null)" == "404" ] && return 1 || return 0 | |
} | |
download_file() { | |
local url="$1" | |
local retries=10 | |
local attempts=0 | |
while [ ${attempts} -lt ${retries} ]; do | |
attempts=$((attempts + 1)) | |
wget --timeout=30 --tries=3 --passive-ftp --no-check-certificate -O ${DOWNLOAD_DIR}/.tmp ${url} 2>/tmp/sync.log && return 0 | |
done | |
return 1 | |
} | |
# Download the specified package | |
getkpkg() { | |
local package_name="$1" | |
local package_source="$2" | |
local package_url="$3" | |
local result msg | |
local onsource=N onmirror=N | |
[ -n "${package_url}" ] || return 0 | |
remote_file_exists ${package_url} && onsource=Y | |
if [ ${onsource} == N ]; then | |
remote_file_exists ${DISTRO_MIRROR}/${package_name}/${package_source} && onmirror=Y | |
fi | |
# Only source is the distro server | |
if [[ ${package_url} =~ ^${DISTRO_SOURCE}.* ]]; then | |
# Check if the file exists on the distro server | |
if [ ${onsource} == N ]; then | |
# And the mirror | |
if [ ${onmirror} == N ]; then | |
# If not found show a warning, otherwise continue with download | |
echo "${LINE_PREFIX}${TXMAGENTA}PACKAGE UPDATE OR MKPKG REQUIRED${TXRESET}: Unable to download ${package_name} from url ${package_url}" >&2 | |
return 0 | |
fi | |
fi | |
fi | |
if [ ${DRYRUN} == Y ]; then | |
echo -n "${LINE_PREFIX}Downloading package: ${package_name} (${package_source})..." | |
if [ ${onsource} == N ]; then | |
[ ${ISMIRROR} == Y -a ${onmirror} == Y ] && msg=" ${TXMAGENTA}(found on mirror)${TXRESET}" | |
fi | |
[ ${onsource} == Y -o ${onmirror} == Y ] && echo " ${TXGREEN}OK${TXRESET}${msg}" || echo " ${TXRED}FAILED!!${TXRESET} ${package_url}" | |
return 0 | |
fi | |
rm -f ${DOWNLOAD_DIR}/.tmp /tmp/sync.log | |
echo -n "${LINE_PREFIX}Downloading package: ${package_name} (${package_source})..." | |
if [ ${onsource} == Y ]; then | |
download_file ${package_url} && result=0 || result=1 | |
elif [ -a ${onmirror} == Y ]; then | |
download_file ${DISTRO_MIRROR}/${package_name}/${package_source} && result=0 || result=1 | |
else | |
result=1 | |
fi | |
if [ ${result} -ne 0 -o ! -s ${DOWNLOAD_DIR}/.tmp ]; then | |
echo " ${TXRED}FAILED!!${TXRESET}" | |
[ -f /tmp/sync.log ] && cat /tmp/sync.log >&2 | |
rm -f ${DOWNLOAD_DIR}/.tmp | |
else | |
echo " ${TXGREEN}OK${TXRESET}" | |
if [ ${ISMIRROR} == Y ]; then | |
mkdir -p ${DOWNLOAD_DIR}/${package_name} | |
mv ${DOWNLOAD_DIR}/.tmp ${DOWNLOAD_DIR}/${package_name}/${package_source} | |
else | |
mv ${DOWNLOAD_DIR}/.tmp ${DOWNLOAD_DIR}/${package_source} | |
fi | |
fi | |
[ ${IGNORE_ERRORS} == Y ] && return 0 || return ${result} | |
} | |
check_single_package() { | |
local package_name="$1" revision="$2" | |
local package_dir="$(cd ${LIBREELEC_DIR}; find packages -type d -name ${PACKAGE})" | |
if [ -n "${package_dir}" ]; then | |
if [ -f ${LIBREELEC_DIR}/${package_dir}/package.mk ]; then | |
check_package "${package_dir}/package.mk" "${revision}" | |
return 0 | |
else | |
echo "ERROR: ${package_name} is not a valid package - package.mk does not exist" | |
fi | |
else | |
echo "ERROR: ${package_name} is not a valid package - directory does not exist" | |
fi | |
return 1 | |
} | |
# Download a package if required | |
check_package() { | |
local package_file="$1" revision="$2" | |
local package_name="$(basename $(dirname ${package_file}))" | |
local package_source package_url package_ver | |
package_source="$(get_pkg_option "${package_name}" PKG_SOURCE_NAME)" | |
if [ -n "${revision}" ]; then | |
package_ver=$(get_pkg_option "${package_name}" PKG_VERSION) | |
package_source="${package_source/${package_ver}/${revision}}" | |
fi | |
[ -n "${package_source}" ] || return 0 | |
if need_package "${package_name}" "${package_source}"; then | |
package_url="$(get_pkg_option "${package_name}" PKG_URL)" | |
if [ -n "${revision}" ]; then | |
package_url="${package_url/${package_ver}/${revision}}" | |
fi | |
getkpkg "${package_name}" "${package_source}" "${package_url}" | |
else | |
[ ${VERBOSE} == Y ] && echo "${LINE_PREFIX}Already downloaded : ${package_name} (${package_source}) ${TXGREEN}OK${TXRESET}" || true | |
fi | |
} | |
check_exists() { | |
if [ -z "${!1}" ]; then | |
echo "ERROR: ${1} must not be undefined" | |
return 1 | |
fi | |
[ -d ${!1} ] && return 0 | |
echo "ERROR: ${1} ${!1} does not exist" | |
return 1 | |
} | |
get_abs_path() { | |
echo "$(readlink -f $1)" | |
} | |
get_package_path() { | |
echo "$(basename "$(dirname "$0")") $0" | |
} | |
get_packages() { | |
export -f get_package_path | |
cd $LIBREELEC_DIR | |
find packages -name package.mk -exec bash -c get_package_path "{}" \; | sort -k1 | cut -d' ' -f2- | |
} | |
COUNT=0 | |
progress() { | |
COUNT=$((COUNT + 1)) | |
printf "Working... %3d%%\r" $((COUNT * 100 / $1)) | |
} | |
while [ : ]; do | |
[ -z "$1" ] && break | |
case $1 in | |
-d|--download) | |
shift | |
DOWNLOAD_DIR=$1 | |
;; | |
-l|--libreelec) | |
shift | |
LIBREELEC_DIR=$1 | |
;; | |
-t|--target) | |
shift | |
TARGET_DIR=$1 | |
;; | |
-a|--all) | |
IGNORE_ERRORS=Y | |
;; | |
-p|--package) | |
shift | |
PACKAGE=$1 | |
VERBOSE=Y | |
;; | |
-r|--revision) | |
shift | |
REVISION=$1 | |
;; | |
-m|--mirror) | |
ISMIRROR=Y | |
;; | |
--dry-run|--dryrun) | |
DRYRUN=Y | |
LINE_PREFIX="**DRY-RUN** " | |
;; | |
--clone) | |
DOCLONE=Y | |
;; | |
-v|--verbose) | |
VERBOSE=Y | |
;; | |
--noprogress) | |
PROGRESS=N | |
;; | |
-h|--help) | |
help | |
exit 0 | |
;; | |
*) | |
help "$1" | |
exit 0 | |
;; | |
esac | |
shift | |
done | |
if [ ${DOCLONE} == Y ]; then | |
( | |
if [ -d ${LIBREELEC_DIR}/.git ]; then | |
cd ${LIBREELEC_DIR} | |
git pull | |
else | |
mkdir -p $(dirname "${LIBREELEC_DIR}") 2>/dev/null | |
cd $(dirname "${LIBREELEC_DIR}") | |
git clone https://github.com/LibreELEC/LibreELEC.tv.git $(basename "${LIBREELEC_DIR}") | |
fi | |
) | |
echo | |
fi | |
check_exists LIBREELEC_DIR || exit | |
LIBREELEC_DIR="$(get_abs_path "${LIBREELEC_DIR}")" | |
if [ ${ISMIRROR} == N ]; then | |
TARGET_DIR="$(get_abs_path "${TARGET_DIR}/$(get_libreelec_version)")" | |
else | |
TARGET_DIR="$(get_abs_path "${TARGET_DIR}/mirror")" | |
fi | |
check_exists TARGET_DIR || exit | |
if [ ${ISMIRROR} == N ]; then | |
DOWNLOAD_DIR="$(get_abs_path "${DOWNLOAD_DIR}/$(get_libreelec_version)")" | |
else | |
DOWNLOAD_DIR="$(get_abs_path "${DOWNLOAD_DIR}/mirror")" | |
fi | |
check_exists DOWNLOAD_DIR || exit | |
if [ -n "${REVISION}" -a -z "${PACKAGE}" ]; then | |
echo "ERROR: A single package must be specified with custom revision" | |
exit 1 | |
fi | |
DISTRO_SOURCE="$(get_distro_source)" | |
DISTRO_MIRROR="$(get_distro_mirror)" | |
echo "Distro Source is: ${DISTRO_SOURCE}" | |
if [ ${ISMIRROR} == Y ]; then | |
echo "Distro Mirror is: ${DISTRO_MIRROR}" | |
fi | |
echo " Syncing against: ${TARGET_DIR}" | |
echo " Downloading to: ${DOWNLOAD_DIR}" | |
echo | |
if [ -n "${PACKAGE}" ]; then | |
check_single_package "${PACKAGE}" "${REVISION}" | |
else | |
packages=$(get_packages) | |
pcount="$(echo "${packages}" | wc -l)" | |
for package_mk in ${packages}; do | |
[ ${PROGRESS} == Y ] && progress ${pcount} | |
check_package ${package_mk} | |
done | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment