💾 Archived View for head.baselab.org › gemlog › files › publish.txt captured on 2021-12-05 at 23:47:19.

View Raw

More Information

⬅️ Previous capture (2021-12-04)

➡️ Next capture (2022-04-28)

-=-=-=-=-=-=-

#!/usr/bin/env bash

## variables
fqdn_url="gemini://example.com/" #your fqdn
local_dir="/path/to/capsule"
remote_user="server_user"
remote_host="server_hostname"
remote_dir="/remote/capusle/fullpath"
content_path="${local_dir}/root" #where the main index.gmi is
token="token.pub" #if you need a way to inform the server to trigger the agate/aerozien/whatever reload
output="/dev/stdout"
xoutput="/dev/stdout"

declare -A update_paths
# list here the relative paths you want to update (as key) and
# the anchor and footer between which you want the list of articles (as value, in the form "anchor|footer")
update_paths=(
    ['root/gemlog']="## Index|\n=> ../ Entrance\n"
)

update_excludes=(
    "/index.gmi"
    "/atom.xml"
)


help() {
    cat <<EOF
 Usage:
   bash ${0} [-m MESSAGE] [-wh]
 Options:
   -m MESSAGE    Do git commit and use this message
   -w            Write content to remote location
   -h            Show this help
EOF
    exit
}

fail() {
    echo -e "${1}" 1>&2
    exit 1
}


while getopts ":m:wh" opt ; do
    case "${opt}" in
        m) commit_msg="${OPTARG}" ;;
	w) do_write="y" ;;
	h) help ;;
	\?) fail "Invalid option: -$OPTARG" ;;
	:) fail "Flag -$OPTARG requires an argument" ;;
    esac
done


## index update
for path in "${!update_paths[@]}" ; do

    findex="${local_dir}/${path}/index.gmi"
    fatom="${local_dir}/${path}/atom.xml"
    fatom_tmpl="${local_dir}/${path}/files/atom.xml.tmpl"
    IFS="|" read -r fanchor footer dirt <<<"${update_paths[${path}]}"
    anchor_ln=$( nl -ba "${findex}" | awk '/^[0-9[:blank:]]*'"${fanchor}"'/{print $1}' )
    entries=()
    [[ "${do_write}" == "y" ]] && { output="${findex}" ; xoutput="${fatom}" ;}  || echo -e "\n--- ${findex} ---"

    readarray -t files < <(
        find "${local_dir}/${path}" -maxdepth 1 -type f \
        | grep -vFf <( printf '%s\n' "${update_excludes[@]}" )
    )

    for file in "${files[@]}" ; do
        fname=$( basename "${file}" )
	fmodtime=$( stat -c'%y' "${file}" | cut -d" " -f1 )
	ftitle=$( awk '/^#/ {for(i=2;i<=NF;i++) printf $i" "; print ""}' "${file}" | head -1 )
	flink="${fqdn_url}/$(basename ${path})/${fname}"
	line="=> ${fname} ${fmodtime} - ${ftitle}"
        xline="  <entry>\n    <id>${flink}</id>\n    <title>${ftitle}</title>\n    <updated>${fmodtime}T00:00:00+00:00</updated>\n    <link href=\"${flink}\" rel=\"alternate\"/>\n  </entry>"
        entries+=( "${line}" )
        xentries+=( "${xline}" )
    done

    echo -e "$(head -n ${anchor_ln} ${findex})\n\n$(printf '%s\n' "${entries[@]}" | sort -k3 -rn)\n${footer}" >"${output}"

    ## atom update
    { sed -r 's/##UPDATED##/'$(date "+%Y-%m-%dT%H:%M:%S.000000+00:00")'/' "${fatom_tmpl}" ; printf '%b\n' "${xentries[@]}\n</feed>" ;} >"${xoutput}"
done

## do publish
if [[ "${do_write}" == "y" ]] ; then
    echo $RANDOM >"${local_dir}/${token}"
    rsync -va --exclude=".git*" -e ssh ${local_dir}/ ${remote_user}@${remote_host}:${remote_dir}/
fi

## git update
[[ -n "${commit_msg}" ]] && cd "${local_dir}" && git add . && git commit -am "${commit_msg}"