(define-module (cobra bin)
  #:use-module (guix gexp)
  #:use-module (gnu packages base)
  #:export (%bin:torbrowser
            %bin:suspend-wrap
            %bin:suspend
            %bin:start-pipewire
            %bin:screenshot
            %bin:logout-wofi
            %bin:lock
            %bin:fss
            %bin:beep-wrapper
            %bin:beep
            %bin:backtick))

(define %bin:torbrowser
  (computed-file "bin-torbrowser"
    #~(begin
        (system (string-append #$coreutils "/bin/cat << \"EOF\" >" #$output "\n" "\
#!/usr/bin/env bash

cd ~/.local/share/torbrowser/tor-browser/
guix shell --check --pure --expression='(list (@@ (gnu packages gcc) gcc-11) \"lib\")' coreutils bash grep sed gcc-toolchain patchelf gtk+ dbus-glib libxt libevent openssl file alsa-lib << EOT
set -x
cd Browser
patchelf --set-interpreter \\$LIBRARY_PATH/ld-linux-x86-64.so.2 firefox.real
patchelf --set-interpreter \\$LIBRARY_PATH/ld-linux-x86-64.so.2 TorBrowser/Tor/tor
LD_LIBRARY_PATH=\\$LIBRARY_PATH ./start-tor-browser -v
EOT" "\nEOF"))
        (chmod #$output #o755))))

(define %bin:suspend-wrap
  (computed-file "bin-suspend-wrap"
    #~(begin
        (system (string-append #$coreutils "/bin/cat << \"EOF\" >" #$output "\n" "\
#!/usr/bin/env bash
sudo ~/.local/bin/__suspend
while grep -v '\\[' /sys/power/state; do
        swaylock
        break
done" "\nEOF"))
        (chmod #$output #o755))))

(define %bin:suspend
  (computed-file "bin-suspend"
    #~(begin
        (system (string-append #$coreutils "/bin/cat << \"EOF\" >" #$output "\n" "\
#!/usr/bin/env bash
echo mem > /sys/power/state" "\nEOF"))
        (chmod #$output #o755))))

(define %bin:start-pipewire
  (computed-file "bin-start-pipewire"
    #~(begin
        (system (string-append #$coreutils "/bin/cat << \"EOF\" >" #$output "\n" "\
#!/usr/bin/env bash

rm -rf /run/user/$UID/pulse
pkill pulseaudio
rm -rf /run/user/$UID/pulse

pipewire &
pipewire-pulse &
wireplumber &" "\nEOF"))
        (chmod #$output #o755))))

(define %bin:screenshot
  (computed-file "bin-screenshot"
    #~(begin
        (system (string-append #$coreutils "/bin/cat << \"EOF\" >" #$output "\n" "\
#!/usr/bin/env bash

if [ ! -d $HOME/Pictures/Screenshots ]
then
        mkdir -p $HOME/Pictures/Screenshots
fi

fullscreen() {
        filename=$HOME/Pictures/Screenshots/$(date +%Y%m%d_%H:%M:%S_Fullscreen).png
        grim $filename
        cat $filename | wl-copy \
        && notify-send -i $filename \"Fullscreen Screenshot taken\"
        }

selecting() {
        filename=$HOME/Pictures/Screenshots/$(date +%Y%m%d_%H:%M:%S_Selected).png
        grim -g \"$(slurp -b e3eaf699)\" $filename \
        && notify-send -i $filename \"Selective Screenshot taken\"
        cat $filename | wl-copy
        }

wofirun() {
        case \"$(printf \"fullscreen\\nselect\\n\" | wofi --dmenu -i -H 500 -W 250 -x 0 -y 0 )\" in
                'fullscreen')fullscreen;;
                'select')selecting;;
        esac
}

helpscreen() { 
        echo \"Usage: screenshot [-f] [-s] [-w] [-h] \"
        echo \"Options:\"
        echo \"-f : Fullscreen screenshot\"
        echo \"-s : Select a region for the screenshot\"
        echo \"-w : Shows a wofi prompt for screenshot\"
        echo \"-h : Shows this fucking help menu\"
        echo \"If no flags are selected then -f is chosen\"
}
case $1 in
        -f)fullscreen;;
        -s)selecting;;
        -h)helpscreen;;
        -w)wofirun;;
        *)fullscreen;;
esac" "\nEOF"))
        (chmod #$output #o755))))

(define %bin:logout-wofi
  (computed-file "bin-logout-wofi"
    #~(begin
        (system (string-append #$coreutils "/bin/cat << \"EOF\" >" #$output "\n" "\
#!/usr/bin/env bash

case \"$(printf \"logout\\nreboot\\nshutdown\\nsleep\\n\" | wofi --dmenu -i -H 500 -W 250 -x 0 -y 0 )\" in
        'logout') swaymsg exit;;
        'sleep') ~/.local/bin/suspend ;;
        'reboot') sudo reboot ;;
        'shutdown') sudo poweroff ;;
        *) exit 1 ;;
esac" "\nEOF"))
        (chmod #$output #o755))))

(define %bin:lock
  (computed-file "bin-lock"
    #~(begin
        (system (string-append #$coreutils "/bin/cat << \"EOF\" >" #$output "\n" "\
#!/usr/bin/env bash
if ! ps aux | grep -v grep | grep swaylock; then
#        if [[ $(playerctl status) != \"Playing\" ]]; then
#                swayidle -w timeout 600 'sudo ~/.local/bin/__suspend' &
#                SUSPEND=$!
#        fi
        swayidle -w timeout 15 'swaymsg \"output * dpms off\"' resume 'swaymsg \"output * dpms on\"' &
        SCREENOFF=$!
        swaylock
        kill $SUSPEND $SCREENOFF
fi" "\nEOF"))
        (chmod #$output #o755))))

(define %bin:fss
  (computed-file "bin-fss"
    #~(begin
        (system (string-append #$coreutils "/bin/cat << \"EOF\" >" #$output "\n" "\
#!/usr/bin/env bash
# made by aryak.vern.cc modified by cobra.vern.cc
if [[ -S \"/tmp/mpvfsfsock\" ]]; then
  echo quit | socat - /tmp/mpvfsfsock
else
  mpv --input-ipc-server=/tmp/mpvfsfsock ~/.local/share/fss.ogg
  trap \"rm -rf /tmp/mpvfsfsock\" EXIT
fi" "\nEOF"))
        (chmod #$output #o755))))

(define %bin:beep-wrapper
  (computed-file "bin-beep-wrapper"
    #~(begin
        (system (string-append #$coreutils "/bin/cat << \"EOF\" >" #$output "\n" "\
#!/usr/bin/env bash
while sleep 1; do
        ## Battery beep
        if ls /sys/class/power_supply/BAT* &>/dev/null; then
                BAT0=$(</sys/class/power_supply/BAT0/capacity)
                if [[ $BAT0 -le 5 && -z \"$BEEPED\" && $(</sys/class/power_supply/BAT0/status) == Discharging ]]; then
                        sudo ~/.local/bin/beep 9
                        BEEPED=1
                elif [[ $BAT0 -le 2 && $(</sys/class/power_supply/BAT0/status) == Discharging ]]; then
                        sudo ~/.local/bin/beep 9
                        BEEPED=0
                fi
        fi
done" "\nEOF"))
        (chmod #$output #o755))))

(define %bin:beep
  (computed-file "bin-beep"
    #~(begin
        (system (string-append #$coreutils "/bin/cat << \"EOF\" >" #$output "\n" "\
#!/usr/bin/env sh
echo $1 > /proc/acpi/ibm/beep" "\nEOF"))
        (chmod #$output #o755))))

(define %bin:backtick
  (computed-file "bin-backtick"
    #~(begin
        (system (string-append #$coreutils "/bin/cat << \"EOF\" >" #$output "\n" "\
#!/usr/bin/env bash

cpu() {
        bc<<<\"scale=3;$(ps aux | awk 'BEGIN {sum=0} {sum+=$3}; END {print sum}') / $(nproc --all)\"
}

ram() {
        free -h | sed '2!d' | (read _ _ used _; echo $used)
}

bat() {
        acpi -b | sed 's/Battery /BAT/'
}

echo \"$(cpu)% $(ram)  $(bat)\"" "\nEOF"))
        (chmod #$output #o755))))
