#!/bin/bash

add_params="-Pn"

function initial {

    if [ -f "nmap/initial_$1.nmap" ]; then
       echo "[-] initial scan files are already present on system"
       exit 1
    fi

    echo "[*] Initial scan for $1"
    nmap -T4 -oA "nmap/initial_$1" "$1" "$add_params" 2> /dev/null

}

function allports {

    if [ -f "nmap/all-ports_$1.nmap" ]; then
        echo "[-] initial scan files are already present on system"
        exit 1
    fi
    echo "[*] Full scan for $1"
    nmap -T4 -p- -sV -sC -oA "nmap/all-ports_$1" "$1" "$add_params" 2> /dev/null

}

function udp {

    if [ -f "nmap/udp_$1.nmap" ]; then
        echo "[-] initial scan files are already present on system"
        exit 1
    fi
    echo "[*] UDP scan for $1 (root is needed for that scan-mode)"
    sudo nmap -T4 --top-ports 1000 -sU -oA "nmap/udp_$1" "$1" "$add_params"  2> /dev/null

}

function udpfull {

    if [ -f "nmap/udp-full_$1.nmap" ]; then
        echo "[-] initial scan files are already present on system"
        exit 1
    fi
    echo "[*] Full UDP scan for $1 (root is needed for that scan-mode)"
    sudo nmap -T4 -sV -sU -oA "nmap/udp_$1" "$1" "$add_params"  2> /dev/null

}

if [ $# -ne 2 ]; then
    echo 'scan <range> <type> (initial, all, udp)'
    exit 1
fi

if [ ! -d nmap/ ]; then
    echo "[*] creating nmap folder"
    mkdir nmap
fi

if [ "$2" = "initial" ]; then
    initial "$1"
elif [ "$2" = "full" ] || [ "$2" = "all" ]; then
    allports "$1"
elif [ "$2" = "udp" ]; then
    udp "$1"
elif [ "$2" = "udpfull" ]; then
    udpfull "$1"
fi