#!/bin/sh
trap "exit 1" TERM
export TOP_PID=$$
bold=$(tput bold)
normal=$(tput sgr0)

############################################################
# Banner                                                   #
############################################################

printf '%b\n' "$(base64 -d <<<"H4sIAAAAAAAAA32NMRLEMAgDe79C3V0qfYgZ7iF+fLRmkvLMAAIjaen/635q806beQ2eWQ+SywXF3aCEz57WlArTOU6qtj6tHyg7riAeXKYaPbpFX1vfJi/tNzye+UcU9yEmqo2N8Cz8k95RmKzxdNZJJM6ocxtR+Fo34W71EiMBAAA=" | gunzip)" #User printf for colored output
echo

############################################################
# Help                                                     #
############################################################
Help()
{
   # Display Help
   echo "comMENTOR extracts all comments from the specified URL resource."
   echo
   echo "$(basename "$0") [-c <comment type>] [-h] [-u <website>] [-o <filename.txt>]"
   echo
   echo "Options:"
   echo "c     Specify the type of the comment (i.e., xml, cstyle, hashtag)."
   echo "h     Print this Help."
   echo "u     Input URL."
   echo "o     Print to a file."
   echo
   echo "XML-like: <!-- --> (for HTML, XML, ASP, ASPX web pages)"
   echo "CStyle-like: // or /* */ (for PHP, JS, CSS web pages)"
   echo "Hashtag-like: # (for BASH files)"
   echo
   echo "Usage Examples:"
   echo "commentor -u "https://apis.google.com/js/platform.js" -c cstyle"
   echo "commentor -u "https://debian.org" -c xml"
   echo "commentor -u "https://raw.githubusercontent.com/D3vil0p3r/catana/main/catana" -c hashtag"
   echo
}


############################################################
# Process the input options. Add options as needed.        #
############################################################
# Get the options
while getopts ":c:hu:o:" option; do #When using getopts, putting : after an option character means that it requires an argument (i.e., 'i:' requires arg).
   case "${option}" in
      c) 
         comment=$OPTARG
         if [ $comment == "xml" ];then
            xml=1
         fi
         if [ $comment == "cstyle" ]; then
            cstyle=1
         fi
         if [ $comment == "hashtag" ]; then
            hashtag=1
         fi

         if [ $comment != "xml" ] && [ $comment != "cstyle" ] && [ $comment != "hashtag" ];then
            echo "Select a comment type: xml, cstyle, hashtag."
            exit 0
         fi
         ;;
      h) # display Help
         Help >&2
         exit 0
         ;;
      u) 
         url=$OPTARG
         ;;
      o)
         out=$OPTARG
         ;;
      : )
        echo "Missing option argument for -$OPTARG" >&2; exit 0;;
      #*  )
        #echo "Unimplemented option: -$OPTARG" >&2; exit 0;;
     \?) # Invalid option
         echo "Error: Invalid option" >&2
         ;;
   esac
done

echo
if [ ! "$url" ]; then
    echo "Error: no URL specified. Use -u https://targetsite.com/file.html -c <comment type>."
    exit 0
fi

if [ ! "$comment" ]; then
    echo "Error: no valid argument specified. Use -c <comment type> option."
    exit 0
fi

echo "${bold}Target: $url${normal}"
echo
if [ "$xml" ]; then
   #Look for <!-- --> comments
   xmlcomments=$(curl -s -L "$url" | tidy -q -numeric -asxhtml --show-warnings no --custom-tags blocklevel | xmlstarlet sel --html -t -m '//comment()' -v . -n)

   if [ ! "$xmlcomments" ]; then
      echo "No <!-- --> comments found."
   fi

   if [ "$out" ]; then
       echo "<!-- --> comments:" >> $out
       echo >> $out
       echo "$xmlcomments" >> $out
       echo >> $out
       echo "<!-- --> comments correctly stored in ${out} file"
       echo
   else
       echo "${bold}<!-- --> comments:${normal}"
       echo
       echo "$xmlcomments"
       echo
       echo "${bold}End of <!-- --> comments.${normal}"
       echo
   fi
fi

if [ "$cstyle" ]; then
   #Look for /* */ comments
   tmpfile="cstylecomment.txt"
   curl -s -L $url > $tmpfile
   cstylecomments=$(c_decorate "$tmpfile" | c_filter | c_cleanup)
   rm -rf $tmpfile

   if [ ! "$cstylecomments" ]; then
      echo "No // and /* */ comments found."
   fi

   if [ "$out" ]; then
       echo "// and /* */ comments:" >> $out
       echo >> $out
       echo "$cstylecomments" >> $out
       echo "// and /* */ comments correctly stored in ${out} file"
       echo
   else
       echo "${bold}// and /* */ comments:${normal}"
       echo
       echo "$cstylecomments"
       echo
       echo "${bold}End of // and /* */ comments.${normal}"
       echo
   fi
fi

if [ "$hashtag" ]; then
   #Look for # comments
   hashtagcomments=$(curl -s -L "$url" | grep -o "#.*")

   if [ ! "$hashtagcomments" ]; then
      echo "No # comments found."
   fi

   if [ "$out" ]; then
       echo "# comments:" >> $out
       echo >> $out
       echo "$hashtagcomments" >> $out
       echo >> $out
       echo "# comments correctly stored in ${out} file"
       echo
   else
       echo "${bold}# comments:${normal}"
       echo
       echo "$hashtagcomments"
       echo
       echo "${bold}End of # comments.${normal}"
       echo
   fi
fi
