web/scripts/sync-ebooks
2019-05-29 16:47:03 -05:00

147 lines
2.9 KiB
Bash
Executable file
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
set -e
set -o pipefail
usage(){
fmt <<EOF
DESCRIPTION
Syncs books from standardebooks.org GitHub org to specified folder.
USAGE
${0##*/} [-v,-vv,--verbosity=INTEGER] [-u,--update-only] DIRECTORY
With -v or --verbosity=1, display general progress updates.
With -vv or --verbosity=2, display general progress updates and verbose git output.
With --update-only, only sync existing repositories, do not download new repositories.
DIRECTORY should be where the repositories should go.
EXAMPLE
${0##*/} /standardebooks.org/ebooks
EOF
exit
}
die(){ printf "\033[0;7;31mError:\033[0m %s\n" "${1}" 1>&2; exit 1; }
if [ $# -eq 1 ]; then if [ "$1" = "--help" ] || [ "$1" = "-h" ]; then usage; fi fi
# End boilerplate
# Terminate on CTRL-C
trap ctrl_c INT
ctrl_c() {
exit
}
if [[ $# -lt 1 ]]; then
usage
fi
verbosity=0
updateOnly="false"
target=""
for i in "$@"
do
case $i in
-h|--help)
usage
;;
-v)
verbosity=1
shift
;;
-vv)
verbosity=2
shift
;;
--verbosity=*)
verbosity=${i//[-a-zA-Z0-9]*=/}
shift
;;
-u|--update-only)
updateOnly="true"
shift
;;
*)
target="${i}"
shift
esac
done
if ! [ -d "${target}" ]; then
die "${target} is not a directory."
fi
if ! cd "${target}"; then
die "Couldnt cd into ${target}"
fi
if [ "${verbosity}" -gt 0 ]; then
printf "Updating local repositories ... \n"
fi
for item in ./*; do
[ -e "${item}" ] || break
if [ "${verbosity}" -eq 1 ]; then
printf "Updating %s ... " "${item}"
git -C "${item}" fetch -q
printf "Done.\n"
elif [ "${verbosity}" -gt 1 ]; then
printf "Updating %s ... \n" "${item}"
git -C "${item}" fetch -v
fi
done
if [ "${updateOnly}" = "true" ]; then
exit
fi
if [ "${verbosity}" -gt 0 ]; then
printf "Cloning remote repositories ... \n"
printf "Fetching repository urls ..."
fi
page=1
urls=""
pageurls="placeholder"
while [ -n "${pageurls}" ]; do
pageurls=$(curl -s "https://api.github.com/orgs/standardebooks/repos?per_page=100&page=${page}" \
| awk 'BEGIN { FS="\""; RS="," }; { if ($2 == "clone_url") {print $4} }')
urls=$(printf "%s\n%s" "${urls}" "${pageurls}")
page=$((page + 1))
if [ "${verbosity}" -gt 0 ]; then
printf "."
fi
done
if [ "${verbosity}" -gt 0 ]; then
printf " Done.\n"
fi
urls=$(printf "%s\n" "${urls}" | grep -v -e "/tools.git\$" -e "/web.git\$" -e "/manual.git\$" | awk 'NF')
printf "%s\n" "${urls}" | while IFS= read -r repourl; do
[ -n "${repourl}" ] || continue
[ -d "${repourl##*/}" ] && continue
if [ "${verbosity}" -gt 1 ]; then
printf "Cloning %s ... \n" "${repourl}"
git clone -v --bare "${repourl}"
if ! [ -d "${repourl##*/}" ]; then
printf "Failed.\n"
fi
else
if [ "${verbosity}" -gt 0 ]; then
printf "Cloning %s ... " "${repourl}"
fi
git clone -q --bare "${repourl}"
if [ "${verbosity}" -gt 0 ]; then
if ! [ -d "${repourl##*/}" ]; then
printf "Failed.\n"
else
printf "Done.\n"
fi
fi
fi
done