#!/bin/bash
  # Copying files

ROOT_UID=0
DEST_DIR=

# Destination directory
if [ "$UID" -eq "$ROOT_UID" ]; then
  DEST_DIR="/usr/share/themes"
else
  DEST_DIR="$HOME/.themes"
fi

REO_DIR=$(cd $(dirname $0) && pwd)
SRC_DIR=${REO_DIR}/src

THEME_NAME=Matcha
COLOR_VARIANTS=('' '-dark')
THEME_VARIANTS=('-aliz' '-azul' '-sea')

usage() {
  printf "%s\n" "Usage: $0 [OPTIONS...]"
  printf "\n%s\n" "OPTIONS:"
  printf "  %-25s%s\n" "-d, --dest DIR" "Specify theme destination directory (Default: ${DEST_DIR})"
  printf "  %-25s%s\n" "-n, --name NAME" "Specify theme name (Default: ${THEME_NAME})"
  printf "  %-25s%s\n" "-c, --color VARIANTS" "Specify theme color variant(s) [standard|dark] (Default: All variants)"
  printf "  %-25s%s\n" "-t, --theme VARIANTS" "Specify hue theme variant(s) [aliz|azul|sea] (Default: All variants)"
  printf "  %-25s%s\n" "-h, --help" "Show this help"
}

