# utility functions

msg() {
  printf "\033[32;1m%s\033[m %s\n" "${PROMPT-}" "$*"
}

warn() {
  >&2 printf "\033[33;1m%s \033[mwarning: %s\n" "${PROMPT}" "$*"
}

die() {
  >&2 printf "\033[31;1m%s \033[merror: %s\n" "${PROMPT}" "$*"
  exit 1
}

confirm() {
  >&2 printf "\033[33;1m%s \033[mconfirm? %s" "${PROMPT}" "${CONFIRM_PROMPT-}"
  read -r ans
  if [ "${ans}" != y ]; then
    >&2 printf '%s\n' 'Exiting.'
    exit
  fi
}

# params: [full_string] [partial_string]
contains() { [ "${1#*"${2}"}" != "$1" ] || return 1; }

# args: [file] [sum]
chksum() {
  msg "comparing checksums for ${1##*/}"
  sum=$(sha512sum "$1")
  [ "$2" = "${sum%% *}" ] || return 1
}

# args: [compressed_file] [output_dir]
extract() {
  case $1 in
    *tar.* | *.tgz | *.txz | *.tbz)
      decompress "$1" | tar -C "$2" -xf -

      # Ensure extracted tar files are in the top level of $2.
      #
      # note: Ee must use a loop here as glob will match '.'
      #     in some shells even when not in the directory,
      #     which will cause 'mv' to prematurely error.
      for dot in "$2"/*/.*; do
        mv "${dot}" "$2"/ 2>/dev/null || :
      done
      mv "$2"/*/* "$2"/ 2>/dev/null || :

      # remove any empty dirs we may have created
      rmdir "$2"/* 2>/dev/null || :
      ;;
    *.xz | *.gz | *.bz2)
      out=${1##*/}
      decompress "$1" >"$2/${out%.*}"
      ;;
    *.zip)
      unzip -q "$1" -d "$2"
      ;;
    *)
      # not compressed
      cp -f "$1" "$2"/
      ;;
  esac
}

# args: [compressed_file]
decompress() {
  # shellcheck disable=SC2249
  case ${1##*.} in
    gz | tgz)
      if command -v gunzip >/dev/null; then
        gunzip -qdc "$1"
      else
        tgunzip "$1" /dev/stdout
      fi
      ;;
    xz | txz)
      if command -v xz >/dev/null; then
        xz -qdcT 0 "$1"
      else
        xzminidec <"$1"
      fi
      ;;
    bz2 | tbz)
      bunzip2 -qdc "$1"
      ;;
  esac
}

# Helper utility to create "sources" files for packages.
# shellcheck disable=SC2094
mksum() {
  if [ ! -f sources ]; then
    >&2 printf '%s\n' "no sources file found in \$PWD"
    exit 1
  fi
  : >sums
  trap 'rm sums 2>/dev/null' INT

  # file syntax:
  #
  # $url  $checksum
  # $url2 $checksum2
  # $url3 $checksum3
  # ...
  while read -r line; do
    # shellcheck disable=SC2249
    case ${line} in
      //* | \#*) continue ;; # comment
    esac

    url=${line% *}
    # trim leading slash, if exists
    url=${url%/}

    # if file exists, it is a local file (not an actual url)
    [ -e "${url}" ] && continue

    # make temp dir to download file
    dl=/tmp/mksum-${url##*/}
    mkdir -p "${dl}"

    # download and checksum, appending to temp file
    printf 'downloading %s\n' "${url}"
    curl -qL#C - --url "${url}" -o "${dl}/${url##*/}" || exit 1

    # shellcheck disable=SC2312
    read -r sum _ <<-EOF
$(sha512sum "${dl}/${url##*/}")
EOF

    printf '%s %s\n' "${url}" "${sum}" >>sums

    # remove the temp dir
    rm -r "${dl}"
  done <sources

  mv -f sums sources

  # add checksums of all port's files, ex: patches, omitting leading './'
  # shellcheck disable=SC2312
  find . -type f ! -name . ! -name pkgfile ! -name sources \
    | while read -r file; do

      read -r sum _ <<-EOF
$(sha512sum "${file}")
EOF

      printf '%s %s\n' "${file#./}" "${sum}"
    done >>sources
}
