#!/bin/sh
# Install script for fluxMail
# Installs binary, man page, and sample config to ~/.local and ~/.config

set -e

APP="fluxMail"
BIN="fluxMail"
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"

PREFIX="${HOME}/.local"
BIN_DIR="${PREFIX}/bin"
MAN_DIR="${PREFIX}/share/man/man1"
CONFIG_DIR="${HOME}/.config/${APP}"

# Check that binary exists
if [ ! -f "${SCRIPT_DIR}/${BIN}" ]; then
    echo "Error: ${BIN} binary not found in ${SCRIPT_DIR}" >&2
    echo "Build with: cargo build --release" >&2
    exit 1
fi

# Install binary
mkdir -p "${BIN_DIR}"
cp "${SCRIPT_DIR}/${BIN}" "${BIN_DIR}/${BIN}"
chmod 755 "${BIN_DIR}/${BIN}"
echo "Installed ${BIN} to ${BIN_DIR}/${BIN}"

# Install man page
if [ -f "${SCRIPT_DIR}/${APP}.1" ]; then
    mkdir -p "${MAN_DIR}"
    cp "${SCRIPT_DIR}/${APP}.1" "${MAN_DIR}/${APP}.1"
    chmod 644 "${MAN_DIR}/${APP}.1"
    echo "Installed man page to ${MAN_DIR}/${APP}.1"
fi

# Install config
if [ -f "${SCRIPT_DIR}/config" ]; then
    mkdir -p "${CONFIG_DIR}"
    if [ -f "${CONFIG_DIR}/config" ]; then
        cp "${SCRIPT_DIR}/config" "${CONFIG_DIR}/config.sample"
        echo "Config exists; installed sample to ${CONFIG_DIR}/config.sample"
    else
        cp "${SCRIPT_DIR}/config" "${CONFIG_DIR}/config"
        echo "Installed config to ${CONFIG_DIR}/config"
    fi
fi

echo "Done."
