#!/usr/bin/env bash

log_init() {
  local fallback_dir=""
  local fallback_file=""

  mkdir -p "$RUNTIME_LOG_DIR" 2>/dev/null || true
  if : > "$RUNTIME_LOG_FILE" 2>/dev/null; then
    return 0
  fi

  fallback_dir="${TMPDIR:-/tmp}/postinstall-debian-$(id -u)"
  fallback_file="$fallback_dir/install.log"
  mkdir -p "$fallback_dir"
  RUNTIME_LOG_DIR="$fallback_dir"
  RUNTIME_LOG_FILE="$fallback_file"
  : > "$RUNTIME_LOG_FILE"
}

log_write() {
  local level="$1"
  local message="$2"
  printf '%s [%s] %s\n' "$(date '+%F %T')" "$level" "$message" >> "$RUNTIME_LOG_FILE"
}

log_info() {
  log_write "INFO" "$1"
}

log_error() {
  log_write "ERROR" "$1"
}
