#!/bin/bash

PIS_SBIN="${PIS_SBIN:-/sbin}"
if [ -z "${PIS_LIBDIR:-}" ]; then
    case "$(uname -m)" in x86_64) PIS_LIBDIR=/usr/lib64/pis ;; *) PIS_LIBDIR=/usr/lib/pis ;; esac
fi
. "$PIS_LIBDIR/pis_lib" 2>/dev/null || . "$(dirname "$0")/pis_lib" || exit 1
pis_init || exit 1

output="${1:-}"
[ -n "$output" ] || { pis_error "An output filename is required."; exit 1; }
tmp="$(pis_temp_file)" || exit 1
{
    printf '# PIS portable configuration profile\n'
    printf '# Passwords and machine-specific secrets are never exported.\n'
    printf 'PIS_PROFILE_VERSION=1\n'
    for key in language_country keyboard_map login kernels wheel_group user user_wheel gui_desktop gui_keyboard slackpkg_mirror slackpkg_slackpkgplus sbopkg slapt_get_install slapt_get_extra_repo slapt_get_gslapt slapt_get_slapt_src slapt_get_slapt_update_notifier; do
        value="$(pis_state_read "$key")"
        [ -n "$value" ] && printf '%s=%s\n' "$key" "$value"
    done
} > "$tmp"

if [ -e "$output" ]; then pis_backup_file "$output" "profile-export" || { rm -f "$tmp"; exit 1; }; fi
mkdir -p "$(dirname "$output")" || { rm -f "$tmp"; exit 1; }
install -m 600 "$tmp" "$output" || { rm -f "$tmp"; exit 1; }
rm -f "$tmp"
pis_info "Export Profile" "Configuration profile saved to:\n$output\n\nNo passwords were included."
