#!/bin/sh
#
# $NetBSD: header,v 1.3 2014/12/30 15:13:20 wiz Exp $

SELF="$0"
PKGNAME="$1"
STAGE="$2"
shift 2

AWK="/usr/bin/awk"
BASENAME="/usr/bin/basename"
CAT="/bin/cat"
CHGRP="/usr/bin/chgrp"
CHMOD="/bin/chmod"
CHOWN="/usr/sbin/chown"
CMP="/usr/bin/cmp"
CP="/bin/cp"
DIRNAME="/usr/bin/dirname"
ECHO="echo"
ECHO_N="echo -n"
EGREP="/usr/bin/egrep"
EXPR="/bin/expr"
FALSE="false"
FIND="/usr/bin/find"
GREP="/usr/bin/grep"
GTAR=""
HEAD="/usr/bin/head"
ID="/usr/bin/id"
LINKFARM="/usr/sbin/linkfarm"
LN="/bin/ln"
LS="/bin/ls"
MKDIR="/bin/mkdir -p"
MV="mv -f"
PERL5="/usr/pkg/bin/perl"
PKG_ADMIN="/usr/sbin/pkg_admin"
PKG_INFO="/usr/sbin/pkg_info"
PWD_CMD="/bin/pwd"
RM="/bin/rm"
RMDIR="/bin/rmdir"
SED="/usr/bin/sed"
SETENV="/usr/bin/env"
SH="/bin/sh"
SORT="/usr/bin/sort"
SU="/usr/bin/su"
TEST="test"
TOUCH="/usr/bin/touch"
TR="/usr/bin/tr"
TRUE="true"
XARGS="/usr/bin/xargs"

CURDIR=`${PWD_CMD}`
: ${PKG_METADATA_DIR=${CURDIR}}
PKGBASE="pkg_comp-cron"

LOCALBASE="/usr/pkg"
X11BASE="/usr/X11R7"
PREFIX="/usr/pkg"

PKG_SYSCONFBASE="/usr/pkg/etc"
PKG_SYSCONFBASEDIR="/usr/pkg/etc"
PKG_SYSCONFDIR="/usr/pkg/etc"
CONF_DEPENDS=""

CONF_IGNORE_FILES="*[~#] *.OLD *.orig *,v .pkgsrc */.pkgsrc"

case "${PKG_CONFIG:-YES}" in
[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
	_PKG_CONFIG=yes
	;;
[Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
	_PKG_CONFIG=yes
	;;
esac

#! /bin/sh

BUILDBASE="/var/pkg_comp"
EGDIR="/usr/pkg/share/examples/pkg_comp-cron"
PKG_COMP_HOME="/var/pkg_comp"
PKG_COMP_USER="root"
PKG_COMP_EGDIR="/usr/pkg/share/examples/pkg_comp"
SANDBOXCTL_EGDIR="/usr/pkg/share/examples/sandboxctl"

# Regexp to match our crontab entry.
CRONTAB_RE="${PREFIX}/sbin/pkg_comp4cron .*${BUILDBASE}/pkg_comp.conf"

# Dumps the pkg_comp user's crontab to a temporary file and outputs
# the path to the file.  If the crontab does not exist, populates
# the output with some basic contents.
get_crontab() {
    local tempfile="$(mktemp "${TMPDIR:-/tmp}/pkg_comp.XXXXXX")"
    if ! crontab -u "${PKG_COMP_USER}" -l >>"${tempfile}"; then
        cat >>"${tempfile}" <<EOF
PATH=${PREFIX}/bin:${PREFIX}/sbin:/usr/bin:/usr/sbin:/bin:/sbin
SHELL=/bin/sh

# Cheatsheet: minute hour day-of-month month day-of-week(0,7=Sun)
EOF
    fi
    echo "${tempfile}"
}

# Adds an entry to for pkg_comp to the crontab if not yet present.
install_crontab() {
    local tempfile; tempfile="$(get_crontab)" || return
    if ! grep "${CRONTAB_RE}" "${tempfile}" >/dev/null; then
        echo "@daily ${PREFIX}/sbin/pkg_comp4cron -l \"${BUILDBASE}/log\"" \
            "-- -c \"${BUILDBASE}/pkg_comp.conf\" auto" \
            >>"${tempfile}"
        crontab -u "${PKG_COMP_USER}" - <"${tempfile}"
        echo "pkg_comp daily entry added to ${PKG_COMP_USER}'s crontab"
    fi
    rm -f "${tempfile}"
}

# Removes the previously-configured crontab entry by this package.
uninstall_crontab() {
    local tempfile; tempfile="$(get_crontab)" || return
    if grep "${CRONTAB_RE}" "${tempfile}" >/dev/null; then
        local tempfile2="$(mktemp "${TMPDIR:-/tmp}/pkg_comp.XXXXXX")"
        grep -v "${CRONTAB_RE}" "${tempfile}" >>"${tempfile2}"
        if [ -s "${tempfile2}" ]; then
            crontab -u "${PKG_COMP_USER}" "${tempfile2}"
        else
            crontab -u "${PKG_COMP_USER}" -r
        fi
        echo "pkg_comp daily entry removed from ${PKG_COMP_USER}'s crontab"
        rm -f "${tempfile2}"
    fi
    rm -f "${tempfile}"
}

case "${STAGE}" in
    POST-INSTALL)
        if [ ! -e "${PKG_COMP_HOME}/sandbox.conf" ]; then
            echo "${PKGNAME}: Installing sample sandbox.conf file"
            ${MKDIR} -p "${PKG_COMP_HOME}"
            sed "s,^SANDBOX_ROOT=.*$,SANDBOX_ROOT=${PKG_COMP_HOME}/sandbox," \
                "${SANDBOXCTL_EGDIR}/default.conf" \
                >"${PKG_COMP_HOME}/sandbox.conf"
        fi

        if [ ! -e "${PKG_COMP_HOME}/extra.mk.conf" ]; then
            echo "${PKGNAME}: Installing sample extra.mk.conf file"
            ${MKDIR} -p "${PKG_COMP_HOME}"
            cp "${PKG_COMP_EGDIR}/extra.mk.conf" "${PKG_COMP_HOME}/"
        fi

        install_crontab
        ;;

    DEINSTALL)
        uninstall_crontab
        ;;
