#!/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/bar.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")"
python3 - "$CONFIG_FILE" "$position" <<'PY'
import json
import os
import sys

path, position = sys.argv[1], sys.argv[2]
try:
  with open(path) as file:
    data = json.load(file)
    if not isinstance(data, dict):
      data = {}
except (FileNotFoundError, json.JSONDecodeError):
  data = {}

data["position"] = position

with open(path, "w") as file:
  json.dump(data, file, indent=2)
  file.write("\n")
PY

omarchy-restart-bar
