#!/bin/bash
#                       /usr/local/bin/blerp
#  http://crystalfaeries.net/posix/bin/blerp
# celeste:crystalfaery BLERP 2017-06-19 04:56:43+00:00

let EMDASH=1	# default to NOT using Em Dashes

# check configuration file for CharacterSet Mode
if [ -r ~/.blerprc ]
then
	grep ANSEL ~/.blerprc > /dev/null
	if [ $? -eq 0 ]
	then	# IN ANSEL MODE (SELECTED)
		echo	"`basename $0`: What part of 'switch to UTF-8 mode by invoking with the -u flag' did you not comprehend?"	1>&2
		echo	"`basename $0`: Your Operating System is too modern to use the Default ANSEL CharacterSet!"	1>&2
	fi
else	# IN ANSEL MODE (DEFAULT)
	echo		"`basename $0`: Your Operating System is too modern to use the Default ANSEL CharacterSet!"	1>&2
	echo		"`basename $0`: It is recommended to switch to UTF-8 mode by invoking with the -u flag :-)"	1>&2
	echo "ANSEL" > ~/.blerprc	# selected by default
fi

while [ $# -gt 0 ]
do	# iterate arguments

if [ $EMDASH -eq 0 ]
then
	FLAG="`echo ${1} | sed 's/^——/--/;s/^—/-/'`"
else
	FLAG="${1}"
fi

case "${FLAG}" in
-a)	# Attack Mode
	shift
	echo "Attack Mode not yet coded"	1>&2
	;;
-b)	# Suppress Bees
	shift
	firefox http://sos-bees.org/	&
	firefox http://crystalfaeries.net/fae/alien_interview.html#bugs+blossoms	&
	echo "Suppressing Bees is suicide!"	1>&2
	;;
-—)	# Flags use Em Dashes (TOGGLE)
	shift
	if [ $EMDASH ]
	then
		let EMDASH=1
	else
		let EMDASH=0
		firefox http://www.thepunctuationguide.com/em-dash.html		&
	fi
	;;
-c)	# Count Number of Arguments
	echo "`basename $0`: Argument Count: $#"	1>&2
	shift
	;;
-d)	# Pipes Output to DEBUG.EXE
	shift
	exec $0 $@ | DEBUG.EXE
	;;
-D)	# Deprecated
	shift
	dict Deprecated | less
	;;
-e)	# Execute SomeThing
	shift
	echo "Execute SomeThing not yet coded"	1>&2
	;;
-f)	# Fun Mode
	shift
	echo "Fun Mode not yet coded"	1>&2
	;;
-g)	# Use Google
	shift
	echo "Use Google not yet coded"	1>&2
	;;
-h)	# Check Whether Input Halts
	shift
	echo "Check Whether Input Halts not yet coded"	1>&2
	;;
--help)	# Help (beyond XKCD)
	shift
	echo 'blerp {[OPTION | ARGS]...[ARGS... -f [FLAGS]...}'				1>&2
	echo 'blerp {...DIRECTORY ...URL | BLERP} OPTIONS ] -{}'			1>&2
	echo 'blerp {--help | --version}	this help or blerp script version'	1>&2
	echo "blerp -U blerp:			updates this script: $0"		1>&2
	echo 'blerp -U {apt,debian}:		updates your system software'		1>&2
	echo 'blerp -U {astro,astrology}:	up-to-date astrological forecast'	1>&2
	echo 'blerp -U fae:			up-to-date crystalfaeries blog'		1>&2
	echo 'blerp -U news:			up-to-date crystalfaeries RSS news'	1>&2
	echo 'blerp -U xkcd:			up-to-date XKCD cartoon'		1>&2
	echo 'blerp -U *:			touch the referenced files'		1>&2

	firefox		https://crystalfaeries.net/imgs/tech/portrait/blerp.png	&
	exit	# standard help behaviour
	;;
-i)	# Ignore Case (lower)
	shift
	echo "Ignore Case (lower) not yet coded"	1>&2
	;;
-I)	# Ignore Case (UPPER)
	shift
	echo "Ignore Case (UPPER) not yet coded"	1>&2
	;;
-jk)	# Just Kidding
	shift
	echo "Just Kidding not yet coded"	1>&2
	;;
-n)	# Behaviour Not Defined
	shift
	firefox https://c.xkcd.com/random/comic/	&
	;;
-o)	# OverWrite
	shift
	echo "OverWrite not yet coded"	1>&2
	;;
