# This file is part of the 'dragora-installer'.
#
# Purpose: Steps after installing Dragora.

echo ""
echo "-> Running post-install instructions from the installer ..."

# Copy local config files to the system partition

# Cover keyboard map if it has been changed, previously
if test -e /etc/rc.conf
then
    # Assume the local config (from the livecd) has been changed
    if ! grep -F -m 1 -q 'RC_KEYMAP=qwerty/us' /etc/rc.conf
    then
        # Make backup of existent rc.conf at /media/dragora-root/etc
        if test -e /media/dragora-root/etc/rc.conf
        then
            cp -p /media/dragora-root/etc/rc.conf \
                  /media/dragora-root/etc/rc.conf.bak || true
        fi
        cp -f /etc/rc.conf /media/dragora-root/etc/ || true
    fi
fi

# Copy GPM run control file
if test -e /etc/rc.d/rc.gpm && test ! -e /media/dragora-root/etc/rc.d/rc.gpm
then
    cp -f /etc/rc.d/rc.gpm /media/dragora-root/etc/rc.d/rc.gpm && \
     chmod 755 /media/dragora-root/etc/rc.d/rc.gpm || true
fi

# Create index of scalable font files for X

echo ""
echo "* Creating index of scalable font files for X, if possible ..."
if test -d /media/dragora-root/usr/share/fonts
then
    for directory in /media/dragora-root/usr/share/fonts/X11/*
    do
        test -d "$directory" || continue

        directory="$(echo "$directory" | sed -e 's|^/media/dragora-root||')"

        chroot /media/dragora-root /usr/bin/mkfontscale -b -s -l "$directory" || true
        chroot /media/dragora-root /usr/bin/mkfontscale "$directory" || true
    done
    unset -v directory

    # Build font information cache files

    echo ""
    echo "* Building font information cache files ..."
    chroot /media/dragora-root /usr/bin/fc-cache -f --system-only || true
else
    echo "  /usr/share/fonts does not exist at /media/dragora-root/" 1>&2
fi

echo ""
echo "* Updating index for the GNU Info document system, if possible ..."
chroot /media/dragora-root \
 /usr/bin/find -L /usr/share/info -maxdepth 1 ! -name dir \
  -exec /usr/bin/install-info '{}' /usr/share/info/dir \; || true

echo ""
echo "* Updating database of manual pages, if possible ..."
mkdir -p /media/dragora-root/var/cache/man && \
 chroot /media/dragora-root /usr/bin/mandb --quiet 2> /dev/null || true

# Delete "lost+found" directory on recent file system creation
rmdir /media/dragora-root/lost+found 2> /dev/null || true

