#!/bin/bash -e
# Copyright 2016 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

# Helper script to grant permission to call `nmap` to the current user. This
# is useful if one wants to call `maas-rack scan-network` in the dev env.

NMAP=$(which nmap)
if [ ! -x "$NMAP" ]; then
    echo "nmap command not found." >&2
    echo "Try:" >&2
    echo "    sudo apt-get install -y nmap" >&2
    exit 1
fi
SUDOERS_LINE="$USER ALL= NOPASSWD: $NMAP"
SUDOERS_FILE=/etc/sudoers.d/99-maas-dev-${USER}-nmap
echo "Installing sudoers file: $SUDOERS_FILE"
echo "$SUDOERS_LINE" | sudo tee $SUDOERS_FILE
sudo chmod 440 $SUDOERS_FILE
echo "Done. You should now be able to to run 'sudo nmap' without a password."
