#!/bin/bash
# /usr/local/bin/tags
# http://crystalfaeries.net/posix/bin/tags
# celeste:crystalfaery 2014-07-24 22:18:02+00:00
# $0 is invoked by chronicle as post-execution command
# We work FROM all VISIBLE source files in the input directory:
cd "$(grep '^input = ' /etc/chroniclerc | sed 's/^input = //')" || exit -1
# We output a couple invisible files into the same directory:
# the ALPHABETICAL file .tags.txt
# the _______COUNT file .tagcount.txt
####### do the heavy lifting #######
# Generate the basic tags list
grep Tags: * | sed 's/^.*: // ; s/^.*: // ; s/,/\n/g ; s/ //g ; s/ //g' | sort -u | grep -v '^$' > /tmp/tags.$$.txt
# Iterate the tags to count their useage
mkdir -p /tmp/tags.$$
for tag in `cat /tmp/tags.$$.txt`
do
grep Tags: * | grep $tag > /tmp/tags.$$/$tag
done
# Generate the tags list with counts
pushd /tmp/tags.$$ >& /dev/null || exit -1
/usr/local/bin/wc -l * > /tmp/tags.$$.txt
popd >& /dev/null
####### now that the heavy lifting is done, fill out the paperwork #######
# Reset the ALPHABETICAL file to just the headers
echo "__Count TAG____" > .tags.txt
# Append the data to the preset headers
grep -v total /tmp/tags.$$.txt >> .tags.txt
echo "__Count TAG____" >> .tags.txt
grep total /tmp/tags.$$.txt >> .tags.txt
# Reset the COUNT file to just the headers
echo "__COUNT Tag____" > .tagcount.txt
# Append the data to the preset headers
grep -v total /tmp/tags.$$.txt | sort -nr >> .tagcount.txt
echo "__COUNT Tag____" >> .tagcount.txt
grep total /tmp/tags.$$.txt | sort -nr >> .tagcount.txt
# Clean-Up Temporary Space (in the background since it's on an independent partition)
rm -rf /tmp/tags.$$ &
rm /tmp/tags.$$.txt&
disown %1 %2
exit $?
alert # how did we get here?
syntax highlighted by Code2HTML, v. 0.9.1