#!/bin/bash
#
# lxcursor 0.2
#
# based on pcursor 0.1 by wow <rockingfreak@gmail.com>
#
# Purpose: Change the default X cursor theme
# 
# by Neal <nealbrks0@gmail.com>
# pinoc: changed to work with zenity and new bash 4
# ghostbunny: cleaned up script
# ghostbunny: ported to yad, added preview, rearranges the code in some functions
# ghostbunny: 27.11.2011 added E17 support
# 

# i18n - Internationalization 
#====================================================
export TEXTDOMAIN=lxcursor
export TEXTDOMAINDIR="/usr/share/locale"


# some variables
#====================================================
TITLE=LXCursor
VERSION=0.41
ICON="/usr/share/icons/cursor_ring_icon.png"
YAD2="yad --title=$TITLE-$VERSION --window-icon=$ICON"
ABORT_CONDITION=1

# Function: getDesktopEnv
# The function returns the used desktop environment
# Used for compatibility with E17
getDesktopEnv()
{
  ENVVAR=$(env)

  if [ $(echo $ENVVAR | grep -c "LXDE") -gt 0 ]; then
    echo "LXDE"
  else
    if [ $(echo $ENVVAR | grep -c "E17") -gt 0 ]; then
      echo "E17"
    else
      echo "unknown"
    fi
  fi
}

