#!/bin/bash
#
# Perform initial setup for container.
#
# Ensures that hostname is setup correctly (following /etc/given-hostname),
# bind-mounts shadow directories and files for regular users, etc.
#
# AUTHOR
# ------
#
# Copyright (c) 2019 sakaki <sakaki@deciban.com>
#
# License (GPL v3.0)
# ------------------
#
# 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, either version 3 of the License, or
# (at your option) any later version.
#
# 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.
#

set -e
set -u
shopt -s nullglob

SCRIPT_NAME="$(basename -- "${0}")"

if pidof -o %PPID -x "${SCRIPT_NAME}" &>/dev/null; then
    # already running
    exit 0
fi

if [[ -s /etc/given-hostname ]] && \
       ! diff /etc/hostname /etc/given-hostname &>/dev/null; then
    hostnamectl set-hostname "$(cat /etc/given-hostname)"
fi
HOSTNAME="$(cat /etc/given-hostname)"

# now for all uids 1000 <= uid < 1100 in the guest's
# /etc/passwd file, which have a home directory specified and
# present, bind mount the following, where found:
#   <homedir>/.<file>-$HOSTNAME -> <homedir>/.<file>
#   <homedir>/.<dir>-$HOSTNAME -> <homedir>/.<dir>
# this allows the user to easily prevent 'pollution' between
# 64-bit and 32-bit instances, by creating e.g. a
# ~/.config-debian-buster-64 directory,
# ~/.bash_history-debian-buster-64 file etc
# only regular files and directories are considered for bind mounting
while read -r NEXTLINE; do
    if [[ "$NEXTLINE" =~ ^([^:]+):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*)$ ]]; then
        NUID="${BASH_REMATCH[3]}"
        NDIRECTORY="${BASH_REMATCH[6]}"
        if ((NUID>=1000 && NUID<1100)) && [[ "${NDIRECTORY}" ]] && [[ -d "${NDIRECTORY}" ]]; then
            while read -r NEXTLINE; do
                MASK="${NEXTLINE}"
                ORIG="${MASK%-debian-buster-64}"
                if [[ -d "${MASK}" && -d "${ORIG}" || -f "${MASK}" && -f "${ORIG}" ]]; then
                    # same type, so do the mapping
                    mount --bind "${MASK}" "${ORIG}"
                fi
            done < <(find "${NDIRECTORY}" -mindepth 1 -maxdepth 1 \( -type d -o -type f \) -name '.*-'"${HOSTNAME}")
        fi
    fi
done < "/etc/passwd"

exit 0
