<commit_before>#!/bin/bash -eu
#
# Copyright 2009 Google Inc.
#
# Licensed 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.
#
################################################################################
#
# Outputs a header file comment, with the appropriate comments based on the
# language, as deduced from the extension of the file.
#
# Sample usage:
#   autogen.sh file.js
#   autogen.sh file.py

declare -r AUTOGEN="$(dirname $0)"

LICENSE="${AUTOGEN}/licenses/apache-2.0.txt"
COPYRIGHT_HOLDER="Google Inc."
YEAR="${YEAR:-$(date +%Y)}"

function printLicenseWithYear() {
  cat "${LICENSE}" \
    | sed "s/%YEAR%/${YEAR}/" \
    | sed "s/%COPYRIGHT_HOLDER%/${COPYRIGHT_HOLDER}/"
}

function printLicenseNonHashComment() {
  printLicenseWithYear | sed -E "s#^#$1 #;s/ +$//"
}

function printLicenseHashComment() {
  printLicenseWithYear | sed -E "s/^/# /;s/ +$//"
}

readonly TODO_COMMENT="TODO: High-level file comment."
function printFileCommentTemplate() {
  local comment=$1
  # Fit into 80 cols: repeat enough times, depending on our comment width.
  local repeat=$(echo 80 / $(echo -n ${comment} | wc -c) | bc)
  echo $comment
  perl -e "print \"$comment\" x $repeat . \"\n\""
  echo $comment
  echo "$comment ${TODO_COMMENT}"
}

while getopts c:l:y: opt ; do
  case "${opt}" in
    c)
      COPYRIGHT_HOLDER="${OPTARG}"
      ;;

    l)
      case "${OPTARG}" in
        apache)
          LICENSE="${AUTOGEN}/licenses/apache-2.0.txt"
          ;;
        bsd2|bsd-2)
          LICENSE="${AUTOGEN}/licenses/bsd-2-clause.txt"
          ;;
        bsd3|bsd-3)
          LICENSE="${AUTOGEN}/licenses/bsd-3-clause.txt"
          ;;
        bsd4|bsd-4)
          LICENSE="${AUTOGEN}/licenses/bsd-4-clause.txt"
          ;;
        gpl2|gpl-2)
          LICENSE="${AUTOGEN}/licenses/gpl-2.txt"
          ;;
        gpl3|gpl-3)
          LICENSE="${AUTOGEN}/licenses/gpl-3.txt"
          ;;
        lgpl|lgpl2|lgpl-2|lgpl2.1|lgpl-2.1)
          LICENSE="${AUTOGEN}/licenses/lgpl-2.1.txt"
          ;;
        mit)
          LICENSE="${AUTOGEN}/licenses/mit.txt"
          ;;
        *)
          echo "Invalid license selected: ${OPTARG}" >&2
          exit 1
      esac
      ;;
    y)
      YEAR="${OPTARG}"
      ;;
  esac
done

shift $((OPTIND - 1))

if [[ $# -eq 0 ]]; then
  echo "\
Syntax: $0 [options] <filename>

Options:
  -c [copyright holder]
  -l [license]
  -y [year]

Licenses:
  apache:       Apache 2.0
  bsd2:         BSD, 2-clause, aka Simplified/FreeBSD
  bsd3:         BSD, 3-clause, aka Revised/New/Modified
  bsd4:         BSD, 4-clause, aka Original
  gpl2:         GPL 2
  gpl3:         GPL 3
  lgpl2.1:      LGPL 2.1 (aliases: lgpl, lgpl2)
  mit:          MIT" >&2
  exit 1
fi

case "$1" in

  *.c | *.h | *.css)
    echo "/*"
    printLicenseNonHashComment " *"
    echo " */"
    echo "/* ${TODO_COMMENT} */"
    ;;

  *.cc | *.cpp | *.go | *.hpp | *.java | *.js | *.proto | *.rs | *.scala)
    printLicenseNonHashComment "//"
    printFileCommentTemplate "//"
    ;;

  *.el | *.lisp)
    printLicenseNonHashComment ";;"
    printFileCommentTemplate ";;"
    ;;

  *.erl)
    printLicenseNonHashComment "%"
    printFileCommentTemplate "%"
    ;;

  *.hs)
    printLicenseNonHashComment "--"
    printFileCommentTemplate "--"
    ;;

  *.jsonnet)
    printLicenseHashComment
    printFileCommentTemplate "#"
    ;;

  *.md | *.markdown)
    printLicenseWithYear
    ;;

  *.ml | *.sml)
    echo "(*"
    printLicenseNonHashComment " *"<commit_msg>Added support for *.hh as C++ files.<commit_after>#!/bin/bash -eu
#
# Copyright 2009 Google Inc.
#
# Licensed 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.
#
################################################################################
#
# Outputs a header file comment, with the appropriate comments based on the
# language, as deduced from the extension of the file.
#
# Sample usage:
#   autogen.sh file.js
#   autogen.sh file.py

