<commit_before>#!/usr/bin/env bash

# letsencrypt.sh by lukas2511
# Source: https://github.com/lukas2511/letsencrypt.sh

set -e
set -u
set -o pipefail
umask 077 # paranoid umask, we're creating private keys

# Get the directory in which this script is stored
SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
BASEDIR="${SCRIPTDIR}"

# Check for script dependencies
check_dependencies() {
  # just execute some dummy and/or version commands to see if required tools exist and are actually usable
  openssl version > /dev/null 2>&1 || _exiterr "This script requires an openssl binary."
  _sed "" < /dev/null > /dev/null 2>&1 || _exiterr "This script requires sed with support for extended (modern) regular expressions."
  grep -V > /dev/null 2>&1 || _exiterr "This script requires grep."
  mktemp -u -t XXXXXX > /dev/null 2>&1 || _exiterr "This script requires mktemp."

  # curl returns with an error code in some ancient versions so we have to catch that
  set +e
  curl -V > /dev/null 2>&1
  set -e
  retcode="$?"
  if [[ ! "${retcode}" = "0" ]] && [[ ! "${retcode}" = "2" ]]; then
    _exiterr "This script requires curl."
  fi
}

# Setup default config values, search for and load configuration files
load_config() {
  # Check for config in various locations
  if [[ -z "${CONFIG:-}" ]]; then
    for check_config in "/etc/letsencrypt.sh" "/usr/local/etc/letsencrypt.sh" "${PWD}" "${SCRIPTDIR}"; do
      if [[ -e "${check_config}/config.sh" ]]; then
        BASEDIR="${check_config}"
        CONFIG="${check_config}/config.sh"
        break
      fi
    done
  fi

  # Default values
  CA="https://acme-v01.api.letsencrypt.org/directory"
  LICENSE="https://letsencrypt.org/documents/LE-SA-v1.0.1-July-27-2015.pdf"
  CHALLENGETYPE="http-01"
  HOOK=
  RENEW_DAYS="30"
  PRIVATE_KEY="${BASEDIR}/private_key.pem"
  KEYSIZE="4096"
  WELLKNOWN="${BASEDIR}/.acme-challenges"
  PRIVATE_KEY_RENEW="no"
  KEY_ALGO=rsa
  OPENSSL_CNF="$(openssl version -d | cut -d'"' -f2)/openssl.cnf"
  CONTACT_EMAIL=
  LOCKFILE="${BASEDIR}/lock"

  if [[ -z "${CONFIG:-}" ]]; then
    echo "#" >&2
    echo "# !! WARNING !! No config file found, using default config!" >&2
    echo "#" >&2
  elif [[ -e "${CONFIG}" ]]; then
    echo "# INFO: Using config file ${CONFIG}"
    BASEDIR="$(dirname "${CONFIG}")"
    # shellcheck disable=SC1090
    . "${CONFIG}"
  else
    _exiterr "Specified config file doesn't exist."
  fi

  # Remove slash from end of BASEDIR. Mostly for cleaner outputs, doesn't change functionality.
  BASEDIR="${BASEDIR%%/}"

  # Check BASEDIR and set default variables
  [[ -d "${BASEDIR}" ]] || _exiterr "BASEDIR does not exist: ${BASEDIR}"

  [[ -n "${PARAM_HOOK:-}" ]] && HOOK="${PARAM_HOOK}"
  [[ -n "${PARAM_CHALLENGETYPE:-}" ]] && CHALLENGETYPE="${PARAM_CHALLENGETYPE}"
  [[ -n "${PARAM_KEY_ALGO:-}" ]] && KEY_ALGO="${PARAM_KEY_ALGO}"

  [[ "${CHALLENGETYPE}" =~ (http-01|dns-01) ]] || _exiterr "Unknown challenge type ${CHALLENGETYPE}... can not continue."
  if [[ "${CHALLENGETYPE}" = "dns-01" ]] && [[ -z "${HOOK}" ]]; then
   _exiterr "Challenge type dns-01 needs a hook script for deployment... can not continue."
  fi
  [[ "${KEY_ALGO}" =~ ^(rsa|prime256v1|secp384r1)$ ]] || _exiterr "Unknown public key algorithm ${KEY_ALGO}... can not continue."
}

