hardened makefile and matched codebase to style preferences

This commit is contained in:
James Tomasino 2018-03-05 17:33:38 -05:00
parent 22dd803806
commit 3d1fafae7c
2 changed files with 107 additions and 86 deletions

View File

@ -1,12 +1,22 @@
PREFIX ?= /usr/local PREFIX ?= /usr/local
BINDIR ?= $(PREFIX)/bin BINDIR ?= $(PREFIX)/bin
MANDIR ?= $(PREFIX)/share/man MANDIR ?= $(PREFIX)/share/man
# Attempt to find bash completion dir in order of preference
ifneq ($(wildcard /etc/bash_completion.d/.),) ifneq ($(wildcard /etc/bash_completion.d/.),)
CPLDIR ?= /etc/bash_completion.d CPLDIR ?= /etc/bash_completion.d
else
ifneq ($(shell command -v pkg-config 2> /dev/null))
CPLDIR ?= $$(pkg-config --variable=completionsdir bash-completion)
endif endif
HAS_BREW := $(shell command -v brew 2> /dev/null)
ifdef HAS_BREW
ifneq ($(wildcard $$(brew --prefix)/etc/bash_completion.d/),)
CPLDIR ?= $$(brew --prefix)/etc/bash_completion.d
endif
endif
HAS_PKGCONFIG := $(shell command -v pkg-config 2> /dev/null)
ifdef HAS_PKGCONFIG
CPLDIR ?= $$(pkg-config --variable=completionsdir bash-completion)
endif endif
install: install:
@ -18,16 +28,21 @@ install:
@mkdir -p $(MANDIR) @mkdir -p $(MANDIR)
@cp -f burrow.1 $(MANDIR)/man1/burrow.1 @cp -f burrow.1 $(MANDIR)/man1/burrow.1
@chmod 644 $(MANDIR)/man1/burrow.1 @chmod 644 $(MANDIR)/man1/burrow.1
ifdef CPLDIR
@echo Installing the command completion to $(CPLDIR) @echo Installing the command completion to $(CPLDIR)
@cp -f burrow.d $(CPLDIR)/burrow @cp -f burrow.d $(CPLDIR)/burrow
@chmod 644 $(CPLDIR)/burrow @chmod 644 $(CPLDIR)/burrow
endif
uninstall: uninstall:
@echo Removing the executable from $(BINDIR) @echo Removing the executable from $(BINDIR)
@rm -f $(BINDIR)/burrow @rm -f $(BINDIR)/burrow
@echo Removing the manual page from $(MANDIR)/man1 @echo Removing the manual page from $(MANDIR)/man1
@rm -f $(BINDIR)/man1/burrow.1 @rm -f $(BINDIR)/man1/burrow.1
ifdef CPLDIR
@echo Installing the command completion to $(CPLDIR)
@echo Removing the command completion from $(CPLDIR) @echo Removing the command completion from $(CPLDIR)
@rm -f $(CPLDIR)/burrow @rm -f $(CPLDIR)/burrow
endif
.PHONY: install uninstall .PHONY: install uninstall

170
burrow
View File