declare -r AUTOGEN="$(dirname $0)"

LICENSE="${AUTOGEN}/licenses/apache-2.0.txt"
COPYRIGHT_HOLDER="Google Inc."
YEAR="${YEAR:-$(date +%Y)}"

function printLicenseWithYear() {
  cat "${LICENSE}" \
    | sed "s/%YEAR%/${YEAR}/" \
    | sed "s/%COPYRIGHT_HOLDER%/${COPYRIGHT_HOLDER}/"
}

function printLicenseNonHashComment() {
  printLicenseWithYear | sed -E "s#^#$1 #;s/ +$//"
}

function printLicenseHashComment() {
  printLicenseWithYear | sed -E "s/^/# /;s/ +$//"
}

readonly TODO_COMMENT="TODO: High-level file comment."
function printFileCommentTemplate() {
  local comment=$1
  # Fit into 80 cols: repeat enough times, depending on our comment width.
  local repeat=$(echo 80 / $(echo -n ${comment} | wc -c) | bc)
  echo $comment
  perl -e "print \"$comment\" x $repeat . \"\n\""
  echo $comment
  echo "$comment ${TODO_COMMENT}"
}

while getopts c:l:y: opt ; do
  case "${opt}" in
    c)
      COPYRIGHT_HOLDER="${OPTARG}"
      ;;

    l)
      case "${OPTARG}" in
        apache)
          LICENSE="${AUTOGEN}/licenses/apache-2.0.txt"
          ;;
        bsd2|bsd-2)
          LICENSE="${AUTOGEN}/licenses/bsd-2-clause.txt"
          ;;
        bsd3|bsd-3)
          LICENSE="${AUTOGEN}/licenses/bsd-3-clause.txt"
          ;;
        bsd4|bsd-4)
          LICENSE="${AUTOGEN}/licenses/bsd-4-clause.txt"
          ;;
        gpl2|gpl-2)
          LICENSE="${AUTOGEN}/licenses/gpl-2.txt"
          ;;
        gpl3|gpl-3)
          LICENSE="${AUTOGEN}/licenses/gpl-3.txt"
          ;;
        lgpl|lgpl2|lgpl-2|lgpl2.1|lgpl-2.1)
          LICENSE="${AUTOGEN}/licenses/lgpl-2.1.txt"
          ;;
        mit)
          LICENSE="${AUTOGEN}/licenses/mit.txt"
          ;;
        *)
          echo "Invalid license selected: ${OPTARG}" >&2
          exit 1
      esac
      ;;
    y)
      YEAR="${OPTARG}"
      ;;
  esac
done

shift $((OPTIND - 1))

if [[ $# -eq 0 ]]; then
  echo "\
Syntax: $0 [options] <filename>

Options:
  -c [copyright holder]
  -l [license]
  -y [year]

Licenses:
  apache:       Apache 2.0
  bsd2:         BSD, 2-clause, aka Simplified/FreeBSD
  bsd3:         BSD, 3-clause, aka Revised/New/Modified
  bsd4:         BSD, 4-clause, aka Original
  gpl2:         GPL 2
  gpl3:         GPL 3
  lgpl2.1:      LGPL 2.1 (aliases: lgpl, lgpl2)
  mit:          MIT" >&2
  exit 1
fi

case "$1" in

  # TODO(mbrukman): in some projects, *.h are actually C++ files where users
  # want to use C++ style "//" comments. How can we handle this flexibility?
  *.c | *.h | *.css)
    echo "/*"
    printLicenseNonHashComment " *"
    echo " */"
    echo "/* ${TODO_COMMENT} */"
    ;;

  *.cc | *.cpp | *.go | *.hh | *.hpp | *.java | *.js | *.proto | *.rs | *.scala)
    printLicenseNonHashComment "//"
    printFileCommentTemplate "//"
    ;;

  *.el | *.lisp)
    printLicenseNonHashComment ";;"
    printFileCommentTemplate ";;"
    ;;

  *.erl)
    printLicenseNonHashComment "%"
    printFileCommentTemplate "%"
    ;;

  *.hs)
    printLicenseNonHashComment "--"
    printFileCommentTemplate "--"
    ;;

  *.jsonnet)
    printLicenseHashComment
    printFileCommentTemplate "#"
    ;;

  *.md | *.markdown)
    printLicenseWithYear
    ;;

  *.ml | *.sml)
    echo "(*"
    printLicenseNonHashComment " *"
{'fraction_non_alphanumeric': 0.1702389751170239, 'fraction_numerical': 0.016506528701650654, 'mean_word_length': 2.4666951323654995, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2514226', 'n_tokens_mistral': 2885, 'n_tokens_neox': 2693, 'n_words': 686}