#!/bin/bash

# omarchy:summary=Set bar position
# omarchy:args=<top|bottom|left|right>
# omarchy:examples=omarchy-style-bar-position top | omarchy style bar position bottom

set -e

CONFIG_FILE="$HOME/.config/omarchy/shell.json"
position=$1

if [[ ! $position =~ ^(top|bottom|left|right)$ ]]; then
  echo "Usage: omarchy-style-bar-position top|bottom|left|right" >&2
  exit 1
fi

mkdir -p "$(dirname "$CONFIG_FILE")"
tmp=$(mktemp)

if [[ -s $CONFIG_FILE ]] && jq -e 'type == "object"' "$CONFIG_FILE" >/dev/null 2>&1; then
  jq --arg position "$position" '
    .version = 1 |
    .bar = ((.bar // {}) | if type == "object" then . else {} end) |
    .bar.position = $position
  ' "$CONFIG_FILE" >"$tmp"
else
  jq -n --arg position "$position" '{ version: 1, bar: { position: $position } }' >"$tmp"
fi

mv "$tmp" "$CONFIG_FILE"