# Copying files
install() {
  local dest=${1}
  local name=${2}
  local color=${3}
  local theme=${4}

  local themedir=${dest}/${name}${color}${theme}

  [[ -d ${themedir} ]] && rm -rf ${themedir}

  echo "Installing '${themedir}'..."
  mkdir -p                                                                            ${themedir}

  # Install index.theme
  echo "[Desktop Entry]"                                                           >> ${themedir}/index.theme
  echo "Type=X-GNOME-Metatheme"                                                    >> ${themedir}/index.theme
  echo "Name=${name}${color}${theme}"                                              >> ${themedir}/index.theme
  echo "Comment=A dark modern design theme"                                        >> ${themedir}/index.theme
  echo "Encoding=UTF-8"                                                            >> ${themedir}/index.theme
  echo ""                                                                          >> ${themedir}/index.theme
  echo "[X-GNOME-Metatheme]"                                                       >> ${themedir}/index.theme
  echo "GtkTheme=${name}${color}${theme}"                                          >> ${themedir}/index.theme
  echo "MetacityTheme=${name}${color}${theme}"                                     >> ${themedir}/index.theme
  echo "IconTheme=${name}"                                                         >> ${themedir}/index.theme
  echo "CursorTheme=Breeze_cursors"                                                >> ${themedir}/index.theme
  echo "ButtonLayout=menu:minimize,maximize,close"                                 >> ${themedir}/index.theme

  # Install GNOME Shell Theme
  mkdir -p                                                                            ${themedir}/gnome-shell
  cd ${SRC_DIR}/gnome-shell
  cp -ur extensions                                                                   ${themedir}/gnome-shell
  cp -ur gnome-shell${color}${theme}.css                                              ${themedir}/gnome-shell/gnome-shell.css
  cp -ur common-assets                                                                ${themedir}/gnome-shell/assets

  cd ${SRC_DIR}/gnome-shell/assets
  cp -ur no-events${color}.svg                                                        ${themedir}/gnome-shell/assets/no-events.svg
  cp -ur no-notifications${color}.svg                                                 ${themedir}/gnome-shell/assets/no-notifications.svg
  cp -ur calendar-arrow-left${color}.svg                                              ${themedir}/gnome-shell/assets/calendar-arrow-left.svg
  cp -ur calendar-arrow-right${color}.svg                                             ${themedir}/gnome-shell/assets/calendar-arrow-right.svg
  cp -ur key-hide${color}.svg                                                         ${themedir}/gnome-shell/assets/key-hide.svg
  cp -ur key-layout${color}.svg                                                       ${themedir}/gnome-shell/assets/key-layout.svg
  cp -ur key-shift${color}.svg                                                        ${themedir}/gnome-shell/assets/key-shift.svg
  [[ ${color} == '' ]] && \
  cp -ur menu.svg                                                                     ${themedir}/gnome-shell/assets
  cp -ur submenu${color}.svg                                                          ${themedir}/gnome-shell/assets/submenu.svg
  cp -ur submenu-open${color}.svg                                                     ${themedir}/gnome-shell/assets/submenu-open.svg

  cd ${SRC_DIR}/gnome-shell/theme-assets
  cp -ur checkbox${theme}.svg                                                         ${themedir}/gnome-shell/assets/checkbox.svg
  [[ ${color} == '-dark' ]] && \
  cp -ur menu${color}${theme}.svg                                                     ${themedir}/gnome-shell/assets/menu.svg
  cp -ur menu-hover${theme}.svg                                                       ${themedir}/gnome-shell/assets/menu-hover.svg
  cp -ur more-results${theme}.svg                                                     ${themedir}/gnome-shell/assets/more-results.svg
  cp -ur toggle-on${theme}.svg                                                        ${themedir}/gnome-shell/assets/toggle-on.svg

  cd ${themedir}/gnome-shell
  ln -s assets/no-events.svg no-events.svg
  ln -s assets/process-working.svg process-working.svg
  ln -s assets/no-notifications.svg no-notifications.svg

  # Install GTK+ 2 Theme
  mkdir -p                                                                            ${themedir}/gtk-2.0
  cd ${SRC_DIR}/gtk-2.0
  cp -ur {apps.rc,main.rc,panel.rc,xfce-notify.rc}                                    ${themedir}/gtk-2.0
  cp -ur gtkrc${color}${theme}                                                        ${themedir}/gtk-2.0/gtkrc
  cp -ur assets${color}${theme}                                                       ${themedir}/gtk-2.0/assets
  cp -ur menubar-toolbar${color}.rc                                                   ${themedir}/gtk-2.0/menubar-toolbar.rc

  # Install GTK+ 3 Theme
  mkdir -p                                                                            ${themedir}/gtk-3.0
  cd ${SRC_DIR}/gtk-3.0
  cp -ur assets${theme}                                                               ${themedir}/gtk-3.0/assets
  cp -ur gtk${color}${theme}.css                                                      ${themedir}/gtk-3.0/gtk.css
  cp -ur gtk-dark${theme}.css                                                         ${themedir}/gtk-3.0/gtk-dark.css

  cp -ur thumbnail${color}${theme}.png                                                ${themedir}/gtk-3.0/thumbnail.png

  # Install CINNAMON Theme
  mkdir -p                                                                            ${themedir}/cinnamon
  cd ${SRC_DIR}/cinnamon
  cp -ur cinnamon${color}${theme}.css                                                 ${themedir}/cinnamon/cinnamon.css
  cp -ur thumbnail${color}${theme}.png                                                ${themedir}/cinnamon/thumbnail.png

  cd ${SRC_DIR}/cinnamon/assets${theme}
  cp -ur common-assets                                                                ${themedir}/cinnamon
  cp -ur assets${color}                                                               ${themedir}/cinnamon/assets

  # Install Metacity Theme
  mkdir -p                                                                            ${themedir}/metacity-1
  cd ${SRC_DIR}/metacity-1
  cp -ur {thumbnail.png,*.svg,metacity-theme-3.xml}                                   ${themedir}/metacity-1
  cp -ur metacity-theme-1${theme}.xml                                                 ${themedir}/metacity-1/metacity-theme-1.xml

  cd ${themedir}/metacity-1
  ln -s metacity-theme-1.xml metacity-theme-2.xml

  # Install xfwm4 Theme
  mkdir -p                                                                            ${themedir}/xfwm4
  cd ${SRC_DIR}/xfwm4
  cp -ur assets${color}${theme}/*.png                                                 ${themedir}/xfwm4
  cp -ur themerc${theme}                                                              ${themedir}/xfwm4/themerc

  # Install openbox Theme
  mkdir -p                                                                            ${themedir}/openbox-3
  cd ${SRC_DIR}/openbox-3
  cp -ur *.xbm                                                                        ${themedir}/openbox-3
  cp -ur themerc${color}${theme}                                                      ${themedir}/openbox-3/themerc

  # Install Unity Theme
  mkdir -p                                                                            ${themedir}/unity
  cd ${SRC_DIR}
  cp -ur unity                                                                        ${themedir}
}

#  Install theme
install_theme() {
for color in "${colors[@]:-${COLOR_VARIANTS[@]}}"; do
  for theme in "${themes[@]:-${THEME_VARIANTS[@]}}"; do
    install "${dest:-${DEST_DIR}}" "${name:-${THEME_NAME}}" "${color}" "${theme}"
  done
done
}

while [[ $# -gt 0 ]]; do
  case "${1}" in
    -d|--dest)
      dest="${2}"
      if [[ ! -d "${dest}" ]]; then
        echo "ERROR: Destination directory does not exist."
        exit 1
      fi
      shift 2
      ;;
    -n|--name)
      name="${2}"
      shift 2
      ;;
    -t|--theme)
      shift
      for theme in "${@}"; do
        case "${theme}" in
          aliz)
            themes+=("${THEME_VARIANTS[0]}")
            shift 1
            ;;
          azul)
            themes+=("${THEME_VARIANTS[1]}")
            shift 1
            ;;
          sea)
            themes+=("${THEME_VARIANTS[2]}")
            shift 1
            ;;
          -*|--*)
            break
            ;;
          *)
            echo "ERROR: Unrecognized thin variant '$1'."
            echo "Try '$0 --help' for more information."
            exit 1
            ;;
        esac
      done
      ;;
    -c|--color)
      shift
      for color in "${@}"; do
        case "${color}" in
          standard)
            colors+=("${COLOR_VARIANTS[0]}")
            shift 1
            ;;
          dark)
            colors+=("${COLOR_VARIANTS[1]}")
            shift 1
            ;;
          -*|--*)
            break
            ;;
          *)
            echo "ERROR: Unrecognized color variant '$1'."
            echo "Try '$0 --help' for more information."
            exit 1
            ;;
        esac
      done
      ;;
    -h|--help)
      usage
      exit 0
      ;;
    *)
      echo "ERROR: Unrecognized installation option '$1'."
      echo "Try '$0 --help' for more information."
      exit 1
      ;;
  esac
done

install_theme

echo "Finished!..."