-O)	# Opposite Day
	shift
	echo "Opposite Day not yet coded"	1>&2
	echo "Opposite Day is an unofficial holiday, that is generally observed whenever it is declared, where every action is modified so that meaning is negated. It is usually observed among children, and rarely lasts an entire day. Once Opposite Day is declared, statements mean the opposite of what they usually mean. Opposite Day can also be declared retroactively to indicate that what was just asserted had the opposite meaning of what was originally intended (similar to the practice of crossed fingers to automatically nullify promises). Paradoxically, the declaration that it is opposite day is negated as well, implying that it is not opposite day. Play has been compared to a children's 'philosophy course'. The expression 'Opposite Day' can also be used to express facetiousness by sounding sincere instead of sarcastic, allowing for a brief pause before stating it, in the hopes that the person will believe you. For example, upon seeing an ugly woman “Madam, you are one of the most beautiful people I’ve ever seen… because it’s Opposite Day today, right?.”, or upon a teacher handing back a failing test “Wow Joey, you got one of the best marks in the class… oh wait, it’s not Opposite Day.” Another example being upon failing to get or hold the attention of a woman “Wow, Bill sure is good with the ladies… on Opposite Day…” A game can be made out of this fashion by secretly being facetious for an extended period of time before telling the person that it’s “Opposite Day” attempting to mislead them for a laugh." 1>&2
	;;
-p)	# Set True Pope; Accepts "ROME" or "AVIGNON"
	shift
	case ${1} in
	ROME)
		shift
		export POPE="ROME"
		;;
	AVIGNON)
		shift
		export POPE="AVIGNON"
		;;
	*)
		echo "`basename $0`: POPE INVALID OR MISSING"	1>&2
		export POPE="Anti-Christ"
		;;
	esac
	;;
-q)	# Quiet Mode; Output is printed to StdOut instead of being spoken aLOUD!
	shift
	echo "unQuiet Mode not yet coded"	1>&2
	;;
-r)	# Randomize Arguments
	shift
	echo "Randomize Arguments not yet coded"	1>&2
	;;
-R)	# Run Recursively on http://*
	shift
	echo "Run Recursively on http://* not yet coded"	1>&2
	exit	255
#	HOSTnames are composed of series of labels concatenated with dots, as are all DOMAIN names.
#	Each label must be between 1 and 63 characters long.
#	The entire HOSTname (including the delimiting dots but not a trailing dot) has a maximum of 253 ASCII characters.
#	The Internet standards mandate that component HOSTname labels may contain only:
#		ASCII letters 'a' through 'z' (in a case-insensitive manner)
#		digits '0' through '9'
#		hyphen ('-') (may not begin nor end a label)
#	Our strategy shall be to generate minimum to maximum length domain names with all valid permutations
#	then we shall do DNS look-up to see if the generated name is alive
#	if it is not we'll output it to stderr as "down".
#	if it is,    we'll output it to stdout as "up".
	;;
-s)	# Follow Symbolic Links Symbolically
	shift
	echo "Follow Symbolic Links Symbolically not yet coded"	1>&2
	;;
-S)	# Stealth Mode
	shift
	if [ -x "`which tor-browser`" ]
	then
		tor-browser "$@" &
	else
		firefox https://www.torproject.org/download/download-easy.html.en &
	fi
	;;
-t)	# Tumble Dry
	shift
	TUMBLE="$(xrandr | grep connected | grep -v disconnected | sort -k2 | sed 's/ .*$//g' | head -n 1)"
	DRY=normal
	trap "xrandr --output "$TUMBLE" --rotate normal; exit" 0 1 2 15
	let sleep=0
	while sleep $sleep
	do
		let sleep+=1
		case $DRY in
		normal)
			DRY=right
			;;
		right)
			DRY=inverted
			;;
		inverted)
			DRY=left
			;;
		left)
			DRY=normal
			;;
		*)
			xrandr --output "$TUMBLE" --rotate normal
			exit
			;;
		esac
		xrandr --output "$TUMBLE" --rotate $DRY
	done
	exit
	;;