@ -33,8 +33,8 @@ flag_help=0
flag_noautoindent=0 flag_noautoindent=0
# vars from args # vars from args
arg_options=hvd arg_options="hvd"
arg_longoptions=help,version,debug,noautoindent arg_longoptions="help,version,debug,noautoindent"
arg_shortlist=0 arg_shortlist=0
arg_recipe=0 arg_recipe=0
arg_phlog=0 arg_phlog=0
@ -51,7 +51,7 @@ popd () {
command popd 2>/dev/null 1>&2 command popd 2>/dev/null 1>&2
} }
function show_help() { function show_help {
cat > /dev/stdout << END cat > /dev/stdout << END
burrow [options] [commands] burrow [options] [commands]
@ -70,8 +70,12 @@ OPTIONAL FLAGS:
END END
} }
function parse_input() { function parse_input {
parsed=$(getopt --options=$arg_options --longoptions=$arg_longoptions --name "$0" -- "$@") parsed=$(getopt \
--options=$arg_options \
--longoptions=$arg_longoptions \
--name "$0" \
-- "$@")
if [[ $? -ne 0 ]]; then if [[ $? -ne 0 ]]; then
die "Invalid input" 2 die "Invalid input" 2
fi fi
@ -106,8 +110,7 @@ function parse_input() {
esac esac
done done
for arg in "$@" for arg in "$@"; do
do
argc=${arg,,} argc=${arg,,}
case $argc in case $argc in
"shortlist") arg_shortlist=1 ;; "shortlist") arg_shortlist=1 ;;
@ -121,7 +124,7 @@ function parse_input() {
done done
} }
function day_suffix() { function day_suffix {
case $(date +%d) in case $(date +%d) in
01|1|21|31) echo "st";; 01|1|21|31) echo "st";;
02|2|22) echo "nd";; 02|2|22) echo "nd";;
@ -130,29 +133,39 @@ function day_suffix() {
esac esac
} }
function update_gopher_date() { function update_gopher_date {
sed -i "s/.*Last\ Updated:.*/ ==== Last Updated: $(date +"%B %d$(day_suffix), %Y") ====/" "${config_dir_gopher}/gophermap" newdate=$(date +"%B %d$(day_suffix), %Y")
sed -i "s/.*Last\ Updated:.*/ ==== Last Updated: $newdate ====/" \
"${config_dir_gopher}/gophermap"
} }
function check_directory() { function check_directory {
if [[ ! -d "$1" ]] if [[ ! -d "$1" ]]; then
then die "Missing directory: $1 aborting." 1
die "Missing directory: $1 aborting."
fi fi
} }
function die() { function die {
if [[ ! -z "$1" ]]; then msg="$1"
echo "$1"; code="$2"
if [[ "$2" =~ /^[0-9]+$/ ]] ; then
exit "$2" # exit code defaults to 1
if [[ "$code" =~ /^[0-9]+$/ ]] ; then
code=1
fi
# output message to stdout or stderr based on code
if [[ ! -z "$msg" ]]; then
if [[ "$code" == 0 ]]; then
echo "$msg"
else else
exit 1 echo "$msg" >&2
fi fi
fi fi
exit "$code"
} }
function finish() { function finish {
if [[ -f "$temp_gophermap" ]]; then if [[ -f "$temp_gophermap" ]]; then
rm "$temp_gophermap" rm "$temp_gophermap"
fi fi
@ -165,17 +178,18 @@ function finish() {
} }
trap finish EXIT trap finish EXIT
function autofold() { function autofold {
file="$1" file="$1"
if $config_autofold; then if $config_autofold; then
temp_fold=$(mktemp -t "$(basename "$0").fold.XXXXXXX") || die "Failed to create temporary file" 1 temp_fold=$(mktemp -t "$(basename "$0").fold.XXXXXXX") || \
die "Failed to create temporary file" 1
fold -s -w "$config_foldwidth" "$file" > "$temp_fold" fold -s -w "$config_foldwidth" "$file" > "$temp_fold"
cat "$temp_fold" > "$file" cat "$temp_fold" > "$file"
rm "$temp_fold" rm "$temp_fold"
fi fi
} }
function make_post() { function make_post {
query="$1" query="$1"
type="$2" type="$2"
use_gophermap="$3" use_gophermap="$3"
@ -186,15 +200,17 @@ function make_post() {
check_directory "${config_dir_gopher}${type}" check_directory "${config_dir_gopher}${type}"
read -r -e -p "$query" title read -r -e -p "$query" title
if [[ $title == "" ]] if [[ $title == "" ]]; then
then
die "Cancelled." 0 die "Cancelled." 0
fi fi
type_gophermap="${config_dir_gopher}${type}/gophermap" type_gophermap="${config_dir_gopher}${type}/gophermap"
if $use_gophermap; then if $use_gophermap; then
title_slug=$(echo "${title}" | sed -E -e 's/[^[:alnum:]]/-/g' -e 's/^-+|-+$//g' | tr -s '-' | tr '[:upper:]' '[:lower:]') title_slug=$(echo "${title}" | \
sed -E -e 's/[^[:alnum:]]/-/g' -e 's/^-+|-+$//g' | \
tr -s '-' | \
tr '[:upper:]' '[:lower:]')
if $use_date; then if $use_date; then
post_dir="${config_dir_gopher}${type}/$(date +%Y%m%d)-$title_slug" post_dir="${config_dir_gopher}${type}/$(date +%Y%m%d)-$title_slug"
post_path="${config_gopher_root}${type}/$(date +%Y%m%d)-$title_slug" post_path="${config_gopher_root}${type}/$(date +%Y%m%d)-$title_slug"
@ -207,7 +223,10 @@ function make_post() {
else else
post_dir="${config_dir_gopher}${type}" post_dir="${config_dir_gopher}${type}"
post_path="${config_gopher_root}${type}" post_path="${config_gopher_root}${type}"
title_slug=$(echo "${title}" | sed -E -e 's/[^[:alnum:]]/-/g' -e 's/^-+|-+$//g' | tr -s '-' | tr '[:upper:]' '[:lower:]') title_slug=$(echo "${title}" | \
sed -E -e 's/[^[:alnum:]]/-/g' -e 's/^-+|-+$//g' | \
tr -s '-' | \
tr '[:upper:]' '[:lower:]')
if $use_date; then if $use_date; then
title_slug="$(date +%Y%m%d)-${title_slug}" title_slug="$(date +%Y%m%d)-${title_slug}"
fi fi
@ -215,13 +234,11 @@ function make_post() {
post_file_path="${post_path}/${title_slug}.txt" post_file_path="${post_path}/${title_slug}.txt"
fi fi
if [[ -f $post_file ]] if [[ -f $post_file ]]; then
then
$editor "$post_file" $editor "$post_file"
else else
mkdir -p "${post_dir}" mkdir -p "${post_dir}"
if [[ -f "${post_dir}/.template" ]] if [[ -f "${post_dir}/.template" ]]; then
then
cat "${post_dir}/.template" > "$post_file" cat "${post_dir}/.template" > "$post_file"
else else
{ {
@ -243,9 +260,9 @@ function make_post() {
$editor "$post_file" $editor "$post_file"
if $use_gophermap; then if $use_gophermap; then
if [[ $config_autoindent == true ]] && [[ $flag_noautoindent == 0 ]] if [[ $config_autoindent ]] && [[ $flag_noautoindent == 0 ]]; then
then temp_links=$(mktemp -t "$(basename "$0").links.XXXXXXX") || \
temp_links=$(mktemp -t "$(basename "$0").links.XXXXXXX") || die "Failed to create temporary file" 1 die "Failed to create temporary file" 1
sed -n '/^Links:$/,$p' "$post_file" | tail -n +2 > "$temp_links" sed -n '/^Links:$/,$p' "$post_file" | tail -n +2 > "$temp_links"
sed -i '/^Links:$/,$d' "$post_file" sed -i '/^Links:$/,$d' "$post_file"
autofold "$post_file" autofold "$post_file"
@ -257,23 +274,28 @@ function make_post() {
autofold "$post_file" autofold "$post_file"
fi fi
temp_gophermap=$(mktemp -t "$(basename "$0").gophermap.XXXXXXX") || die "Failed to create temporary file" 1 temp_gophermap=$(mktemp -t "$(basename "$0").gophermap.XXXXXXX") || \
die "Failed to create temporary file" 1
if $use_gophermap; then if $use_gophermap; then
if $use_date; then if $use_date; then
# if using gophermap and date # if using gophermap and date
echo -e "1$(date +%Y-%m-%d) - $title\t${post_file_path}\t${config_gopher_server}\t${config_gopher_port}" > "$temp_gophermap" echo -e "1$(date +%Y-%m-%d) - $title\t${post_file_path}\t"\
"${config_gopher_server}\t${config_gopher_port}" > "$temp_gophermap"
else else
# if using gophermap but not date # if using gophermap but not date
echo -e "1$title\t${post_file_path}\t${config_gopher_server}\t${config_gopher_port}" > "$temp_gophermap" echo -e "1$title\t${post_file_path}\t"\
"${config_gopher_server}\t${config_gopher_port}" > "$temp_gophermap"
fi fi
else else
if $use_date; then if $use_date; then
# if not using gophermap but using date # if not using gophermap but using date
echo -e "0$(date +%Y-%m-%d) - $title\t${post_file_path}\t${config_gopher_server}\t${config_gopher_port}" > "$temp_gophermap" echo -e "0$(date +%Y-%m-%d) - $title\t${post_file_path}\t"\
"${config_gopher_server}\t${config_gopher_port}" > "$temp_gophermap"
else else
# if not using gophermap or date # if not using gophermap or date
echo -e "0$title\t${post_file_path}\t${config_gopher_server}\t${config_gopher_port}" > "$temp_gophermap" echo -e "0$title\t${post_file_path}\t"\
"${config_gopher_server}\t${config_gopher_port}" > "$temp_gophermap"
fi fi
fi fi
@ -290,16 +312,14 @@ function make_post() {
update_gopher_date update_gopher_date
fi fi
if [[ $config_git_commit != false ]] if [[ $config_git_commit != false ]]; then
then
pushd "$config_dir_gopher" pushd "$config_dir_gopher"
git add "${post_dir}/gophermap" "${post_file}" "$type_gophermap" git add "${post_dir}/gophermap" "${post_file}" "$type_gophermap"
if $update_root; then if $update_root; then
git add "${config_dir_gopher}/gophermap" git add "${config_dir_gopher}/gophermap"
fi fi
git commit -m "$type: $title" git commit -m "$type: $title"
if [[ $config_git_push != false ]] if [[ $config_git_push != false ]]; then
then
git pull git pull
git push git push
fi fi
@ -308,11 +328,11 @@ function make_post() {
fi fi
} }
function create_config() { function create_config {
if [[ ! -f "$HOME/.config/burrow/config" ]] && if [[ ! -f "$HOME/.config/burrow/config" ]] && \
[[ ! -f "$HOME/.config/burrow" ]] && [[ ! -f "$HOME/.config/burrow" ]] && \
[[ ! -f "$HOME/.burrow" ]] [[ ! -f "$HOME/.burrow" ]]; then
then
config="$HOME/.config/burrow/config" config="$HOME/.config/burrow/config"
mkdir -p "$(dirname "$config")" mkdir -p "$(dirname "$config")"
{ {
@ -342,65 +362,53 @@ function create_config() {
echo "config_autoindent=true" echo "config_autoindent=true"
} >> "$config" } >> "$config"
else else
echo "Configuration already exists. Abort." echo "Configuration already exists."
fi fi
} }
function update_git() { function update_git {
pushd "$config_dir_gopher" pushd "$config_dir_gopher"
if [[ $? -eq 0 ]] if [[ $(git rev-parse --is-inside-work-tree 2> /dev/null) ]]; then
then git pull -q
if [[ $(git rev-parse --is-inside-work-tree 2> /dev/null) ]]
then
git pull -q
fi
popd
fi fi
popd
} }
function main() { function main {
parse_input "$@" parse_input "$@"
if [[ $arg_shortlist -gt 0 ]] if [[ $arg_shortlist -gt 0 ]]; then
then die "phlog topic recipe create-config update-git -v -h -d --version \
die "phlog topic recipe create-config update-git -v -h -d --version --help --debug --noautoindent" 0 --help --debug --noautoindent" 0
fi fi
if [[ $flag_version -gt 0 ]] if [[ $flag_version -gt 0 ]]; then
then
echo "$version" echo "$version"
fi fi
if [[ $flag_help -gt 0 ]] if [[ $flag_help -gt 0 ]]; then
then
show_help show_help
fi fi
if [[ $flag_debug -gt 0 ]] if [[ $flag_debug -gt 0 ]]; then
then
set -x set -x
fi fi
if [[ $arg_create_config -gt 0 ]] if [[ $arg_create_config -gt 0 ]]; then
then
create_config create_config
fi fi
for configfile in $configfiles for configfile in $configfiles; do
do if [[ -f $configfile ]]; then
if [[ -f $configfile ]]
then
source "$configfile" source "$configfile"
fi fi
done done
if [[ ${arg_update_git} -gt 0 ]] if [[ ${arg_update_git} -gt 0 ]]; then
then
update_git update_git
fi fi
if [[ ${arg_recipe} -gt 0 ]] if [[ ${arg_recipe} -gt 0 ]]; then
then
make_post "What is the name of your recipe? " \ make_post "What is the name of your recipe? " \
"$config_dir_recipebox" \ "$config_dir_recipebox" \
"$config_recipebox_gophermap" \ "$config_recipebox_gophermap" \
@ -408,8 +416,7 @@ function main() {
true true
fi fi
if [[ ${arg_topic} -gt 0 ]] if [[ ${arg_topic} -gt 0 ]]; then
then
make_post "What is the name of your topic? " \ make_post "What is the name of your topic? " \
"$config_dir_topics" \ "$config_dir_topics" \
"$config_topics_gophermap" \ "$config_topics_gophermap" \
@ -417,8 +424,7 @@ function main() {
true true
fi fi
if [[ ${arg_phlog} -gt 0 ]] if [[ ${arg_phlog} -gt 0 ]]; then
then
make_post "Enter a title for your post: " \ make_post "Enter a title for your post: " \
"$config_dir_phlog" \ "$config_dir_phlog" \
"$config_phlog_gophermap" \ "$config_phlog_gophermap" \