#!/bin/bash
#
# Template for AV Linux scripts based on the work of Andrew M. Taylor
#
# This is free and unencumbered software released into the public domain.
# Please read UNLICENSE for more details, or refer to <http://unlicense.org/>

# DECLARATION OF TEXT COLORS AND FORMATTING OPTIONS

# Text colours
declare -r BLACK='\e[0;30m'
declare -r RED='\e[0;31m'
declare -r GREEN='\e[0;32m'
declare -r BROWN='\e[0;33m'
declare -r BLUE='\e[0;34m'
declare -r PURPLE='\e[0;35m'
declare -r CYAN='\e[0;36m'
declare -r LIGHT_GREY='\e[0;37m'
declare -r DARK_GREY='\e[0;90m'
declare -r LIGHT_RED='\e[0;91m'
declare -r LIGHT_GREEN='\e[0;92m'
declare -r YELLOW='\e[0;93m'
declare -r LIGHT_BLUE='\e[0;94m'
declare -r LIGHT_PURPLE='\e[0;95m'
declare -r LIGHT_CYAN='\e[0;96m'
declare -r WHITE='\e[0;97m'

# Bold text
declare -r BOLD_DARK_GREY='\e[1;30m'
declare -r BOLD_LIGHT_RED='\e[1;31m'
declare -r BOLD_LIGHT_GREEN='\e[1;32m'
declare -r BOLD_YELLOW='\e[1;33m'
declare -r BOLD_LIGHT_BLUE='\e[1;34m'
declare -r BOLD_LIGHT_PURPLE='\e[1;35m'
declare -r BOLD_LIGHT_CYAN='\e[1;36m'
declare -r BOLD_WHITE='\e[1;37m'

# Underlined text
declare -r UNDERLINE_BLACK='\e[4;30m'
declare -r UNDERLINE_RED='\e[4;31m'
declare -r UNDERLINE_GREEN='\e[4;32m'
declare -r UNDERLINE_BROWN='\e[4;33m'
declare -r UNDERLINE_BLUE='\e[4;34m'
declare -r UNDERLINE_PURPLE='\e[4;35m'
declare -r UNDERLINE_CYAN='\e[4;36m'
declare -r UNDERLINE_LIGHT_GREY='\e[4;37m'

# Default text
declare -r DEFAULT_TEXT='\e[0m'

#INTERACTIVE FUNCTION TO ASK BEFORE EXECUTING

function ask_yes_or_no() {
    read -p "$1 ([y]es or [N]o): "
    case $(echo $REPLY | tr '[A-Z]' '[a-z]') in
        y|yes) echo "yes" ;;
        *)     echo "no" ;;
    esac
}


# FUNCTION TO SET TEXT COLOR AND STYLE
#
#   Usage: setTextStyle $RED
#          setTextStyle $UNDERLINE_GREEN
#
function setTextStyle() { echo -en "$1"; }

# FUNCTION TO SHOW RED ERROR MESSAGE
#
#   Usage: exitWithError "Something went wrong!"
#
function exitWithError() 
{
    setTextStyle $RED
    echo -e "\n$1\n"
    setTextStyle $DEFAULT_TEXT
    exit 1
}

###################################################################################
#                                                                                 #
# The script starts executing from here, anything beyond this point can be edited #
#                                                                                 #
###################################################################################

# CLEAR TERMINAL OF ANY TEXT

clear 

# DISPLAY ICON (TERMINOLOGY ONLY)
tycat /usr/local/share/icons/custom/AVL-FileActions.png

setTextStyle $BROWN
echo " Rebuild efreet Icon Cache!"
setTextStyle $DEFAULT_TEXT

# TEXT SECTION TO PRINT COLORED MESSAGE IN TERMINAL

setTextStyle $WHITE
cat << EndOfText

In rare cases Enlightenment's 'efreet' daemon may fail to show the correct Icon theme.

This utility will remove the existing 'efreet' Icon cache folder then it will
create a fresh efreet Icon cache and restart Enlightenment. Once it is complete
you may need to reselect your desired Icon theme from the Settings Panel-->Look
-->Application Theme-->Icons tab. On the next Log Out or Reboot the correct Icon
theme should appear on the system and be retained.


EndOfText
setTextStyle $DEFAULT_TEXT

sleep 10s

# COMMANDLINE TO BE RUN

setTextStyle $BROWN
echo "Removing your existing efreet cache..."
setTextStyle $DEFAULT_TEXT
sleep 3s
rm -rf ~/.cache/efreet

setTextStyle $BROWN
echo "Rebuilding your efreet Desktop File cache..."
setTextStyle $DEFAULT_TEXT
sleep 3s
/usr/lib/x86_64-linux-gnu/efreet/*/efreet_desktop_cache_create -d /usr/share/applications ~/.local/share/applications

setTextStyle $BROWN
echo "Rebuilding your efreet Icon cache (may take several seconds)..."
setTextStyle $DEFAULT_TEXT
sleep 3s
/usr/lib/x86_64-linux-gnu/efreet/*/efreet_icon_cache_create -d /usr/share/icons ~/.icons -e .png .svg .jpg .xpm

setTextStyle $BROWN
echo "Restarting your Enlightenment Session..."
setTextStyle $DEFAULT_TEXT
sleep 3s
enlightenment_remote -restart

# ECHO SUCCESSFUL COMPLETION MESSAGE

setTextStyle $BOLD_LIGHT_PURPLE
echo -n "Task Complete! Press Enter to Close Terminal."

read
