#!/usr/bin/env bash

# Fix problems of directories
here=`dirname "$0"`
cd "$here"

# colors art
. ./modules/colors.sh
# Banner
echo -e """
$red	               /\ o 
	        o     /_ /~/
	         \      /\/
	          \    /   
	           \  / 
                .{{}}}}}}.
               {{{{{}}}}}}}.
              {{{{    {}{{}}}}
             }}}}} -   - {{{{{
             }}}}  0   0  }}}&
            {{{{{---~^~---{{{&
           }}}}}}\   ☭   /}}&&&
           {{{{{{{;._  .;}}&&&&
            '{{{{{{) \_(}}|//&
             ´''''':   :''''´
             [ \e[5m$bgred\033[38;5;232mOWASP D4N155$end $red]
"""

# ignore lower and uppercase
shopt -s nocasematch
# and colors with dont fuck terminal
printf "$end"
# Vars
export aggressive="0"
bug="""
  You can report new bug or open a issue
  \t$yellow→ $orange https://github.com/owasp/D4N155/issues$end
"""
help="""
    D4N155: Tool for smart audit security

    Usage: bash main <option> <value>
    All options are optionals

    Options:
    	-w, --wordlist	<url|ip>	Make the smartwordlist based in informations
					on website.
	-t, --targets	<file>  	Make the smart-wordlist based in your passed
					source informations in urls.
	-b, --based	<file>		Analyze texts to generate the
		                        custom wordlist
	-r, --rate	<time>		Defines time interval between requests
	-o, --output	<file>		For to store the all wordlist.
        -?a, --aggressive                Aggressive reading with headless
    	-h, --help			Show this mensage.

     Value: <url | ip | source | file | time>
     	URL				URL target, example: scanme.nmap.org
	IP				IP address
	TIME				Time, example: 2.5. I.e: 00:00:02:30. 0 are default
	FILE				File, for save the result, get urls or using in
					 wordlist

     Version: 0.9

     It's GNU/GPL version 3
     Project page: https://github.com/owasp/D4N155"""

# All functions
. modules/functions.sh
. modules/load.sh
printf "\033[32m"

trap -- "printf \"\n$bug\";kill $! &> /dev/null;exit 2" "SIGINT"

#	Menu
__interative(){
	printf "\033[0m"
	PS3="D4N155%#~> "
	select option in "Make wordlist tradicional" "Make wordlist aggressive"
	do
		case $option in
			"Make wordlist tradicional")
        export aggressive="0"
				__wordlist
				;;
			"Make wordlist aggressive")
        export aggressive="1"
				__wordlist
				;;
			*) echo -e "\033[31mRFTM\033[0m";exit ;;
		esac
	done
	printf "\033[0m"
}

if [[ ! "$1" ]]
then
  __interative
else
	# vars for iterations
	i=1
	x=1
  util=0
	save=""
  time=""

	while [ "$i" -le "$#" ]
	do
		# arg: argument
		# narg: next arg.
		# parg: primary arg.
		# pvarg: primary value of arg.

		eval "arg=\${$i}"
		eval "narg=\${$(($i+1))}"
		
		# The block of code are for arguments
		# like "-o, --output", for get the arg.
		# when will be used.

		while [ "$x" -le "$#" ]
		do
			# para: parameter
			# dest: destination
			eval "para=\${$x}"
			eval "dest=\${$(($x+1))}"

			case "$para" in
				--o* | "-o")
					# export the file for storaged
					export save="$dest" 
					;;
        --r* | "-r")
          export time="$dest"
          ;;
        --a* | -*a*)
          export aggressive="1"
          _checkGecko
          ;;
			esac

			# For work in loop
			x=$(($x+1))
		done

		case "$arg" in
		        --h* | "-h")
				echo "$help"
				exit 0
				;;&
			--w* | -*w*)
				echo "Make the smart wordlist"
				if [[ $narg =~ ^- ]]
				then
					test "$save" == "" && \
					       __wordlist "" "" "$time"|| \
					       __wordlist "" "$save" "$time"
				else
					test "$save" == "" && \
					       __wordlist "$narg" "" "$time"|| \
					       __wordlist "$narg" "$save" "$time"
				fi 
        
        util=$(($util+1))
				;;&
      --b* | -*b*)
        echo "Make custom wordlist"
        if [ $save ]
        then
            [ "$narg" ] && __cus "$narg" "$save" || ( echo -e " $orange → bash main --help$end ";exit 2)
        else
            [ "$narg" ] && __cus "$narg" || ( echo -e " $orange → bash main --help$end ";exit 2 )
        fi

        util=$(($util+1))
        ;;
			--t* | -*t*)
				echo -e "Targets inputed in  $orange $narg $green "
				__fwordlist "$narg" "" "$time" 

        
        util=$(($util+1))
				;;

		esac
		i=$(($i+1))
	done

  if [ $util == 0 ]
  then
    echo "Opening to interative mode..."
    __interative
  fi
fi
