#!/bin/bash
set -e

usage="lxc-rename OLD NEW"
if [ $# -ne 2 ]; then
	echo "invalid parameter count."
	echo $usage
	exit 5
fi
lxc="/var/lib/lxc"
old="$lxc/$1"
new="$lxc/$2"

if [ ! -e $old ]; then
	echo "Container $1 not existing."
	exit 10
fi
if [ -e $new ]; then
	echo "Container $2 existing."
	exit 14
fi

# check if container running
if lxc-ls --fancy $1 |grep RUNNING; then
	echo "Container $1 running. Use:"
	echo "lxc-stop $1"
	exit 17
fi
mv $old $new
sed -i "s/$1/$2/" $new/config
sed -i "s/$1/$2/" $new/rootfs/etc/hostname
sed -i "s/$1/$2/" $new/rootfs/etc/hosts
