#!/bin/sh
#
#    adapt
#    Copyright (C) 2015 Dustin Kirkland
#
#    Authors: Dustin Kirkland <kirkland@byobu.co>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, version 3 of the License.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.


set -e

usage() {
	echo "
Example usage
   adapt install -r wily -p tomcat7
   adapt run -r xenial -c 'gcc -v'
   adapt alias -r xenial -c juju
   adapt unalias -c juju
   adapt shell -d centos -r 6
   adapt list
"
	exit 0
}

PKG="adapt"
CMD="$1"
[ -n "$1" ] || usage
shift
case "$(uname -m)" in
	x86_64)
		ARCH="amd64"
	;;
	i*86)
		ARCH="i386"
	;;
esac
DISTRO="ubuntu"
RELEASE=$(distro-info --latest 2>/dev/null || distro-info -d)
while [ ! -z "$1" ]; do
	case "$1" in
		-c|--command)
			COMMAND="$2"
			shift 2
		;;
		-v|--verbose|--debug)
			set -x
			shift
		;;
		-d|--distro)
			DISTRO="$2"
			shift 2
		;;
		-p|--packages)
			PACKAGES="$2"
			shift 2
		;;
		-r|--release)
			RELEASE="$2"
			shift 2
		;;
		-h|--help|*)
			usage
		;;
	esac
done
case "$CMD" in
	help|-h|--help)
		usage
	;;
esac

# Default centos to v7
[ "$DISTRO" = "centos" ] && [ -z "$RELEASE" ] && RELEASE=7

# Launch a new image, only if necessary
NAME="$PKG-$DISTRO-$RELEASE"
if ! match=$(lxc list | grep " $NAME "); then
	lxc remote add images images.linuxcontainers.org 2>/dev/null || true
	lxc launch images:$DISTRO/$RELEASE/$ARCH/default "$NAME"
	if [ "$DISTRO" = "ubuntu" ]; then
		# We want command-not-found and unattended upgrades enabled
		run-one-until-success lxc exec $NAME -- ping -c 1 archive.ubuntu.com
		lxc exec $NAME -- apt update --fix-missing
		lxc exec $NAME -- apt install -y --force-yes command-not-found unattended-upgrades
		lxc exec $NAME -- sed -i "s:^//\(.*-updates.*\):    \1:" /etc/apt/apt.conf.d/50unattended-upgrades
	fi
fi

case "$CMD" in
	alias)
		mkdir -p "$HOME/.$PKG"
cat >"$HOME/.$PKG/$COMMAND" <<EOF
#!/bin/sh
lxc exec $NAME -- $COMMAND \$@
EOF
		chmod +x "$HOME/.$PKG/$COMMAND"
		sed -i -e ":PATH=.*$HOME/.$PKG:d" $HOME/.bashrc
		echo "export PATH=$HOME/.$PKG:$PATH" >> $HOME/.bashrc
	;;
	install|purge|erase)
		case "$DISTRO" in
			ubuntu|debian)
				if [ "$CMD" = "install" ]; then
					lxc exec $NAME -- apt install -y --force-yes $PACKAGES
				else
					lxc exec $NAME -- apt purge -y --force-yes $PACKAGES
					lxc exec $NAME -- apt-get autoremove -y
				fi
			;;
			centos|fedora)
				if [ "$CMD" = "install" ]; then
					lxc exec $NAME -- yum install $PACKAGES
				else
					lxc exec $NAME -- yum erase $PACKAGES
				fi
			;;
		esac
	;;
	list)
		lxc list | grep " $PKG-"
	;;
	run)
		lxc exec $NAME -- $COMMAND
	;;
	shell)
		lxc exec $NAME -- $SHELL
	;;
	unalias)
		rm -f "$HOME/.$PKG/$COMMAND"
	;;
	*)
		usage
	;;
esac