# Function: create .desktop-files for iconbox
# function needs one parameter, it have to be a name of a cursor theme
#====================================================
create_dektop_files()
{
  rm -rf $HOME/.local/lxcursor/desktop_files/*

  #echo "$1"
  
  if [ -f $HOME/.local/lxcursor/cursorthemes/$1/circle_000.png ]; then
    echo "[Desktop Entry]" > $HOME/.local/lxcursor/desktop_files/circle.desktop
    echo "Name=circle" >> $HOME/.local/lxcursor/desktop_files/circle.desktop
    echo "Icon=$HOME/.local/lxcursor/cursorthemes/$1/circle_000.png" >> $HOME/.local/lxcursor/desktop_files/circle.desktop
    echo "Type=Application" >> $HOME/.local/lxcursor/desktop_files/circle.desktop
    echo "Exec=\#" >> $HOME/.local/lxcursor/desktop_files/circle.desktop
  fi

  if [ -f $HOME/.local/lxcursor/cursorthemes/$1/hand2_000.png ]; then
    echo "[Desktop Entry]" > $HOME/.local/lxcursor/desktop_files/hand2.desktop
    echo "Name=hand2" >> $HOME/.local/lxcursor/desktop_files/hand2.desktop
    echo "Icon=$HOME/.local/lxcursor/cursorthemes/$1/hand2_000.png" >> $HOME/.local/lxcursor/desktop_files/hand2.desktop
    echo "Type=Application" >> $HOME/.local/lxcursor/desktop_files/hand2.desktop
    echo "Exec=\#" >> $HOME/.local/lxcursor/desktop_files/hand2.desktop
  fi
  
  if [ -f $HOME/.local/lxcursor/cursorthemes/$1/arrow_000.png ]; then
    echo "[Desktop Entry]" > $HOME/.local/lxcursor/desktop_files/arrow.desktop
    echo "Name=arrow" >> $HOME/.local/lxcursor/desktop_files/arrow.desktop
    echo "Icon=$HOME/.local/lxcursor/cursorthemes/$theme/arrow_000.png" >> $HOME/.local/lxcursor/desktop_files/arrow.desktop
    echo "Type=Application" >> $HOME/.local/lxcursor/desktop_files/arrow.desktop
    echo "Exec=\#" >> $HOME/.local/lxcursor/desktop_files/arrow.desktop
  fi

  if [ -f $HOME/.local/lxcursor/cursorthemes/$1/move_000.png ]; then
    echo "[Desktop Entry]" > $HOME/.local/lxcursor/desktop_files/move.desktop
    echo "Name=move" >> $HOME/.local/lxcursor/desktop_files/move.desktop
    echo "Icon=$HOME/.local/lxcursor/cursorthemes/$1/move_000.png" >> $HOME/.local/lxcursor/desktop_files/move.desktop
    echo "Type=Application" >> $HOME/.local/lxcursor/desktop_files/move.desktop
    echo "Exec=\#" >> $HOME/.local/lxcursor/desktop_files/move.desktop
  fi

  if [ -f $HOME/.local/lxcursor/cursorthemes/$1/double_arrow_000.png ]; then
    echo "[Desktop Entry]" > $HOME/.local/lxcursor/desktop_files/double_arrow.desktop
    echo "Name=double_arrow" >> $HOME/.local/lxcursor/desktop_files/double_arrow.desktop
    echo "Icon=$HOME/.local/lxcursor/cursorthemes/$1/double_arrow_000.png" >> $HOME/.local/lxcursor/desktop_files/double_arrow.desktop
    echo "Type=Application" >> $HOME/.local/lxcursor/desktop_files/double_arrow.desktop
    echo "Exec=\#" >> $HOME/.local/lxcursor/desktop_files/double_arrow.desktop
  fi
}


# Function: create .png files from cursor files
# function needs one parameter, it have to be a name of a cursor theme
#====================================================
get_png_cursor()
{
  if [ ! -d $HOME/.local/lxcursor/cursorthemes/$1 ]; then
    mkdir $HOME/.local/lxcursor/cursorthemes/$1
    
    if [ -f /usr/share/icons/$1/cursors/circle ]; then
      xcur2png -q -d $HOME/.local/lxcursor/cursorthemes/$1 /usr/share/icons/$1/cursors/circle
    fi

    if [ -f /usr/share/icons/$1/cursors/move ]; then
      xcur2png -q -d $HOME/.local/lxcursor/cursorthemes/$1 /usr/share/icons/$1/cursors/move
    fi

    if [ -f /usr/share/icons/$1/cursors/double_arrow ]; then
      xcur2png -q -d $HOME/.local/lxcursor/cursorthemes/$1 /usr/share/icons/$1/cursors/double_arrow
    fi

    if [ -f /usr/share/icons/$1/cursors/arrow ]; then
      xcur2png -q -d $HOME/.local/lxcursor/cursorthemes/$1 /usr/share/icons/$1/cursors/arrow
    fi

    if [ -f /usr/share/icons/$1/cursors/hand2 ]; then
      xcur2png -q -d $HOME/.local/lxcursor/cursorthemes/$1 /usr/share/icons/$1/cursors/hand2
    fi

    rm -f $HOME/.local/lxcursor/cursorthemes/$1/*_001.png
    rm -f $HOME/.local/lxcursor/cursorthemes/$1/*_002.png
    rm -f $HOME/.local/lxcursor/cursorthemes/$1/*_003.png
    rm -f $HOME/.local/lxcursor/cursorthemes/$1/*_004.png
    rm -f $HOME/.local/lxcursor/cursorthemes/$1/*_005.png
    rm -f $HOME/.local/lxcursor/cursorthemes/$1/*_006.png
    rm -f $HOME/.local/lxcursor/cursorthemes/$1/*_007.png
  fi
}


# Function: preview of the cursors
#====================================================
preview()
{ 
  # display iconbox
  #----------------------------------------------------
  $YAD2 --text=$"<b>Preview</b>" --icons --read-dir=$HOME/.local/lxcursor/desktop_files --button=$"Back":3 --button=$"Ok":0 --button=$"Cancel":1
  USER_CHOICE="$?" 

  # echo/return the result
  #----------------------------------------------------
  echo "$USER_CHOICE"
}


# Function: get choice from themes list
#====================================================
get_selected_theme()
{
  # create icon-theme list
  #----------------------------------------------------
  CURSORLIST=`ls -d /usr/share/icons/*/cursors | cut -d/ -f5`


  # current theme
  #----------------------------------------------------
  ctheme=$(get_current_theme)


  # remove the theme which is in use from list
  #----------------------------------------------------
  CURSORLIST=${CURSORLIST/$ctheme/}


  # display list dialog
  #----------------------------------------------------
  if [ "$ctheme" == "" ]; then
    SELECTED_THEME=$($YAD2 --text=$" Please select the new cursor theme by double clicking or OK \n or press Cancel to quit.\n Note: The following preview pictures do not have the normal cursor size." --list --height 300 --column=$"Cursor Theme" --button=$"Ok":0 --button=$"Cancel":1 $CURSORLIST)
  else
    SELECTED_THEME=$($YAD2 --text=$" Please select the new cursor theme by double clicking or OK \n or press Cancel to quit.\n Note: The following preview pictures do not have the normal cursor size." --list --height 300 --column=$"Cursor Theme" --button=$"Ok":0 --button=$"Cancel":1 $"$ctheme (current Theme)" $CURSORLIST)
  fi
    
  return_code=$?
    
  if [[ "$return_code" == "1" ]] || [[ "$return_code" == "252" ]]; then
    SELECTED_THEME=$return_code
  fi

  # echo/return the result
  #----------------------------------------------------
  echo "$SELECTED_THEME"
}


# Function: test config directory
#====================================================
test_config_directory()
{
  if [ ! -d $HOME/.local/lxcursor ]; then
    mkdir $HOME/.local/lxcursor
    mkdir $HOME/.local/lxcursor/cursorthemes
    mkdir $HOME/.local/lxcursor/desktop_files
  else
    if [ ! -d $HOME/.local/lxcursor/cursorthemes ]; then
      mkdir $HOME/.local/lxcursor/cursorthemes
    fi

    if [ ! -d $HOME/.local/lxcursor/desktop_files ]; then
      mkdir $HOME/.local/lxcursor/desktop_files
    fi
  fi
}


# Function: restart some services to accept the theme in the running session
#====================================================
restart_services()
{ 
 if [ $(getDesktopEnv) == "LXDE" ]; then
    lxsession -r
    openbox --restart

   # Old code
   #----------------------
   # pcmanfm --desktop-off
   # pcmanfm --desktop
   # lxpanelctl restart

 else
    if [ $(getDesktopEnv) == "E17" ]; then
      enlightenment_remote -restart&
    fi
  fi
}


# Function: get current cursor theme
#====================================================
get_current_theme()
{
  currentTheme=""

  if [ $(getDesktopEnv) == "LXDE" ]; then
    currentTheme=$(grep "sGtk\/CursorThemeName=" $HOME/.config/lxsession/LXDE/desktop.conf)
    currentTheme=${currentTheme#sGtk\/CursorThemeName\=}
  else
    if [ -f ~/.local/lxcursor/ctheme ]; then
      currentTheme=$(cat ~/.local/lxcursor/ctheme)
    else
      currentTheme=""
    fi
  fi
    
  # echo/return the result
  #----------------------------------------------------
  echo "$currentTheme"
}





# running main part of the script, ask user, apply theme or send error message
#================================================================================================================================
#================================================================================================================================

# help variables
#----------------------------------------------------
ctheme=$(get_current_theme)
help=$"$ctheme (current Theme)"
preview_check=2

test_config_directory

if [ $(getDesktopEnv) == "unknown" ]; then
  YAD --text=$"Your desktop environment isn't supported. Aborting..."
  exit 1
else
  while [ "$ABORT_CONDITION" != "0" ]
  do
    NEW_THEME=$(get_selected_theme)
    NEW_THEME=${NEW_THEME%|}
    #echo "$NEW_THEME"
  
    if [[ "$NEW_THEME" == "1" ]] || [[ "$NEW_THEME" == "252" ]]; then
      ABORT_CONDITION=0
    else
      if [ "$NEW_THEME" == "$help" ]; then
        $YAD2 --text=$"The selected theme is the current theme.\nNothing will change." --button=$"Ok":0 --button=$"Cancel":1
        ABORT_CONDITION=0
      else
        get_png_cursor $NEW_THEME
        create_dektop_files $NEW_THEME
        preview_check=$(preview)
      

        if [[ "$preview_check" == "1" ]] || [[ "$preview_check" == "252" ]]; then
          ABORT_CONDITION=0
        else
          if [ "$preview_check" == "0" ]; then
            ABORT_CONDITION=0
          
            # apply the new cursor theme
            if [ -d $HOME/.icons/default ]; then 
              rm -rf $HOME/.icons/default
            fi

            if [ ! -d $HOME/.icons ]; then 
              mkdir -p $HOME/.icons/default
            fi
  
            mkdir -p $HOME/.icons/default
            ln -fs /usr/share/icons/$NEW_THEME/cursors $HOME/.icons/default

            if [ $(getDesktopEnv) == "LXDE" ]; then    
              if [ $(grep -c -i "$NEW_THEME" $HOME/.config/lxsession/LXDE/desktop.conf) -eq 0 ]; then
                perl -pi -e "s/$ctheme/$NEW_THEME/g" $HOME/.config/lxsession/LXDE/desktop.conf
              fi
            else
              echo $NEW_THEME > ~/.local/lxcursor/ctheme
            fi

            restart_services
          fi
        fi
      fi
    fi
  done
fi

exit 0
