#!/bin/bash
#
# ezswitch 0.4
#
# Purpose: Change the plymouth boot theme
# 
# by Neal <nealbrks0@gmail.com> with help from Travisn000, Torsten (tschommer), Leiche and Texstar
# 
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#


# verbose output for debugging
#set -xv; 

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

# define some variables
#=================================================
TITLE="EZSwitch"
VERSION="0.4"
TMP_DIR=$HOME/tmp/ezswitch_gui
ICON="/usr/share/icons/ezswitch.png"


# test for temp folder
#=================================================
if [ ! -d $TMP_DIR ]; then mkdir -p $TMP_DIR; fi

function exit_clean {
  rm -fR $TMP_DIR
  exit $1
}


# Check if kdialog exists, if not, use gdialog.
#=================================================
if [ -f /usr/bin/kdialog ]; then
    DIALOG=kdialog
    QUESTION="yesno"
else
    DIALOG=gdialog
    QUESTION="yesno"
fi


# test for zenity
#=================================================
if which zenity >/dev/null; then
  DIALOG=`which zenity`
  QUESTION="question"
else 
  $DIALOG --$QUESTION $"Zenity is required to run this script.\n\nWould you like to install it now?"
  if [ "$?" == "0" ]; then
    synaptic &
    exit_clean 1
  elif [ "$?" == "1" ]; then
    $DIALOG --sorry $"Exiting now"
    exit_clean 1
  fi
fi


# Check if user is logged in as root
#=================================================
if [ "$UID" != "0" ]; then
    $DIALOG --error --title="$TITLE" --text=$"Please run this script as root!"
    exit_clean 1
fi



# welcome screen
#=================================================
echo -e "\nPrevious theme - `grep -i "^Theme=" /etc/plymouth/plymouthd.conf | cut -d= -f2`"

MSG=$"This program will change your plymouth bootsplash theme to the theme of your choice\!\n\nDo you wish to continue with the theme switch\?"
$DIALOG --$QUESTION --width 400 --title="$TITLE $VERSION" --text="$MSG" --window-icon=$ICON

if [ $? = 1 ];
then exit_clean 1
fi


function change_theme(){

    # create plymouth-theme list for zenity dialog
    #=================================================
    #ZEN_LIST=`ls /usr/share/plymouth/themes/ | cut -d"/" -f1 | awk '{print "FALSE " $1}'`
    #SELECTED_THEME=$( $DIALOG --title $"Select a Plymouth Theme" --text $"Please select a plymouth theme or press Cancel to quit.\nNOTE: you must reboot to apply the new plymouth theme\!" --list --height 300 --radiolist --column $"Select" --column $"Plymouth Theme" $ZEN_LIST )

    ZEN_LIST=""
    for plym_theme in `ls /usr/share/plymouth/themes/ | cut -d"/" -f1 | awk '{print $1}'`; do
      theme_description=`grep -i "^description=" /usr/share/plymouth/themes/$plym_theme/*.plymouth |cut -d= -f2` 
      # make cleanly formatted zenity list..  no extra line breaks
      if [ "$ZEN_LIST" == "" ]; then
	ZEN_LIST+="FALSE \n$plym_theme \n$theme_description "
      else
	ZEN_LIST+="\nFALSE \n$plym_theme \n$theme_description "
      fi
    done

#     echo -e $ZEN_LIST
    SELECTED_THEME=$( echo -e $ZEN_LIST | $DIALOG --title $"Select a Plymouth Theme" --text $"Please select a plymouth theme or press Cancel to quit.\nNOTE: you must reboot to apply the new plymouth theme\!" --list --height 300 --width 600 --radiolist --column $"Select" --column $"Plymouth Theme" --column $"Theme Description" --window-icon=$ICON )

    # check if a new theme was selected and if so, apply it
    #=================================================
    if [ "$?" != "0" ]; then  # just quit
      $DIALOG --title "$TITLE" --info --text=$"Plymouth theme unchanged." --window-icon=$ICON
      exit_clean 0
    fi

    echo -e "Selected theme - $SELECTED_THEME \n\n"

    if [ $SELECTED_THEME == `grep -i "^Theme=" /etc/plymouth/plymouthd.conf | cut -d= -f2` ]; then  # check if already set as theme
      $DIALOG --$QUESTION --width=300 --title "$TITLE $VERSION" --text=$"Plymouth theme unchanged.  \n\nWould you like to preview your current theme?" --window-icon=$ICON
      if [ "$?" == "0" ]; then 
	plymouthd #--mode=shutdown
	plymouth --show-splash
	for ((i=0; i<10; i++)); do 
	  PROGRESS=$(expr $i \* 1 )
	  plymouth --update=test$PROGRESS
	  sleep 1
	done
	plymouth quit
      fi
      $DIALOG --$QUESTION --width=300 --title "$TITLE $VERSION" --window-icon=$ICON \
	--cancel-label=$"Change Theme" --ok-label=$"Exit" \
	--text=$"Select 'Exit' below to keep your selected theme, or \n'Change Theme' if you would like to run $TITLE again."
      if [ "$?" == "1" ]; then change_theme; else exit_clean 0; fi

    else  # apply the new plymouth theme
      #(echo && /usr/sbin/plymouth-set-default-theme -R $SELECTED_THEME) | $DIALOG --progress --pulsate --auto-close --width=300 --title "$TITLE" --text=$"Updating plymouth theme, please wait.. \n(This may take a while)" --window-icon=$ICON
      (echo && remove-theme >/dev/null && switch-themes $SELECTED_THEME >/dev/null ) | $DIALOG --progress --pulsate --auto-close --width=300 --title "$TITLE" --text=$"Updating plymouth theme, please wait.. \n(This may take a while)" --window-icon=$ICON 
    fi

    # check if sucessfully set as theme
    #=================================================
    if [ $SELECTED_THEME == `grep -i "^Theme=" /etc/plymouth/plymouthd.conf | cut -d= -f2` ]; then  
      $DIALOG --$QUESTION --width=300 --title "$TITLE $VERSION" --text=$"Plymouth theme was updated successfully.  \n\nWould you like to preview your selection?" --window-icon=$ICON
      if [ "$?" == "0" ]; then 
	plymouthd #--mode=
	plymouth --show-splash
	for ((i=0; i<100; i++)); do 
	  PROGRESS=$(expr $i \* 1 )
	  plymouth --update=test$PROGRESS ;
	  sleep 0.1
	done
	plymouth quit
      fi
    else
      $DIALOG --$QUESTION --width=300 --title "$TITLE $VERSION" --text=$"Plymouth theme unchanged.  \n\nWould you like to preview your current theme?" --window-icon=$ICON
      if [ "$?" == "0" ]; then 
	plymouthd #--mode=
	plymouth --show-splash
	for ((i=10; i>0; i--)); do 
	  PROGRESS=$(expr $i \* 1 )
	  plymouth --update=test$PROGRESS
	  sleep 1
	done
	plymouth quit
      fi
    fi

    $DIALOG --$QUESTION --width=300 --title "$TITLE $VERSION" --window-icon=$ICON \
      --cancel-label=$"Change Theme" --ok-label=$"Exit" \
      --text=$"Select 'Exit' below to keep your selected theme, or \n'Change Theme' if you would like to run $TITLE again."
    if [ "$?" == "1" ]; then change_theme; else exit_clean 0; fi
}

change_theme
exit_clean 0


# plymouth --show-splash; for ((i=0; i<10; i++)); do PROGRESS=$(expr $i \* 1 ); plymouth --update=test$PROGRESS; sleep 1; done; plymouth quit
