#!/usr/bin/env bash set -euo pipefail msg() { printf "\033[32;1m%s:\033[m %s\n" 'info' "$*" } warn() { >&2 printf "\033[33;1m%s:\033[m %s\n" 'warning' "$*" } die() { >&2 printf "\033[31;1m%s:\033[m %s\n" 'error' "$*" exit 1 } confirm() { local msg="$1" echo -n "$msg [y/N] " local confirm read -r confirm [[ "${confirm,,}" = 'y' ]] } phelp() { cat < ebil push --site robin.ebil.club dist/ pull | pull your site from ebil.club to a local directory > ebil pull --site robin.ebil.club site/ env: you can set certain options using environment variables. EBIL_SITE="robin.ebil.club" | --site robin.ebil.club EBIL_PATH="dist/" | --path dist/ EBIL_HOST="ebil.club" EBIL_REMOTE="/var/ebil.club/\${name}/\${site}" note: specify options *before* regular arguments. EOF exit 0 } sourceenv() { # shellcheck disable=SC2046 [[ -f .env ]] && export $(grep -v '^#' .env | xargs -0) || return 0 } checksite() { local site="$1" local pat='^[a-z]+.ebil.club$' [[ -z "$site" ]] && die 'site not specified' [[ "$site" =~ $pat ]] || die 'malformed site url' "(expected form '${pat}'):" "$site" } checkpath() { [[ -d "$1" ]] || die 'path does not exist' } rcopy() { local src="$1" local dst="$2" shift shift rsync -rltzq --progress "$@" "$src/" "$dst" } push() { local site="$1" local path="$2" local host="$3" local remote="$4" local name checkpath "$path" if [[ ! -n "$remote" ]]; then checksite "$site" name="${site%.ebil.club}" msg 'user:' "$name" remote="/var/ebil.club/${name}/${site}" fi [[ -f "${path}/index.txt" ]] && msg 'custom curl message configured' "(${path}/index.txt)" [[ -f "${path}/index.html" ]] || warn 'index.html not found' "(${path}/index.html)" msg 'pushing to' "${site:-${remote}}" 'from' "$path" rcopy "$path" "${host}:${remote}" --delete --chmod=D755,F644 } pull() { local site="$1" local path="$2" local host="$3" local remote="$4" if [ ! -d "$path" ]; then msg 'creating destination directory' "$path" mkdir -p "$path" fi if [[ ! -n "$(find "$path" -maxdepth 0 -empty)" ]]; then warn 'destination directory is not empty' "(${path})" confirm 'are you sure you want to use this directory?' fi if [[ ! -n "$remote" ]]; then checksite "$site" name="${site%.ebil.club}" msg 'user:' "$name" remote="/var/ebil.club/${name}/${site}" fi msg 'pulling from' "${site:-${remote}}" 'to' "$path" rcopy "${host}:${remote}" "$path" } main() { sourceenv local cmd='' local site="${EBIL_SITE:-}" local path="${EBIL_PATH:-.}" local host="${EBIL_HOST:-ebil.club}" local remote="${EBIL_REMOTE:-}" local path_opt='' if [ "$#" -gt 0 ]; then cmd="${1#-}" shift fi while [ "$#" -gt 0 ]; do case "$1" in --site) site="$2" shift ;; --path) path_opt="$2" shift ;; --help) phelp ;; *) break ;; esac shift done if [[ -n "$path_opt" ]]; then path="$path_opt" else [[ -n "${1:-}" ]] && path="$1" fi path="$(realpath "$path")" case "$cmd" in push) push "$site" "$path" "$host" "$remote" ;; pull) pull "$site" "$path" "$host" "$remote" ;; '') phelp ;; *) die 'command not found' ;; esac } main "$@"