#! /bin/sh -
#
# Multi-user mode script
#
# Copyright (c) 2018-2021 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# To reflect the name of this script
RC="[${0##*/}]"

umask 022
IFS='
 	'
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/usr/sbin:/bin:/usr/bin
LC_ALL=C

### X11-related database

echo "${RC}: Updating X11 database (X.Org), if available"

echo "${RC}: X font index cache ..."
if command -v mkfontscale > /dev/null
then
    for directory in /usr/share/fonts/X11/*
    do
        if test ! -e "${directory}/fonts.dir"
        then
            echo "Using \`mkfontscale' for missing fonts.dir at $directory" 1>&2
            mkfontscale -b -s -l "$directory" && mkfontscale "$directory"
        fi
    done
    unset -v directory
fi

echo "${RC}: fontconfig cache: fc-cache --system-only /usr/share/fonts/X11 &"
if command -v fc-cache > /dev/null
then
    fc-cache --system-only /usr/share/fonts/X11 &
else
    echo "    Could not find fc-cache(1)" 1>&2
fi

echo "${RC}: GTK+-2.0 modules: gtk-query-immodules-2.0 --update-cache &"
if command -v gtk-query-immodules-2.0 > /dev/null
then
    gtk-query-immodules-2.0 --update-cache &
else
    echo "    Could not find gtk-query-immodules-2.0" 1>&2
fi

echo "${RC}: GTK+-3.0 modules: gtk-query-immodules-3.0 --update-cache &"
if command -v gtk-query-immodules-3.0 > /dev/null
then
    gtk-query-immodules-3.0 --update-cache &
else
    echo "    Could not find gtk-query-immodules-3.0" 1>&2
fi

echo "${RC}: GDK pixbuffer loaders: gdk-pixbuf-query-loaders --update-cache &"
if command -v gdk-pixbuf-query-loaders > /dev/null
then
    gdk-pixbuf-query-loaders --update-cache &
else
    echo "    Could not find gdk-pixbuf-query-loaders(1)" 1>&2
fi

echo "${RC}: GIO modules: gio-querymodules /usr/lib/gio/modules &"
if command -v gio-querymodules > /dev/null
then
    if test -r /usr/lib/gio/modules
    then
        gio-querymodules /usr/lib/gio/modules &
    else
        echo "    Could not read /usr/lib/gio/modules" 1>&2
    fi
else
    echo "    Could not find gio-querymodules(1)" 1>&2
fi

echo "${RC}: Glib schemas: glib-compile-schemas /usr/share/glib-2.0/schemas"
if command -v glib-compile-schemas > /dev/null
then
    if test -r /usr/share/glib-2.0/schemas
    then
        glib-compile-schemas /usr/share/glib-2.0/schemas
    else
        echo "    Could not read /usr/share/glib-2.0/schemas" 1>&2
    fi
else
    echo "    Could not find glib-compile-schemas(1)" 1>&2
fi

echo "${RC}: MIME database: update-mime-database /usr/share/mime &"
if command -v update-mime-database > /dev/null
then
    update-mime-database /usr/share/mime &
else
    echo "    Could not find update-mime-database(1)" 1>&2
fi

echo "${RC}: Desktop database: update-desktop-database -q &"
if command -v update-desktop-database > /dev/null
then
    update-desktop-database -q &
else
    echo "    Could not find update-desktop-database(1)" 1>&2
fi

### ^ End Of 'X11-related database'

# Start FUSE filesystem

if test -x /etc/rc.d/rc.fuse
then
    /etc/rc.d/rc.fuse start
fi

# Start services through perpd(8) instances

echo "${RC}: Starting process supervisor under perpd(8) instances"
if ! pidof perpd > /dev/null
then
    perpboot -d /etc/perp
else
    echo "    perpd(8) is already running." 1>&2
fi

# Start GPM (General Purpose Mouse daemon)

if test -x /etc/rc.d/rc.gpm
then
    /etc/rc.d/rc.gpm
fi

# Verify the /etc/fstab file to activate the quota file system

echo "${RC}: Checking quota availability"
if grep -E -q -m 1 '(usr|grp)quota' /etc/fstab
then
    echo "${RC}: Checking /etc/fstab for quota usage"
    if quotacheck -vugcm
    then
        echo "${RC}: Turning ON quota file system: quotaon -vaug"
        quotaon -vaug
    fi
else
    echo "${RC}: No quota filesystem declared on /etc/fstab." 1>&2
fi

# Start user-local instructions

if test -x /etc/rc.d/rc.local
then
    echo "${RC}: Running /etc/rc.d/rc.local"
    /etc/rc.d/rc.local
fi

