#!/bin/sh

# Copyright 2015-2023 Rivoreo

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
# IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.


src_dir="${0%/*}"
if [ "$src_dir" != "$0" ] && [ "$src_dir" != . ] && [ "$src_dir" != "$PWD" ] && [ ! "$src_dir" -ef . ]; then
	echo "Error: You must run this program under the directory where it resides" 1>&2
	exit 1
fi
unset src_dir

if [ "`printf 0 | hexdump -v -e '1/1 \"%02x\"' 2> /dev/null`" = 30 ]; then
	crypt_wrapper=crypt.raw.sh
	have_hexdump=1
else
	crypt_wrapper=crypt.hex.sh
	have_hexdump=
fi
alt_shell=
for a in "$@"; do case "$a" in
	--with-hexdump)
		#with_hexdump=1
		crypt_wrapper=crypt.raw.sh
		;;
	--without-hexdump)
		#with_hexdump=
		crypt_wrapper=crypt.hex.sh
		;;
	--with-alt-shell=*)
		alt_shell="${a#--with-alt-shell=}"
		if [ -z "$alt_shell" ]; then
			echo "It can't be empty!" 1>&2
			exit 255
		fi
		;;
	--help|-h)
		printf 'Usage: %s [--with[out]-hexdump] [--with-alt-shell=<path>]\n' "${0##*/}"
		exit 0
		;;
	--)
		break
		;;
	*)
		echo \? 1>&2
		exit 255
		;;
esac done

if [ -z "$have_hexdump" ] && [ "$crypt_wrapper" = crypt.raw.sh ]; then
	echo "Warning: Enabling raw cipher-text I/O, but hexdump(1) isn't appear to be available" 1>&2
fi
if [ -n "$alt_shell" ] && ! "$alt_shell" -c true > /dev/null 2>&1; then
	printf "Warning: Specified alternative shell '%s' not available\\n" "$alt_shell" 1>&2
fi

set -e

blank=
first_blank=
IFS=
while read -r line; do case "$line" in
	"#!"*)
		continue
		;;
	"")
		echo
		[ -n "$blank" ] && break
		blank=1
		if [ -z "$first_blank" ]; then
			echo "# Encrypted history for bash(1)"
			echo "# Automatically assembled code for sourcing from bashrc(5)"
			first_blank=1
		fi
		;;
	"# "*)
		printf %s\\n "$line"
		blank=
		;;
esac done < "$0"
if [ -n "$alt_shell" ]; then
	printf 'XTEA_CTR_CODE="'
	sed -E -e "/^[[:space:]]*#/d" -e "s/([\\\`\$\"])/\\\\\\1/g" xtea-ctr.sh
	printf '"\n\nif "%s" -c true > /dev/null 2>&1; then
	xtea_ctr_crypt() {
		"%s" -c "$XTEA_CTR_CODE\nxtea_ctr_crypt \"\$@\"" xtea-ctr.sh "$@"
	}
else
	eval "$XTEA_CTR_CODE"
fi\n' "$alt_shell" "$alt_shell"
	sed -E "/^[[:space:]]*#/d" "$crypt_wrapper" bashrc
else
	sed -E "/^[[:space:]]*#/d" xtea-ctr.sh "$crypt_wrapper" bashrc
fi
