diff --git a/README.md b/README.md index 5ff62823..53bfb9f7 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,8 @@ You’ll also need to ensure the following: The website pulls all ebook information from what is contained in `/standardebooks.org/www/ebooks/`. It does not inspect `/standardebooks.org/ebooks/`. Therefore it is possible for one or the other to hold different catalogs if they become out of sync. + To automatically populate your server with ebooks from https://github.com/standardebooks/, you can use sync-ebooks and deploy-ebook-to-www in the [scripts](scripts) directory. If you don't want to clone all ebooks, don't use sync-ebooks, and instead clone the books you want into `/standardebooks.org/ebooks` with `git clone --bare`. To clone a list of books, you can use `while IFS= read -r line; do git clone --bare "${line}"; done < urllist.txt` + # Testing This repository includes [PHPStan](https://github.com/phpstan/phpstan) to statically analyze the codebase and [Safe PHP](https://github.com/thecodingmachine/safe) to replace old functions that don't throw exceptions. diff --git a/scripts/README.md b/scripts/README.md new file mode 100644 index 00000000..c761be19 --- /dev/null +++ b/scripts/README.md @@ -0,0 +1,7 @@ +# sync-ebooks + +To use, call this script with the directory where your ebooks go as its last argument. For example `sync-ebooks /standardebooks.org/ebooks` or if you want to clone them into this repository `sync-ebooks ebooks`. If you want progress output, use -v, and if you want detailed git progress output use -vv. + +# deploy-ebook-to-www + +To use, call this script with the directories of the books you want to deploy as its arguments. For example, to deploy all ebooks after using sync-ebooks, run `deploy-ebook-to-www /standardebooks.org/ebooks/*`. To deploy only The Time Machine by H.G Wells, you would run `deploy-ebook-to-www /standardebooks.org/ebooks/h-g-wells_the-time-machine`. Note that deploy-ebook-to-www assumes that your webroot is in /standardebooks.org/. To output progress information, use -v or --verbose. diff --git a/scripts/sync-ebooks b/scripts/sync-ebooks new file mode 100755 index 00000000..b66fb0d0 --- /dev/null +++ b/scripts/sync-ebooks @@ -0,0 +1,122 @@ +#!/bin/bash +set -e +set -u +set -o pipefail + +usage(){ + fmt <&2 printf "%s does not exist.\n" "$*" + exit +fi + +if ! cd "$*"; then + >&2 printf "Couldn't cd into %s.\n" "$*" + exit +fi + +if [ "${verbosity}" -gt 0 ]; then + printf "** Updating local repositories. **\n" +fi + +for item in ./*; do + [ -e "${item}" ] || break + if [ "${verbosity}" -gt 0 ]; then + printf "Updating %s\n" "${item}" + fi + + if [ "${verbosity}" -gt 1 ]; then + git -C "${item}" fetch -v + else + git -C "${item}" fetch -q + fi +done + +if [ "${update_only}" = "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 "\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 0 ]; then + printf "Cloning %s\n" "${repourl}" + fi + + if [ "${verbosity}" -gt 1 ]; then + git clone -v --bare "${repourl}" + else + git clone -q --bare "${repourl}" + fi + + if ! [ -d "${repourl##*/}" ]; then + >&2 printf "%s wasn't cloned." "${repourl}" + fi +done