#!/bin/bash

# Public Domain
# Israel Dahl 2015

shopt -s globstar
THEME_TO_USE="hicolor"
SIZE_TO_USE="32"
FILENAME=false
EXPORT_FILE="${HOME}"/icon_include
usage(){
echo "
${0##*/} [-h] [-s SIZE] [-t THEME] [-j]  [ -f EXPORT_FILENAME ]
  -h 			display this help
  -s SIZE 		SIZE of icons to use
  -t THEME		icon THEME to import
  -j 			put it into the jwmrc file instead of a separate file
  -f EXPORT_FILENAME	use EXPORT_FILENAME instead of the default: ($EXPORT_FILE)


Example (Default values):

   ${0##*/} -s $SIZE_TO_USE -t $THEME_TO_USE
"
exit
}

OPTIND=1
## the colon here denotes $OPTARG
# h AND j do not take args
# s AND t do
while getopts hjs:t:f: opt
do
  case $opt in
  h)
      usage
      ;;
  s)
      SIZE_TO_USE=$OPTARG
      ;;
  t)
      THEME_TO_USE=$OPTARG
      ;;
  j)
      FILENAME=true
      ;;
  f)
      EXPORT_FILE=$OPTARG
      ;;
  esac
done
shift $((OPTIND - 1))

## Clear the OLD file
BASE_THEME_DIR="/usr/share/pixmaps"
ICON_WRITE="  <IconPath>${BASE_THEME_DIR}</IconPath>"

if [ -z "${XDG_DATA_DIRS}" ]
then
  XDG_DATA_DIRS=/usr/local/share/:/usr/share/
fi

