#!/usr/bin/env bash
# 
# Different plaforms add things to diffenent places, even bash, so we use env. 
# 
# parse a maven project directory tree so it can be examined for source structure 
# and be pasted into a wiki with a bit of elbowgrease.
#
# wiki-indented hierarchy goes into hier_out.
# descriptions and package names go into pdescr_out and pname_out. 

OUT_DIR=/tmp/

usage() {
cat << EOF
usage: $0 -i [project] [-c] [options]
   
options:
   -l <level>     : directory levels (defualt 3)
   -o <file>      : output hierarchy to <file> (default ${OUT_DIR}hier_out)
   -n <file>      : output project names to <file> (default ${OUT_DIR}pkname_out)
   -d <file>      : output project descriptions to <file> (default ${OUT_DIR}pkdescr_out)
   -c             : clear default files in ${OUT_DIR}
EOF
}

# argument parsing 
while getopts ":ci:l:o:n:d:" opt; do
  case $opt in
    c)
      cd $OUT_DIR
      rm hier_out pkname_out pkdescr_out
      cd -
      exit
      ;;
    i)
      treedir=$(echo $OPTARG | sed 's:[/]$::')
      ;;
    l)
      level=$OPTARG
      ;;
    o)
      hier_out=$OPTARG
      ;;
    n)
      pname_out=$OPTARG
      ;;
    d)
      pdescr_out=$OPTARG
      ;;
    \?)
      usage
      exit
      ;;
  esac
done

# sanity check
if [ -z $treedir ]; then
  echo "must specify a project directory"
  echo ""
  usage
  exit
fi

# set defaults if unset
: ${level:=3}
: ${hier_out:=${OUT_DIR}hier_out}
: ${pname_out:=${OUT_DIR}pkname_out}
: ${pdescr_out:=${OUT_DIR}pkdescr_out}

# collect up to $level of visible directory names
find $treedir -maxdepth $((${level} - 1)) -type d -not -path '*/\.*' | grep -v src | grep -v target | sort -d > /tmp/h_tmp
# collect all poms up to that level
find $treedir -maxdepth ${level} -name pom.xml > /tmp/p_tmp
# sort and reconstruct path in a greusomely hack way. 
sed 's:pom.xml: pom.xml:g' </tmp/p_tmp > /tmp/p2_tmp
sort -k 1 /tmp/p2_tmp -o /tmp/p2_tmp
sed 's: pom.xml:pom.xml:g' </tmp/p2_tmp > /tmp/p_tmp

# spit slash-separated dirpaths into arrays, then replace all 
# but last with wiki notation list bullet
# Ex: /a/b/c -> [a b c] -> ** c
while read line; do
  # split directory into array: ./a/b/c -> a b c
  arr=($(echo $line | sed 's:[./]: :g'))
  # replace all but last entry with asterisks
  st=$(echo ${arr[@]} | sed 's:[a-zA-Z]*-*[a-zA-Z]* :*:g') 
  # tack another asterisk in front, plus a space : **c -> *** c
  echo \*${st} | sed 's:[a-zA-Z]: &:' >> $hier_out
  #siz=${#arr[@]}
  #if [ $siz -le $level ]; then
  #      echo ${arr[@]} | sed 's:[a-z]* :	:g' | grep -v "src" >> $out
  #fi
done < /tmp/h_tmp

# scrape package name and descriptions from pom.xml files, and bullet-point that
# the correct number of indentations in
# Ex: ./a/b/pom.xml: a-package-name -> ** a-package-name
#     "some package that does junk" -> ** some package that does junk
while read line; do
  #lines are /a/b/c/pom.xml
  #fetch second match, if not, the first, and grab stuff between XML tags
  if [ "$(grep -rn "parent" $line)" ]; then 
    pname=$(grep -hrnm 2 "artifactId" $line | tail -n 1 | sed 's:.*<artifactId>\(.*\)<\/artifactId>:\1:')
  else
    pname=$(grep -hrnm 1 "artifactId" $line | sed 's:.*<artifactId>\(.*\)<\/artifactId>:\1:')
  fi
  pdescr=$(grep -hrn "description" $line | tr '\n' ' ' | sed 's:.*<description>\(.*\)<\/description>.*:\1:') 
  
  #make an array and make that number of stars
  arr=($(echo $line | sed 's:\/: :g'))
  str=$(echo ${arr[@]} | sed -e 's:[A-Za-z]*-*[A-Za-z]* :*:g' -e 's:\(\**\).*:\1:')
  echo $line >> /tmp/pname_raw
  echo "${str}" ${pname} >> $pname_out
  echo "${str}" ${pdescr} >> $pdescr_out
done < /tmp/p_tmp

# tack path info to end
echo "---debug--- " | tee -a $pname_out $pdescr_out 
cat /tmp/pname_raw | tee -a $pname_out $pdescr_out 

rm /tmp/pname_raw /tmp/h_tmp /tmp/p_tmp /tmp/p2_tmp
