#!/bin/bash
#------------------------------------------------------#
#                  SVG Resize                          #   
# Simple gui resizes SVG's icons in resizable options  # 
#------------------------------------------------------#
#  © 2025 WTFPL lxgator@gmail.com                     #
#------------------------------------------------------#

TITLE='SVG Resize'
VERSION='0.1.5'
ICON='/usr/share/svg-resize/icons/svg-resize.svg'
PSVG='/usr/share/svg-resize/svg-resize.py'
USER=$(who | awk '{print $1}' | head -n1)

#Function to resize icons based on dimensions
resize_icons() {
    local width=$1
    local height=$2
    local label=$3

    files=$(yad --window-icon=$ICON --on-top --text='<b>Select one or more SVG files</b>' --height=500 --width=700 \
    --title='SVG Resize' --file --multiple --filename=/home/"$USER" --file-filter='SVG files | *.svg')

    [ -z "$files" ] && exit 1
    files=${files//|/ } 

    local dir=""

    for f in $files; do
        if [[ "${f##*.}" != "svg" ]]; then
            continue
        fi

        dir=$(dirname "$f")
        $PSVG "$f" -x "$width" -y "$height"
    done

    # Show only folder and resize info
    yad --width=425 --height=100 --fixed --center \
        --text="\nResized icons in $dir at $label" \
        --window-icon=$ICON \
        --title='SVG Resize' \
        --image=$ICON \
        --button=OK:0
}

# Main dialog
action=$(yad \
--width=450 \
--height=115 \
--image=$ICON --on-top \
--button=About!/usr/share/svg-resize/icons/about.svg:3 \
--button=Cancel!/usr/share/svg-resize/icons/cancel.svg:1 \
--button=Ok!/usr/share/svg-resize/icons/ok.svg:0 \
--center --fixed \
--window-icon=$ICON \
--title='SVG Resize' \
--entry \
--column="Icon"  \
--column="Size" \
--text='\nSelect Icon Resize Option' \
'16 x 16' '22 x 22' '24 x 24' '32 x 32' '48 x 48' '64 x 64' '96 x 96' '128 x 128' '256 x 256' '512 x 512')

if [ -n "$action" ]; then
    case $action in
        '16 x 16') resize_icons "4.5" "4.5" "16x16";;
        '22 x 22') resize_icons "6.2" "6.2" "22x22";;
        '24 x 24') resize_icons "6.7" "6.7" "24x24";;
        '32 x 32') resize_icons "9" "9" "32x32";;
        '48 x 48') resize_icons "13.5" "13.5" "48x48";;
        '64 x 64') resize_icons "18" "18" "64x64";;
        '96 x 96') resize_icons "27" "27" "96x96";;
        '128 x 128') resize_icons "36" "36" "128x128";;
        '256 x 256') resize_icons "72.3" "72.3" "256x256";;
        '512 x 512') resize_icons "144.5" "144.5" "512x512";;
    esac
fi |
if [ "$?" = "3" ]; then
    yad --width=450 --on-top --height=100 --fixed --center \
    --text='\n\nA simple gui program that resize svg icons in\nresizable options.\n\n© 2025 WTFPL lxgator@gmail.com\nhttps://www.pclosdebian.com/\n\nThis program is free software: you can redistribute\nit or modify it under the terms of the WTFPL.' \
    --window-icon=$ICON \
    --title="SVG Resize $VERSION" \
    --image=$ICON \
    --button=Cancel!/usr/share/svg-resize/icons/cancel.svg:1
    exec "$0"
fi