THEME_DIR_XDG=(${XDG_DATA_DIRS//:// })
export THEME_DIR_XDG=($(echo ${THEME_DIR_XDG[*]} |sed 's|$| |' |sed 's|/ |/icons |g' | sed 's://:/:'))

### FUNCTIONS
searchthemes(){ #THEME_TO_INHERIT
INHERIT_ICON_WRITE="<!-- Inherited Icons -->"
for i in "${THEME_DIR_XDG[@]}"
do ## for i in "${THEME_DIR_XDG[@]}"
 THEME_DIR="$i"
  if [ -d "${THEME_DIR}/${THEME_TO_INHERIT}" ]
  then
    echo "Searching ${THEME_DIR}"
    for THEME_FILE in "${THEME_DIR}"/${THEME_TO_INHERIT}/**.theme
    do ## theme file loops
      HEADER=""
      INHERITS=""
      DIRS=""
      MIN=""
      MAX=""
      SIZE=""
      TYPE=""
      THRESH=""
      CONTEXT=""
      while read LINE || [ "$LINE" ]
      do ## read LINE
        case $LINE in
          [*)
            HEADER="${LINE#[}"''
            HEADER="${HEADER%]}"'';;
          Inherits=*)
            INHERITS="${LINE#*=}"'';;
          Directories=*)
            DIRS="${LINE#*=}"'';;
          MinSize=*)
            MIN="${LINE#*=}"'';;
          MaxSize=*)
            MAX="${LINE#*=}"'';;
          Size=*)
            SIZE="${LINE#*=}"'';;
          Type=*)
            TYPE="${LINE#*=}"'';;
          Threshold=*)
            THRESH="${LINE#*=}"'';;
          Context=*)
            CONTEXT="${LINE#*=}"'';;
          *) ;;
        esac ## $LINE in

        ICON_PATH="${HEADER}"
        if [ "${CONTEXT}" != "Animations" ] && [ "${CONTEXT}" != "MimeTypes" ] && [ "${CONTEXT}" != "Stock" ] && [ "${CONTEXT}" != "Status" ] && [ "${CONTEXT}" != "Emblems" ]&& [ "${CONTEXT}" != "Emotes" ] && [ "${CONTEXT}" != "International" ] 
        then
          if [ "${SIZE}" == "${SIZE_TO_USE}" ] || [ $(( THRESH + SIZE )) == $(( SIZE_TO_USE )) ] || [ $(( THRESH - SIZE  )) == $(( SIZE_TO_USE )) ]
          then
             ## create some cool include file
            if [ "${ICON_PATH//$SIZE/}" != "${ICON_PATH}" ]
            then
                if [ -d "${THEME_DIR}/${THEME_TO_INHERIT}/$ICON_PATH" ] && [ "$(ls -A "${THEME_DIR}/${THEME_TO_INHERIT}/$ICON_PATH")" ]
                then
                  INHERIT_ICON_WRITE="${INHERIT_ICON_WRITE}
  <IconPath>${THEME_DIR}/${THEME_TO_INHERIT}/$ICON_PATH</IconPath>"
                fi
            fi ## i//SIZE/
          else
            if [ "${TYPE}" == "Scalablex" ]
            then
              if [ $(( MAX >= SIZE )) ] && [ $(( MIN <= SIZE )) ]
              then
                if [ -d "${THEME_DIR}/${THEME_TO_INHERIT}/$ICON_PATH" ] && [ "$(ls -A "${THEME_DIR}/${THEME_TO_INHERIT}/$ICON_PATH")" ]
                then
                  INHERIT_ICON_WRITE="${INHERIT_ICON_WRITE}
  <IconPath>${THEME_DIR}/${THEME_TO_INHERIT}/$ICON_PATH</IconPath>"
                fi
              fi ## MAX > Size
            fi  ### Scalable
          fi ## "${SIZE}" == "${SIZE_TO_USE}"
        fi ## CONTEXT CHECKER
      done < "$THEME_FILE"   ## read LINE
    done ## theme file loops
  fi # If directory exists
done ## for i in "${THEME_DIR_XDG[@]}"
} ## searchthemes()


mainLOOP(){
for i in "${THEME_DIR_XDG[@]}"
do ## for i in "${THEME_DIR_XDG[@]}"
 THEME_DIR="$i"
  if [ -d "${THEME_DIR}/${THEME_TO_USE}" ]
  then
    echo "Searching ${THEME_DIR}"
    for THEME_FILE in "${THEME_DIR}"/${THEME_TO_USE}/**.theme
    do ## theme file loops
      HEADER=""
      INHERITS=""
      DIRS=""
      MIN=""
      MAX=""
      SIZE=""
      TYPE=""
      THRESH=""
      CONTEXT=""
      while read LINE || [ "$LINE" ]
      do ## read LINE
        case $LINE in
          [*)
            HEADER="${LINE#[}"''
            HEADER="${HEADER%]}"'';;
          Inherits=*)
            INHERITS="${LINE#*=}"'';;
          Directories=*)
            DIRS="${LINE#*=}"'';;
          MinSize=*)
            MIN="${LINE#*=}"'';;
          MaxSize=*)
            MAX="${LINE#*=}"'';;
          Size=*)
            SIZE="${LINE#*=}"'';;
          Type=*)
            TYPE="${LINE#*=}"'';;
          Threshold=*)
            THRESH="${LINE#*=}"'';;
          Context=*)
            CONTEXT="${LINE#*=}"'';;
          *) ;;
        esac ## $LINE in

        ICON_PATH="${HEADER}"
        if [ "${CONTEXT}" != "Animations" ] && [ "${CONTEXT}" != "MimeTypes" ] && [ "${CONTEXT}" != "Stock" ] && [ "${CONTEXT}" != "Status" ] && [ "${CONTEXT}" != "Emblems" ] && [ "${CONTEXT}" != "Emotes" ]  && [ "${CONTEXT}" != "International" ] 
        then
          if [ "${SIZE}" == "${SIZE_TO_USE}" ] || [ $(( THRESH + SIZE )) == $(( SIZE_TO_USE )) ] || [ $(( THRESH - SIZE  )) == $(( SIZE_TO_USE )) ]
          then
            ## create some cool include file
            if [ "${ICON_PATH//$SIZE/}" != "${ICON_PATH}" ]
            then
                if [ -d "${THEME_DIR}/${THEME_TO_USE}/$ICON_PATH" ] && [ "$(ls -A "${THEME_DIR}/${THEME_TO_USE}/$ICON_PATH")" ]
                then
                  ICON_WRITE="${ICON_WRITE}
  <IconPath>${THEME_DIR}/${THEME_TO_USE}/$ICON_PATH</IconPath>"
                fi
            fi ## i//SIZE/
          else
            if [ "${TYPE}" == "Scalablex" ]
            then
              if [ $(( MAX >= SIZE )) ] && [ $(( MIN <= SIZE )) ]
              then
                if [ -d "${THEME_DIR}/${THEME_TO_USE}/$ICON_PATH" ] && [ "$(ls -A "${THEME_DIR}/${THEME_TO_USE}/$ICON_PATH")" ]
                then
                  ICON_WRITE="${ICON_WRITE}
  <IconPath>${THEME_DIR}/${THEME_TO_USE}/$ICON_PATH</IconPath>"
                fi
              fi ## MAX > Size
            fi  ### Scalable
          fi ## "${SIZE}" == "${SIZE_TO_USE}"
        if [ ! -z "${INHERITS}" ]
        then
          INHERIT_ARRAY=($(echo "${INHERITS//,/ }" | sort -u))
          for i in "${INHERIT_ARRAY[@]}"
          do
            export THEME_TO_INHERIT="${i}"
            echo "Inheriting: $THEME_TO_INHERIT"
            searchthemes
          done
        fi
      fi
      done < "$THEME_FILE"   ## read LINE
    done ## theme file loops
  fi # If directory exists
done ## for i in "${THEME_DIR_XDG[@]}"
}
HOME_THEME_DIR="${HOME}/.icons"
if [ -d "${HOME_THEME_DIR}" ]
then
  echo "user icons directory found"
  ## AFAIK this *should* work
  THEME_DIR_XDG="${HOME_THEME_DIR}"
  mainLOOP

  THEME_DIR_XDG=(${XDG_DATA_DIRS//:// })
  export THEME_DIR_XDG=($(echo ${THEME_DIR_XDG[*]} |sed 's|$| |' |sed 's|/ |/icons |g' | sed 's://:/:'))
fi ## ~/.icons

mainLOOP
if [ "${FILENAME}" != true ]
then
  echo "<JWM>">"${EXPORT_FILE}"
  echo "${ICON_WRITE}
${INHERIT_ICON_WRITE}" >> "${EXPORT_FILE}"
  echo "</JWM>" >>  "${EXPORT_FILE}"
else
  JWM_FILE="${HOME}"/.jwmrc
  sed -i "/<IconPath>/ d" "${JWM_FILE}"
  sed -i "/<!-- ICONS -->/ d" "${JWM_FILE}"
  sed -i "/<!-- Inherited Icons -->/ d" "${JWM_FILE}"
  sed  -i "/<\/JWM>/ d" "${JWM_FILE}"
  echo "<!-- ICONS -->
${ICON_WRITE}
${INHERIT_ICON_WRITE}
</JWM>" >>"${JWM_FILE}"

fi