esac
# $NetBSD: deinstall,v 1.5 2017/06/14 16:23:09 prlw1 Exp $

case ${STAGE} in
DEINSTALL)
  #
  # Unregister package from ocaml-findlib.
  #
  ${TEST} ! -x ./+OCAML_FINDLIB_REGISTER ||
    ./+OCAML_FINDLIB_REGISTER REMOVE ${OCAML_FINDLIB_DIR}

	#
	# Unregister info files.
	#
	${TEST} ! -x ./+INFO_FILES ||
		./+INFO_FILES REMOVE ${PKG_METADATA_DIR}
	#
	# Remove any icon theme caches.
	#
	${TEST} ! -x ./+ICON_THEMES ||
		./+ICON_THEMES REMOVE ${PKG_METADATA_DIR}
	#
	# Remove shells from /etc/shells.
	#
	${TEST} ! -x ./+SHELL ||
		./+SHELL REMOVE ${PKG_METADATA_DIR}
	${TEST} ! -x ./+SHELL ||
		./+SHELL CHECK-REMOVE ${PKG_METADATA_DIR}

	# Remove configuration files if they don't differ from the default
	# config file.
	#
	${TEST} ! -x ./+FILES ||
		./+FILES REMOVE ${PKG_METADATA_DIR}
	;;

POST-DEINSTALL)
	#
	# Update any fonts databases.
	#
	${TEST} ! -x ./+FONTS ||
		./+FONTS ${PKG_METADATA_DIR}
	#
	# Update any icon theme caches.
	#
	${TEST} ! -x ./+ICON_THEMES ||
		./+ICON_THEMES UPDATE ${PKG_METADATA_DIR}
	#
	# Rebuild the system run-time library search path database.
	#
	${TEST} ! -x ./+SHLIBS ||
		./+SHLIBS REMOVE ${PKG_METADATA_DIR}
	#
	# Remove empty directories and unused users/groups.
	#
	${TEST} ! -x ./+DIRS ||
		./+DIRS REMOVE ${PKG_METADATA_DIR}
	${TEST} ! -x ./+USERGROUP ||
		./+USERGROUP REMOVE ${PKG_METADATA_DIR}
	#
	# Check for any existing bits after we're finished de-installing.
	#
	${TEST} ! -x ./+USERGROUP ||
		./+USERGROUP CHECK-REMOVE ${PKG_METADATA_DIR}
	${TEST} ! -x ./+FILES ||
		./+FILES CHECK-REMOVE ${PKG_METADATA_DIR}
	${TEST} ! -x ./+DIRS ||
		./+DIRS CHECK-REMOVE ${PKG_METADATA_DIR}
	;;
esac

# $NetBSD: footer,v 1.1 2006/05/21 23:50:15 jlam Exp $

exit 0

