#!/bin/sh

[ "`pidof osmo 2>/dev/null`" ] && killall osmo 2>/dev/null

sleep 0.2

# get screen size
read MX MY << EOF
`wlr-randr | grep -m1 'current' | awk '{print $1}' | tr 'x' ' '`
EOF

[ -f $HOME/.osmo/config.xml ] && CONFIG="$HOME/.osmo/config.xml" || CONFIG="$HOME/.config/osmo/config.xml"

# get current window size
WINDOW_SIZE_X=$(grep '<window_size_x' $CONFIG 2>/dev/null | cut -f2- -d '>' | cut -f1 -d '<')
[ ! "$WINDOW_SIZE_X" ] && WINDOW_SIZE_X="540"

WINDOW_SIZE_Y=$(grep '<window_size_y' $CONFIG 2>/dev/null | cut -f2- -d '>' | cut -f1 -d '<')
[ ! "$WINDOW_SIZE_Y" ] && WINDOW_SIZE_Y="600"

# set default window position (right lower screen)
WINDOW_X=$(expr $MX - $WINDOW_SIZE_X)
WINDOW_Y=$(expr $MY - $WINDOW_SIZE_Y)
WINDOW_Y=$(expr $WINDOW_Y - 48)

sed -i "s/^.*window_x.*$/    <window_x>$WINDOW_X<\/window_x>/" $CONFIG 2>/dev/null
sed -i "s/^.*window_y.*$/    <window_y>$WINDOW_Y<\/window_y>/" $CONFIG 2>/dev/null
sed -i "s/^.*enable_systray.*$/    <enable_systray>0<\/enable_systray>/" $CONFIG 2>/dev/null

exec osmo &
