#!/bin/sh

# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# This shell script converts all package.html files found
# in a sources directory tree into package-info.java files
# Command-line flags:
#  -h (or --help) to display usage help
#  -v (or --verbose) to display the names of converted files
#  -d (or --delete) to delete the package.html files after conversion
#  -t top-level-package (or --top top-level-package) to set the top level
#     package name to something different from the default org.apache
#  directory for the top directory from which package files are searched

# temporary sed scripts directory
scripts=/tmp/scripts-$$

trap "test -d $scripts && rm -fr $scripts" 0
trap "exit 1" 1 2 15

usage()
{
  echo "usage: sh convert-to-package-info [-h|--help] [-v|verbose] [-d|--delete] [-t|--top top-level-package] directory" 1>&2
  exit $1
}

# set up sed conversion scripts
create_sed_scripts()
{
  if [ ! -d $scripts ] ; then
    mkdir $scripts
  fi

  cat > $scripts/canonicalize.sed <<EOF
s,<body>,<body>\\n,
s,</body>,\\n</body>,
s,\*,\\&lowast;,g
EOF

  cat > $scripts/convert.sed <<EOF
1i\\
/*\\
 * Licensed to the Apache Software Foundation (ASF) under one or more\\
 * contributor license agreements.  See the NOTICE file distributed with\\
 * this work for additional information regarding copyright ownership.\\
 * The ASF licenses this file to You under the Apache License, Version 2.0\\
 * (the "License"); you may not use this file except in compliance with\\
 * the License.  You may obtain a copy of the License at\\
 *\\
 *      http://www.apache.org/licenses/LICENSE-2.0\\
 *\\
 * Unless required by applicable law or agreed to in writing, software\\
 * distributed under the License is distributed on an "AS IS" BASIS,\\
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\\
 * See the License for the specific language governing permissions and\\
 * limitations under the License.\\
 */

:delete-header-footer
\%<body>%{
         s,.*,/**,
         n
         b copy-body
        }
d
n
b delete-header-footer

:copy-body
\%</body>%{
          s,.*, */,
          a\
package $1;
          n
          b delete-header-footer
         }
s,^, *,
n
b copy-body
EOF

}

extract_package_name()
{
  echo $1 | sed -e "s,/,.,g" -e "s,.*\.\($toplevel\..*\)\.package.html$,\1,"
}

help=no
verbose=no
delete=no
toplevel=org.apache
directory=""
while [ $# -gt 0 ] ; do
  case "$1" in
    -h|--help)
                 help=yes
                 shift;;
    -v|--verbose)
                 verbose=yes
                 shift;;
    -d|--delete)
                 delete=yes
                 shift;;
    -t|--top)
                 shift
                 toplevel="$1"
                 shift;;
    *)
                 directory="$1"
                 shift;;
  esac
done

if [ "$help" = "yes" ] ; then
  usage 0
fi
if [ "$directory" = "" ] ; then
  usage 1
fi
if [ ! -d "$directory" ] ; then
  echo "$directory is not a directory" 1>&2
  usage 1
fi

for f in `find "$directory" -name .svn -prune -o -name "package.html" -print` ; do
  package=`extract_package_name "$f"`
  create_sed_scripts $package
  g="`dirname $f`/package-info.java"
  sed -f $scripts/canonicalize.sed < "$f" | sed -f $scripts/convert.sed > "$g"
  if [ "$verbose" = "yes" ] ; then
    echo "converted $f"
  fi
  if [ "$delete" = "yes" ] ; then
    rm -f "$f"
  fi
done
