#! /usr/bin/env bash
#
#   Copyright (c) 2017 Nat! - Mulle kybernetiK
#   Copyright (c) 2017 Nat! - Codeon GmbH
#   All rights reserved.
#
#   Redistribution and use in source and binary forms, with or without
#   modification, are permitted provided that the following conditions are met:
#
#   Redistributions of source code must retain the above copyright notice, this
#   list of conditions and the following disclaimer.
#
#   Redistributions in binary form must reproduce the above copyright notice,
#   this list of conditions and the following disclaimer in the documentation
#   and/or other materials provided with the distribution.
#
#   Neither the name of Mulle kybernetiK nor the names of its contributors
#   may be used to endorse or promote products derived from this software
#   without specific prior written permission.
#
#   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
#   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
#   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
#   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
#   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
#   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
#   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
#   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
#   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
#   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
#   POSSIBILITY OF SUCH DAMAGE.


#
# this is a quick hack to show dependencies of respositories using
# graphviz. It doesn't work 100% because it can not do expansion
# properly. Also the owning projectname is guessed by using the
# enclosing directory-name. Still better than nothing.
#
fail()
{
   echo "Fatal error: $*" >&2
   exit 1
}


draw_repositories()
{
   local path="$1"
   local repository="$2"
   local color="${3:-black}"
   local bgcolor="${4:-white}"

   [ ! -z "${path}" ]       || fail "empty path"
   [ ! -z "${repository}" ] || fail "empty repository"

   local origin
   local filepath
   local name
   local repo
   local line

   IFS="
"
   for filepath in `find "${path}" -name "${repository}" -print | fgrep -v '.bootstrap.auto' | fgrep '.bootstrap/'`
   do
      origin="`dirname -- "${filepath}"`"
      origin="`dirname -- "${origin}"`"
      origin="`basename -- "${origin}"`"

      for line in `cat "${filepath}" | fgrep -v '#'`
      do
         repo="`cut '-d;' -f1 <<< "${line}"`"
         if [ ! -z "${repo}" ]
         then
            name="`basename -- "${repo}"`"
            case "${name}" in
               *.gz|*.tgz)
               ;;

               *)
                  name="${name%.*}"  # remove extension
               ;;
            esac

            if [ ! -z "${name}" ]
            then
               echo "\"${name}\"                    [ fontcolor=\"${color}\"; fillcolor=\"${bgcolor}\"; color=\"${bgcolor}\" ]"
               echo "\"${origin}\" -> \"${name}\"   [ fontcolor=\"${color}\" ]"
            fi
         fi
      done
   done
}



main()
{
   local path="${1:-`pwd -P`}"

   cat <<EOF >&2
mulle-bootstrap-dotdump.sh has known deficiencies.
Nevertheless it can be useful.

Give it a path to your source code, and it will
create a Graphviz dot file, showing you the
dependencies of your .bootstrap enabled projects.
EOF

   cat <<EOF
digraph dependencies
{
      node [ shape="box"; style="filled" ]
EOF

   draw_repositories "${path}" "repositories" "black" "goldenrod"
   draw_repositories "${path}" "embedded_repositories" "white" "indigo"
   draw_repositories "${path}" "brews" "white" "darkgreen"

   cat <<EOF
}
EOF
}

main "$@"