# Initialize system
init_system() {
  load_config

  # Lockfile handling (prevents concurrent access)
  LOCKDIR="$(dirname "${LOCKFILE}")"
  [[ -w "${LOCKDIR}" ]] || _exiterr "Directory ${LOCKDIR} for LOCKFILE ${LOCKFILE} is not writable, aborting."
  ( set -C; date > "${LOCKFILE}" ) 2>/dev/null || _exiterr "Lock file '${LOCKFILE}' present, aborting."
  remove_lock() { rm -f "${LOCKFILE}"; }
  trap 'remove_lock' EXIT

  # Get CA URLs<commit_msg>Respect configured BASEDIR for default values<commit_after>#!/usr/bin/env bash

# letsencrypt.sh by lukas2511
# Source: https://github.com/lukas2511/letsencrypt.sh

set -e
set -u
set -o pipefail
umask 077 # paranoid umask, we're creating private keys

# Get the directory in which this script is stored
SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
BASEDIR="${SCRIPTDIR}"

# Check for script dependencies
check_dependencies() {
  # just execute some dummy and/or version commands to see if required tools exist and are actually usable
  openssl version > /dev/null 2>&1 || _exiterr "This script requires an openssl binary."
  _sed "" < /dev/null > /dev/null 2>&1 || _exiterr "This script requires sed with support for extended (modern) regular expressions."
  grep -V > /dev/null 2>&1 || _exiterr "This script requires grep."
  mktemp -u -t XXXXXX > /dev/null 2>&1 || _exiterr "This script requires mktemp."

  # curl returns with an error code in some ancient versions so we have to catch that
  set +e
  curl -V > /dev/null 2>&1
  set -e
  retcode="$?"
  if [[ ! "${retcode}" = "0" ]] && [[ ! "${retcode}" = "2" ]]; then
    _exiterr "This script requires curl."
  fi
}

# Setup default config values, search for and load configuration files
load_config() {
  # Check for config in various locations
  if [[ -z "${CONFIG:-}" ]]; then
    for check_config in "/etc/letsencrypt.sh" "/usr/local/etc/letsencrypt.sh" "${PWD}" "${SCRIPTDIR}"; do
      if [[ -e "${check_config}/config.sh" ]]; then
        BASEDIR="${check_config}"
        CONFIG="${check_config}/config.sh"
        break
      fi
    done
  fi

  # Default values
  CA="https://acme-v01.api.letsencrypt.org/directory"
  LICENSE="https://letsencrypt.org/documents/LE-SA-v1.0.1-July-27-2015.pdf"
  CHALLENGETYPE="http-01"
  HOOK=
  RENEW_DAYS="30"
  PRIVATE_KEY=
  KEYSIZE="4096"
  WELLKNOWN=
  PRIVATE_KEY_RENEW="no"
  KEY_ALGO=rsa
  OPENSSL_CNF="$(openssl version -d | cut -d'"' -f2)/openssl.cnf"
  CONTACT_EMAIL=
  LOCKFILE=

  if [[ -z "${CONFIG:-}" ]]; then
    echo "#" >&2
    echo "# !! WARNING !! No config file found, using default config!" >&2
    echo "#" >&2
  elif [[ -e "${CONFIG}" ]]; then
    echo "# INFO: Using config file ${CONFIG}"
    BASEDIR="$(dirname "${CONFIG}")"
    # shellcheck disable=SC1090
    . "${CONFIG}"
  else
    _exiterr "Specified config file doesn't exist."
  fi

  # Remove slash from end of BASEDIR. Mostly for cleaner outputs, doesn't change functionality.
  BASEDIR="${BASEDIR%%/}"

  # Check BASEDIR and set default variables
  [[ -d "${BASEDIR}" ]] || _exiterr "BASEDIR does not exist: ${BASEDIR}"

  [[ -z "${PRIVATE_KEY}" ]] && PRIVATE_KEY="${BASEDIR}/private_key.pem"
  [[ -z "${WELLKNOWN}" ]] && WELLKNOWN="${BASEDIR}/.acme-challenges"
  [[ -z "${LOCKFILE}" ]] && LOCKFILE="${BASEDIR}/lock"

  [[ -n "${PARAM_HOOK:-}" ]] && HOOK="${PARAM_HOOK}"
  [[ -n "${PARAM_CHALLENGETYPE:-}" ]] && CHALLENGETYPE="${PARAM_CHALLENGETYPE}"
  [[ -n "${PARAM_KEY_ALGO:-}" ]] && KEY_ALGO="${PARAM_KEY_ALGO}"

  [[ "${CHALLENGETYPE}" =~ (http-01|dns-01) ]] || _exiterr "Unknown challenge type ${CHALLENGETYPE}... can not continue."
  if [[ "${CHALLENGETYPE}" = "dns-01" ]] && [[ -z "${HOOK}" ]]; then
   _exiterr "Challenge type dns-01 needs a hook script for deployment... can not continue."
  fi
  [[ "${KEY_ALGO}" =~ ^(rsa|prime256v1|secp384r1)$ ]] || _exiterr "Unknown public key algorithm ${KEY_ALGO}... can not continue."
}

# Initialize system
init_system() {
  load_config

  # Lockfile handling (prevents concurrent access)
  LOCKDIR="$(dirname "${LOCKFILE}")"
  [[ -w "${LOCKDIR}" ]] || _exiterr "Directory ${LOCKDIR} for LOCKFILE ${LOCKFILE} is not writable, aborting."
  ( set -C; date > "${LOCKFILE}" ) 2>/dev/null || _exiterr "Lock file '${LOCKFILE}' present, aborting."
  remove_lock() { rm -f "${LOCKFILE}"; }
  trap 'remove_lock' EXIT

  # Get CA URLs
{'fraction_non_alphanumeric': 0.1743191685304565, 'fraction_numerical': 0.017892382581239312, 'mean_word_length': 3.745318352059925, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 33, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, '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': '4461320', 'n_tokens_mistral': 2769, 'n_tokens_neox': 2636, 'n_words': 743}