#!/usr/bin/env bash
# ############################################################################ #
# SPDX-FileCopyrightText: © 2018, Nathan Salaun <natinusala@gmail.com>         #
# SPDX-FileCopyrightText: © 2019, Alfredo Monclus <alfredomonclus@gmail.com>   #
# SPDX-FileCopyrightText: © 2019, Chad Margetts <baxysquare@gmail.com>         #
# SPDX-FileCopyrightText: © 2019, Rob Loach <robloach@gmail.com>               #
# SPDX-FileCopyrightText: © 2020, James Leaver <james@leaver.myzen.co.uk>      #
# SPDX-FileCopyrightText: © 2022, Jonathan McDowell <noodles@earth.li>         #
#                                                                              #
# SPDX-License-Identifier: CC-BY-4.0                                           #
# ############################################################################ #
# Exports a single file from the Monochrome theme to Ozone                     #
# ############################################################################ #
set -e

if [[ -z ${1} ]] || [[ -z ${2} ]]; then
  echo "Usage: ${BASH_SOURCE[0]} <srcfile> <destfile>"
  exit 1
fi

SRC="${1}"
DEST="${2}"
SRCNAME="$(basename "${1}")"

fletter="$(echo "${SRCNAME}" | head -c 1)"

# Sidebar console tabs should be 40x40px (start with uppercase and doesn't end
# with -content), others should be 46x46px. Some icons have overrides for
# size/color.
case ${SRCNAME} in
  battery-*.svg)
    typeset size="92"
    ;;
  clock.svg)
    typeset size="92"
    ;;
  dialog-slice.svg)
    typeset size="256"
    ;;
  key.svg)
    typeset size="256"
    ;;
  key-hover.svg)
    typeset size="256"
    ;;
  *-content.svg)
    typeset size="46"
    ;;
  *)
    if [[ ${fletter} == "${fletter,,}" ]]; then
      typeset size="46"
    else
      typeset size="40"
    fi
    ;;
esac

echo "Exporting ${SRC} to ${DEST} (resized to ${size}x${size}px)…"

inkscape -C -w "${size}" -h "${size}" -o "${DEST}" "${SRC}" 2>/dev/null
mogrify -background 'rgb(255,255,255)' -alpha Background "${DEST}"
optipng -quiet -o7 -strip all "${DEST}"

exit

# ############################################################################ #
# More work to be done for the files below                                     #
# ############################################################################ #

# Export auxiliary sidebar icons
typeset -ag SIDEBAR_SRC SIDEBAR_SRC_DIR
SIDEBAR_SRC=("add.svg"             # add.png
             "add-favorite.svg"    # favorites.png
             "history.svg"         # history.png
             "image.svg"           # image.png
             "music.svg"           # music.png
             "menu_quickmenu.svg"  # retroarch.png
             "setting.svg"         # settings.png
             "menu_video.svg"      # video.png
)

# Some icons are sourced from other themes…
SIDEBAR_SRC_DIR=(""            # add.png
                 ""            # favorites.png
                 "../flatui/"  # history.png
                 ""            # image.png
                 ""            # music.png
                 ""            # retroarch.png
                 "../flatui/"  # settings.png
                 ""            # video.png
)

typeset -ag SIDEBAR_DST SIDEBAR_DST_BORDER SIDEBAR_DST_SIZE
SIDEBAR_DST=("add.png" "favorites.png" "history.png" "image.png" "music.png"
  "retroarch.png" "settings.png" "video.png")
SIDEBAR_DST_BORDER=("3"  # add.png
                   "-3"  # favorites.png
                    "8"  # history.png
                    "0"  # image.png
                    "0"  # music.png
                   "-6"  # retroarch.png
                   "-6"  # settings.png
                    "0"  # video.png
)
SIDEBAR_DST_SIZE=("58"  # add.png
                  "70"  # favorites.png
                  "48"  # history.png
                  "64"  # image.png
                  "64"  # music.png
                  "76"  # retroarch.png
                  "76"  # settings.png
                  "64"  # video.png
)

mkdir -pv ../../../ozone/png/sidebar

for INDEX in $(seq 0 $((${#SIDEBAR_SRC[@]} - 1))); do
  src_dir="${SIDEBAR_SRC_DIR[$INDEX]}"
  src="${src_dir}${SIDEBAR_SRC[$INDEX]}"
  dest="${SIDEBAR_DST[$INDEX]}"
  size="${SIDEBAR_DST_SIZE[$INDEX]}"
  border_size="${SIDEBAR_DST_BORDER[$INDEX]}"

  inkscape -z -C -w "${size}" -h "${size}" -f "${src}" \
    -e "../../../ozone/png/sidebar/${dest}" 2>/dev/null

  # FlatUI icons are colored - change to monochrome
  if [[ ${src_dir} == "../flatui/" ]]; then
    # Mogrify crashes with this command, so we have to use convert…
    temp_file="${dest}.tmp.png"
    magick convert "../../../ozone/png/sidebar/${dest}" xc:"#ffffff" \
      -channel RGB -clut "../../../ozone/png/sidebar/${temp_file}"
    mv -fv "../../../ozone/png/sidebar/${temp_file}" \
      "../../../ozone/png/sidebar/${dest}"
  fi

  # Add or remove a border, if required.
  if [[ ${border_size} -gt 0 ]]; then
    magick mogrify -bordercolor transparent -border "${border_size}" \
      "../../../ozone/png/sidebar/${dest}"
  elif [[ ${border_size} -lt 0 ]]; then
    border_size="$((border_size * -1))"
    magick mogrify -shave "${border_size}" "../../../ozone/png/sidebar/${dest}"
  fi

  magick mogrify -background 'rgb(255,255,255)' -alpha Background \
    "../../../ozone/png/sidebar/${dest}"
  optipng -quiet -o7 -strip all "../../../ozone/png/sidebar/${dest}"
done

# Special case: netplay icon has no source, it exists only as a PNG file in the
# XMB pixel theme.
src="../../../xmb/pixel/png/room.png"
dest="netplay.png"

magick convert "${src}" xc:"#ffffff" -channel RGB \
  -clut "../../../ozone/png/sidebar/${dest}"
magick mogrify -resize 64x64 "../../../ozone/png/sidebar/${dest}"
magick mogrify -background 'rgb(255,255,255)' -alpha Background \
  "../../../ozone/png/sidebar/${dest}"
optipng -quiet -o7 -strip all "../../../ozone/png/sidebar/${dest}"

# Generate main RetroArch icon.
src="menu_quickmenu.svg"
dest="retroarch.png"
size="118"
border_size="11"

inkscape -z -C -w "${size}" -h "${size}" -f "${src}" \
  -e "../../../ozone/png/${dest}" 2>/dev/null
magick mogrify -shave "${border_size}" "../../../ozone/png/${dest}"
magick mogrify -background 'rgb(255,255,255)' -alpha Background \
  "../../../ozone/png/${dest}"
optipng -quiet -o7 -strip all "../../../ozone/png/${dest}"
