#!/bin/bash
# systemd-tmpfiles doesn't support skipping writing to files that already exist
# - copy the docker group to /etc because docker reads /etc/group directly
# - copy the core user to /etc so the passwd utility works correctly

# Inherit root from environment or command line
ROOT="${1:-$ROOT}"
BASE="${ROOT}/usr/share/baselayout"

# readable files
umask 022
if [[ ! -e "${ROOT}/etc/passwd" ]]; then
    grep "^core:" "${BASE}/passwd" > "${ROOT}/etc/passwd"
fi
if [[ ! -e "${ROOT}/etc/group" ]]; then
    grep "^docker:" "${BASE}/group" > "${ROOT}/etc/group"
fi

# secure files
umask 027
if [[ ! -e "${ROOT}/etc/shadow" ]]; then
    grep "^core:" "${BASE}/shadow" > "${ROOT}/etc/shadow"
fi
if [[ ! -e "${ROOT}/etc/gshadow" ]]; then
    grep "^docker:" "${BASE}/gshadow" > "${ROOT}/etc/gshadow"
fi
