#!/usr/bin/env bash

## Run the given command as a background process and then restart Openbox.
##
## Usage: openbox-restart-after <command> [<command-arg>...]
##
## Dependencies: GNU coreutils, GNU sed, Openbox
##
## I wrote this script after realizing that when I restore a Chrome/Chromium
## session that includes windows that had been maximized, a 1px gap sometimes
## appears above one or more of the windows, and the only way to eliminate the
## 1px gap is to restart Openbox after restoring a Chrome/Chromium session.
## Enabling Openbox's `maximized` setting for Chrome/Chromium doesn't do the
## trick and neither does enabling the Chrome/Chromium setting named `Use system
## title bar and borders`.

set -euo pipefail
shopt -s inherit_errexit

die() { echo "${0##*/}: $1" >&2; exit 1; }

[[ " $* " =~ ' --help ' ]] && sed -n 's/^## \?//p' "$0" && exit
(( $# )) || die 'missing argument'

cmd=$1
type "$cmd" &> /dev/null || die "command not found: $cmd"
"$@" &> /dev/null &
sleep 6
openbox --restart
