#! /bin/sh
#----------------------------------------------------------------------------
# /var/install/config.d/updatedb - updatedb configuration
#
# Creation   :  2007-02-28 schlotze
# Last update:  $Id: updatedb.sh 48330 2017-12-31 14:57:50Z hbfl $
#
# Copyright (c) 2007-2009 Sebastian Scholze <webmaster(at)schlotze(dot)de
# Copyright (c) 2009-2018 the eisfair team, team(at)eisfair(dot)org
#
# 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 2 of the License, or
# (at your option) any later version.
#----------------------------------------------------------------------------

. /var/install/include/eislib

package_name=updatedb

VERSION=2.8.1

updatedb_conf=/etc/updatedb.conf
crontab_file=/var/cron/etc/root/updatedb
config_file=/etc/config.d/updatedb
updater=/etc/cron.daily/mlocate.cron
# Error log:
error_file=/tmp/updatedb.errors

prunepaths=''

# load user-edited configuration files:
. ${config_file}
chmod 600 ${config_file}

# ---------------------------------------------------------------------------
# create crontab file for updatedb
# ---------------------------------------------------------------------------
create_crontab_file()
{

    cat >${crontab_file} <<EOF
#--------------------------------------------------------------------
#  cron updatedb generated by ${package_name} version: ${VERSION}
#
#  Do not edit this file, edit ${config_file}
#  Creation Date: ${EISDATE} Time: ${EISTIME}
#--------------------------------------------------------------------
EOF

    if [ "${UPDATEDB_SUPPRESS_CRON_MAIL}" = "yes" ]
    then
        echo "${UPDATEDB_CRON_TIMES} ${updater} 1>/dev/null" >> ${crontab_file}
    else
        echo "${UPDATEDB_CRON_TIMES} ${updater}" >> ${crontab_file}
    fi

    /var/install/config.d/cron --quiet
}

# ---------------------------------------------------------------------------
# delete cron job
# ---------------------------------------------------------------------------
delete_crontab_file()
{
    if [ -f ${crontab_file} ]
    then
        rm -f ${crontab_file}
        /var/install/config.d/cron --quiet
    fi
}

# ---------------------------------------------------------------------------
# updatedb.conf
# ---------------------------------------------------------------------------
create_conf()
{
    # prune paths
    idx=1
    while [ ${idx} -le ${UPDATEDB_PRUNEPATH_N:-0} ]
    do
        eval active='${UPDATEDB_PRUNEPATH_'${idx}_'ACTIVE}'

        if [ ${active} != yes ]
        then
            idx=$((${idx} + 1))
            continue
        fi

        eval prunepath='${UPDATEDB_PRUNEPATH_'${idx}'}'
        prunepaths="${prunepath} ${prunepaths}"

        idx=$((${idx} + 1))
    done


    cat >${updatedb_conf} <<EOF
#--------------------------------------------------------------------
# updatedb.conf generated by ${package_name} version: ${VERSION}
#
# Do not edit this file, edit ${config_file}
# Creation Date: ${EISDATE} Time: ${EISTIME}
#--------------------------------------------------------------------
# config file for mlocate

# This file sets variables that are used by updatedb.
# For more info, see the updatedb.conf(5) manpage.

# Filesystems that are pruned from updatedb database
PRUNEFS="afs anon_inodefs auto autofs bdev binfmt binfmt_misc cgroup cifs coda configfs cramfs cpuset debugfs devfs devpts devtmps ecryptfs eventpollfs exofs futexfs ftpfs fuse fusectl gfs gfs2 hostfs hugetlbfs inotifyfs iso9660 jffs2 lustre misc mqueue ncpfs nfs NFS nfs4 nfsd nnpfs ocfs ocfs2 pipefs proc ramfs rpc_pipefs securityfs selinuxfs sfs shfs smbfs sockfs spufs sshfs subfs supermount sysfs tmpfs ubifs udf usbfs vboxsf vperfctrfs"

# Paths which are pruned from updatedb database
PRUNEPATHS="${prunepaths}"

# Folder names that are pruned from updatedb database
PRUNENAMES=".git .hg .svn CVS"

# Skip bind mounts.
PRUNE_BIND_MOUNTS="yes"

EOF

# default prunepaths
# /tmp /var/tmp /var/cache /var/lock /var/run /var/spool /mnt /cdrom /usr/tmp /proc /media /sys /.snapshots /var/run/media


}

# ---------------------------------------------------------------------------
# main
# ---------------------------------------------------------------------------
main()
{
    if [ "${UPDATEDB_CRON_USE}" = "yes" ]
    then
        create_crontab_file
    else
        delete_crontab_file
    fi

    create_conf


    exit 0
}

# ---------------------------------------------------------------------------
# call function main
# ---------------------------------------------------------------------------
main "${@}"

# ---------------------------------------------------------------------------
# end
# ---------------------------------------------------------------------------
