#!/usr/bin/bash set -euo pipefail term_blue='\033[1;34m' term_green='\033[0;32m' term_yellow='\033[0;33m' term_red='\033[0;31m' term_reset='\033[0m' cat <&2 } # Show an ERROR message then exit with status # $1: message string # $2: exit code number (with 0 does not exit) _msg_error() { local _msg="${1}" local _error=${2} printf '[Gen-Devcerts] %bERROR%b: %s\n' "${term_red}" "${term_reset}" "${_msg}" >&2 if ((_error > 0)); then exit "${_error}" fi } # ======================== # MAIN LOGIC # ======================== # Parse options OPTS=$(getopt -o sqnfh --long save,quiet,noclean,force,help -- "$@") if [[ $? -ne 0 ]]; then _msg_info "Use --help for help." _msg_error "Invalid options" fi eval set -- "$OPTS" while true; do case "$1" in --save | -s) SAVE_LOCAL=1 shift ;; --quiet | -q) QUIET=1 shift ;; --noclean | -n) NO_CLEAN=1 shift ;; --force | -f) IGNORE_ERROR=1 shift ;; --help | -h) _usage exit 1 ;; --) shift break ;; *) _msg_info "Use --help for help." _msg_error "Unknown option: $1" 1 ;; esac done # Detect distro . /etc/os-release _msg_info "Found distribution '$ID'" case "$ID" in debian | ubuntu) PLATFORM_SCRIPT="debian" ;; arch) PLATFORM_SCRIPT="arch" ;; fedora | rhel | centos) PLATFORM_SCRIPT="fedora" ;; *) PLATFORM_SCRIPT="" if [ "$IGNORE_ERROR" = 1 ]; then _msg_warning "Running on unsupported distribution. Platform-specific scripts will not be executed." else _msg_error "Running on unsupported distribution." 1 fi ;; esac # Create working directory _msg_info "Creating working directory" WORK_DIR="$(mktemp -d)" _msg_info "Temp WorkDir at: '$WORK_DIR'" # Set file paths KEYFILE="$WORK_DIR/dotnet-devcert.key" CRTFILE="$WORK_DIR/dotnet-devcert.crt" PFXFILE="$WORK_DIR/dotnet-devcert.pfx" NSSDB_PATHS="$HOME/.pki/nssdb \ $HOME/snap/chromium/current/.pki/nssdb \ $HOME/snap/postman/current/.pki/nssdb" CONF_PATH="$WORK_DIR/localhost.conf" # Post-variable functions cleanup() { _msg_info "Cleaning up" rm -R $WORK_DIR } configure_nssdb() { certutil -d sql:"$1" -D -n dotnet-devcert certutil -d sql:"$1" -A -t "CP,," -n dotnet-devcert -i $CRTFILE } # Write config file _msg_info "Writing certificate config" cat >>$CONF_PATH <