#!/bin/sh
# Switch between keyboard layouts

# Add whatever layouts you want
set -- us latam

# Get current layout, and total
current=$(setxkbmap -query | grep layout | cut -d " " -f 6)
total=$(counter=0 ; for layout in "$@" ; do : $((counter+=1)) ; done ; echo "$counter")

index=1
# For each layout, find whichever is currently
# selected, and apply the next one
for layout in "$@" ; do
    if [ "$layout" = "$current" ] ; then
        : $((index+=1))
        [ "$index" -gt "$total" ] && index=1
        setxkbmap $(eval "echo \${${index}}")
        break
    fi
    : $((index+=1))
done

# Restart statusbar module (dwm & dwmblocks)
kill -46 "$(pidof dwmblocks)"