-u)	# UTF-8 Mode, otherwise defaults to ANSEL (TOGGLE)
	shift
	if [ -r ~/.blerprc ]
	then
		grep UTF-8 ~/.blerprc > /dev/null
		if [ $? -eq 0 ]
		then	#  WAS  UTF-8 selected
			grep -v	UTF-8	  ~/.blerprc	 > /tmp/$$.blerprc
			echo	ANSEL			>> /tmp/$$.blerprc
			sort -u				 < /tmp/$$.blerprc \
					> ~/.blerprc
			echo	"`basename $0`: ANSEL MODE has been selected as your NEW DEFAULT in your ~/.blerprc file :-)"	1>&2
		else	#  WAS  ANSEL selected
			grep -v	ANSEL	  ~/.blerprc	 > /tmp/$$.blerprc
			echo	UTF-8			>> /tmp/$$.blerprc
			sort -u				 < /tmp/$$.blerprc \
					> ~/.blerprc
			echo	"`basename $0`: UTF-8 MODE has been selected as your NEW DEFAULT in your ~/.blerprc file :-)"	1>&2
		fi
	else	#  WAS  ANSEL (DEFAULT)
		echo	UTF-8	> ~/.blerprc	# selected
		echo		"`basename $0`: UTF-8 MODE has been selected as your NEW DEFAULT in your ~/.blerprc file :-)"	1>&2
	fi
	;;
-U)	# Update (Default: FaceBook per XKCD
	shift
	if [ "${1}" == "" ]
	then	# Default: FaceBook as specified by XKCD
		firefox https://www.facebook.com/	# not being a facebook user, I don't know a better facebook URL to refer to?
	else
		case "${1}" in
		apt)
			sudo apt-get update && sudo apt-get upgrade && sudo apt-get dist-upgrade || echo $? 1>&2
			;;
		astro)
			firefox http://crystalfaeries.net/fae/archive/	&
			;;
		astrology)
			firefox http://crystalfaeries.net/fae/archive/	&
			;;
		blerp)
			# ensure we can write into /usr/local/bin
			if [ \! -d /usr/local/bin ]
			then
				mkdir -p /usr/local/bin 2>/dev/null		|| sudo \
				mkdir -p /usr/local/bin				#  sudo only if we have to
			fi
			if [ \! -w /usr/local/bin ]
			then
				chmod ug+w /usr/local/bin 2>/dev/null		|| sudo \
				chmod ug+w /usr/local/bin			#  sudo only if we have to
			fi
			touch	/usr/local/bin	|| exit 255			# fix it manually, user
			pushd	/usr/local/bin	|| exit 254			# fix it manually, user
			# hopefully by now we can write into the directory

			mv		/usr/local/bin/blerp{,-}		# hopefully BASH still executing deprecated copy
			rsync -auvzH	/usr/local/bin/blerp{-,}		# clone the old version then overwrite it
			wget -N https://crystalfaeries.net/posix/bin/blerp	# download latest version if newer
			if [ \! -x /usr/local/bin/blerp ]
			then
				chmod +x blerp 2>/dev/null			|| sudo \
				chmod +x blerp					#  sudo only if we have to
			fi
			popd
			;;
		debian)
			sudo apt-get update && sudo apt-get upgrade && sudo apt-get dist-upgrade || echo $? 1>&2
			;;
		fae)
			firefox http://crystalfaeries.net/fae/	&
			;;
		news)
			firefox http://crystalfaeries.net/news.html	&
			;;
		xkcd)
			firefox http://xkcd.com/	&
			;;
		*)
			touch $@
			;;
		esac
		shift
	fi
	;;
-v)	# Verbose
	shift
	find / -exec cat {} \;	# Verbose to da max
	exit
	;;
-V)	# Set Version #
	shift
	head -n 3 $0				>  /tmp/$$.version
	echo -n "# celeste:crystalfaery "	>> /tmp/$$.version
	if [ "${1}" == "" ]
	then
		echo "$(date)"			>> /tmp/$$.version
	else
		echo "${1}"			>> /tmp/$$.version
	fi
	tail -n +5 $0				>> /tmp/$$.version
	cat					   /tmp/$$.version > $0
	exec ${0} --version			# display new version
	;;
--version)	# Version (beyond XKCD)
	shift
	echo -n "$(basename $0): "	1>&2
	tail -n +4 $0 | head -n 1	1>&2
	exit	# standard version behaviour
	;;
-y)	# Yikes
	shift
	echo -n 'Yikes! The whole world can see my:'
	nmap -A -T4 `wget -q -O - ifconfig.co`
	;;
*)
	echo "`basename $0`: left-over arguments: $@"	1>&2
	shift $#
	;;
esac # flag

done # iterating arguments

exit


syntax highlighted by Code2HTML, v. 0.9.1