# -*- coding: utf-8 mode: sh -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
##@require sysinc/base
##@require sysinc/functions

function add_to_crontab() {
    # Ajouter la ligne $1 au crontab de l'utilisateur $2
    local -a crontab=(crontab ${2:+-u "$2"})
    local current="$("${crontab[@]}" -l 2>/dev/null)"
    local tmpfile
    ac_set_tmpfile tmpfile
    if [ -n "$current" ]; then
        echo "$current" >"$tmpfile"
    fi
    if ! quietgrep -F "$1" "$tmpfile"; then
        echo "$1" >>"$tmpfile"
        "${crontab[@]}" "$tmpfile"
        return 0
    fi
    return 1
}

function remove_from_crontab() {
    # Supprimer la ligne $1 du crontab de l'utilisateur $2
    local -a crontab=(crontab ${2:+-u "$2"})
    local current="$("${crontab[@]}" -l 2>/dev/null)"
    local tmpfile
    ac_set_tmpfile tmpfile
    if [ -n "$current" ]; then
        echo "$current" >"$tmpfile"
    fi
    if quietgrep -F "$1" "$tmpfile"; then
        grep -vF "$1" "$tmpfile" | "${crontab[@]}" -
        return 0
    fi
    return 1
}
