#!/usr/bin/env bash
set -e -x
# If there is a configured resolver, save it.
if [ -a /etc/resolv.conf ]; then
  mv /etc/resolv.conf /etc/resolv.conf.org
fi
# Use the LXC host's dnsmasq service as a resolver
echo "nameserver 10.255.255.1" > /etc/resolv.conf
# Add the host's repository keys, including the RPC-O keys
apt-key add /root/repo.keys
# Update the apt cache
export DEBIAN_FRONTEND=noninteractive
apt-get update
# Check whether there are any installed packages which
# are not available in a configured source.
if apt list --installed 2>/dev/null | egrep '\[.*local.*\]'; then
  # Create a list of those packages.
  pkg_downgrade_list=$(apt list --installed 2>/dev/null | egrep '\[.*local.*\]' | cut -d/ -f1)
  # Work through the list, checking for the latest available version of
  # each package in the configured sources. Put together a list of the
  # packages and their versions in the format that 'apt-get install'
  # expects it.
  pkg_downgrade_list_versioned=""
  for pkg_name in ${pkg_downgrade_list}; do
    # 'apt-cache madison' provides an easy to parse format:
    #   libc-bin | 2.19-0ubuntu6.9 | http://rpc-repo.rackspace.com/apt-mirror/integrated/ r14.0.0rc1-trusty/main amd64 Packages
    #   libc-bin | 2.19-0ubuntu6 | http://mirror.rackspace.com/ubuntu/ trusty/main amd64 Packages
    # The top entry is always the latest package available from a configured source.
    pkg_version=$(apt-cache madison ${pkg_name} | head -n 1 | awk '{ print $3 }')
    pkg_downgrade_list_versioned="${pkg_downgrade_list_versioned} ${pkg_name}=${pkg_version}"
  done
  # Execute the downgrade of all the packages at the same time so that
  # we reduce the likelihood of conflicts.
  apt-get install -y --force-yes ${pkg_downgrade_list_versioned}
fi
# Return the resolver to its previous state.
if [ -a /etc/resolv.conf.org ]; then
  mv /etc/resolv.conf.org /etc/resolv.conf
else
  rm -f /etc/resolv.conf
fi

mkdir -p /etc/ansible/facts.d/
if [ -a /etc/resolv.conf ]; then
  mv /etc/resolv.conf /etc/resolv.conf.org
fi
echo "nameserver 10.255.255.1" >> /etc/resolv.conf
apt-key add /root/repo.keys
rm /root/repo.keys
export DEBIAN_FRONTEND=noninteractive
apt-get update
apt-get install -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" --force-yes apt-transport-https ca-certificates iptables openssh-server python2.7 rsync
apt-get upgrade -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" --force-yes
rm -f /usr/bin/python
ln -s /usr/bin/python2.7 /usr/bin/python
mkdir -p /root/.ssh
chmod 700 /root/.ssh
userdel --force --remove ubuntu || true
apt-get clean
mkdir -p /var/backup
if [ -f  "/usr/lib/systemd/system/poweroff.target" ];then
  ln -sf /usr/lib/systemd/system/poweroff.target /etc/systemd/system/sigpwr.target || true
fi
ln -s /dev/null /etc/systemd/system/systemd-udevd.service || true
ln -s /dev/null /etc/systemd/system/systemd-udevd-control.socket || true
ln -s /dev/null /etc/systemd/system/systemd-udevd-kernel.socket || true
ln -s /dev/null /etc/systemd/system/proc-sys-fs-binfmt_misc.automount || true
echo -e '# The loopback network interface
auto lo
iface lo inet loopback
# LXC interface, this is ALWAYS assumed to be DHCP.
auto eth0
iface eth0 inet dhcp
# Load any additional configs
source /etc/network/interfaces.d/*.cfg
' | tee /etc/network/interfaces
mkdir -p /etc/network/interfaces.d
chage -I -1 -d -1 -m 0 -M 99999 -E -1 root
if [ -a /etc/resolv.conf.org ]; then
  mv /etc/resolv.conf.org /etc/resolv.conf
else
  rm -f /etc/resolv.conf
fi
## post command skipped ##
