#!/bin/bash

# Create the walkingo user and group to match the uid and gid
# of the mounted /home/walkingo/logdir directory

cd /home/walkingo || exit 1
[ -d logdir ] || exit 1
wouid=`/usr/bin/stat -c %u logdir`
wogid=`/usr/bin/stat -c %g logdir`

# See if the gid already exists in the docker;
# this usually is because the group of logdir
# is "users" or something like that
grpname=`/usr/bin/getent group $wogid | cut -d: -f1`
if [ "$grpname" == "" ]; then
    /usr/sbin/groupadd -g $wogid walkingo
    grpname=walkingo
fi
/usr/sbin/useradd -g $grpname -u $wouid -s /bin/bash walkingo
/bin/cp -a /etc/skel/.bash* /etc/skel/.profile .

# Give the new walkingo user sudo permissions in the docker
/usr/sbin/adduser walkingo sudo > /dev/null
echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers

# Chown the files in the new user's homedir (pre-populated by
# the docker image), except for the mounted logdir
/usr/bin/find . -name logdir -prune -o -exec chown ${wouid}:${wogid} {} \;

# Run screen as the new walkingo user, ready to be attached
echo "docker container ready; use ./attach-docker to connect to it."
/bin/su walkingo -c "screen -D -m"
