#!/bin/bash

#======================================
# Greeting...
#--------------------------------------
echo "Configure image: [$kiwi_iname]..."

#======================================
# Remove locale files
#--------------------------------------
(cd /usr/share/locale && find -name '*.mo' | xargs rm)


#======================================
# Create the registry_DO_NOT_USE_GC symlink needed
# by the harbor registryctl component (see 
# https://github.com/goharbor/harbor/issues/11606)
#--------------------------------------
ln -s /usr/bin/registry /usr/bin/registry_DO_NOT_USE_GC

# Make sure harbor user and groups have always same UID:GID in the container
GROUPID=10000
USERID=10000
USERNAME=harbor
GROUPNAME=harbor
HOMEDIR=/var/lib/harbor

if getent group $GROUPNAME >/dev/null; then
    groupmod -g $GROUPID $GROUPNAME
else
    groupadd -r -g $GROUPID $GROUPNAME
fi

if getent passwd $USERNAME >/dev/null; then
    usermod -u $USERID $USERNAME
    chown -R $USERNAME:$GROUPNAME $HOMEDIR
else
    useradd -r -g $GROUPNAME -d $HOMEDIR -s /sbin/nologin -m -l -c "user for Harbor registry" -u $USERID $USERNAME
fi


# Make sure registry user and groups have always same UID:GID in the container
# The UID and GID must not conflict with the ones for the harbor user/group
GROUPID=10001
USERID=10001
USERNAME=registry
GROUPNAME=registry
HOMEDIR=/var/lib/registry

if getent group $GROUPNAME >/dev/null; then
    groupmod -g $GROUPID $GROUPNAME
else
    groupadd -r -g $GROUPID $GROUPNAME
fi

if getent passwd $USERNAME >/dev/null; then
    usermod -u $USERID $USERNAME
    chown -R $USERNAME:$GROUPNAME $HOMEDIR
else
    useradd -r -g $GROUPNAME -d $HOMEDIR -s /sbin/nologin -m -l -c "user for Docker registry" -u $USERID $USERNAME
fi


exit 0
