#!/bin/sh -e # convert twtxt.txt to atom file # 09/05 : make sha256 usable on linux. Thanks Frederic Galusik. # some help can't hurt help() { printf "usage:\n" printf "\t%s -n -u -t -x twtxt.txt\n" "$0" exit 1 } # make sure variables to check later if user set them nick="" url="" title="" twtxturl="" # use getopt to get options while getopts 'n:u:t:x:h' c do case $c in n) nick="${OPTARG}" ;; u) url="${OPTARG}" ;; t) title="${OPTARG}" ;; x) twtxturl="${OPTARG}" ;; h) help ;; esac done # skip options in argumnent index so $1 is twtxt.txt shift $((OPTIND - 1)) # abort if user forgot a parameter test -z "$nick" && help test -z "$url" && help test -z "$title" && help # header template of the atom HEADER=" ${url} ${title} ${nick} " # get the last entry to set last time atom was updated LAST="$(awk 'END {print $1}' $1)" # Let's output the beginning printf '%s' "$HEADER" printf '%s\n' "$LAST" # grep remove comments and empty lines and loop over twtxt.txt lines grep -Ev '^#|^$' "${1}" | while read -r TS MSG; do # ID must be unique, use sha256 on it if [[ "$OSTYPE" == "linux-gnu" ]]; then ID="$(printf '%s' "$1 $TS $MSG" | sha256sum | cut -f 1 -d " " )" else ID="$(printf '%s' "$1 $TS $MSG" | sha256)" fi # Can't have ]]> in CDATA ESCAPED="$(printf "%s" "${MSG}" | awk '{gsub("]]>", "]]]]>")}1')" # output the entry printf '\n' printf 'urn:hash::sha256:%s\n' "${ID}" printf '<![CDATA[%s]]>\n' "${ESCAPED}" printf '%s\n' "${TS}" printf '\n' "${ESCAPED}" printf '\n' "${twtxturl}" printf '\n' done printf '%s' '' exit 0