config() {
  NEW="$1"
  OLD="$(dirname $NEW)/$(basename $NEW .new)"
  # If there's no config file by that name, mv it over:
  if [ ! -r $OLD ]; then
    mv $NEW $OLD
  elif [ "$(cat $OLD | md5sum)" = "$(cat $NEW | md5sum)" ]; then
    # toss the redundant copy
    rm $NEW
  fi
  # Otherwise, we leave the .new copy for the admin to consider...
}

prepare_host() {
groupadd -r -g 208 docker
echo "To use docker as a limited user, add your user to the 'docker' group:"
echo "usermod -a -G docker your_user_name"
echo ""
echo "WARNING: Docker does not support mounting cgroups on the 'all' single controller."
echo "Rather, the individual controllers are required.  To accomplish this, edit your"
echo "/etc/fstab with the following contents"
echo ""
echo "cgroup /cgroup/cpuset cgroup rw,relatime,cpuset 0 0"
echo "cgroup /cgroup/cpu cgroup rw,relatime,cpu 0 0"
echo "cgroup /cgroup/cpuacct cgroup rw,relatime,cpuacct 0 0"
echo "cgroup /cgroup/memory cgroup rw,relatime,memory 0 0"
echo "cgroup /cgroup/devices cgroup rw,relatime,devices 0 0"
echo "cgroup /cgroup/freezer cgroup rw,relatime,freezer 0 0"
echo "cgroup /cgroup/net_cls cgroup rw,relatime,net_cls 0 0"
echo "cgroup /cgroup/blkio cgroup rw,relatime,blkio 0 0"

echo ""; echo "In addition, add this to /etc/cgconfig.conf"
echo "mount { "
echo "cpuset = /cgroup/cpuset;"
echo "cpu = /cgroup/cpu;"
echo "cpuacct = /cgroup/cpuacct;"
echo "memory = /cgroup/memory;"
echo "devices = /cgroup/devices;"
echo "freezer = /cgroup/freezer;"
echo "net_cls = /cgroup/net_cls;"
echo "blkio = /cgroup/blkio;"
echo "}"
}
preserve_perms() {
  NEW="$1"
  OLD="$(dirname $NEW)/$(basename $NEW .new)"
  if [ -e $OLD ]; then
    cp -a $OLD ${NEW}.incoming
    cat $NEW > ${NEW}.incoming
    mv ${NEW}.incoming $NEW
  fi
  config $NEW
}

preserve_perms etc/rc.d/rc.docker.new
config etc/default/docker.new
config etc/logrotate.d/docker.new
prepare_host

( cd usr/bin ; rm -rf docker-containerd-shim )
( cd usr/bin ; ln -sf containerd-shim docker-containerd-shim )
( cd usr/bin ; rm -rf docker-containerd-ctr )
( cd usr/bin ; ln -sf ctr docker-containerd-ctr )
( cd usr/bin ; rm -rf docker-runc )
( cd usr/bin ; ln -sf runc docker-runc )
( cd usr/bin ; rm -rf docker-containerd )
( cd usr/bin ; ln -sf containerd docker-containerd )
