#!/bin/bash #Company: PowerCraft Technology #Author: Copyright Jelle de Jong #Note: Please send me an email if you enhanced this script #Version: 0.0.2 #Date: 24-10-07 / 2008-11-19 / 2008-12-08 #System: Ubuntu/Debian GNU/Linux #Information: http://www.w3.org/TR/1999/REC-xpath-19991116.html#data-model #Information: http://www.w3.org/TR/1999/REC-xpath-19991116.html #Information: http://zvon.org/xxl/XPathTutorial/General/examples.html #Category Install scripts #Description: Installation of thunar custom script system #Command: chmod +x pct-thunar-scripts-setup ./pct-thunar-scripts-setup #Thanks: Special thanks to kojiro and goedel at IRC # Did the script work for you? # Yes # Yes, but with some errors # Yes, but I had to change some things # Not at all # 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., 51 Franklin Street, Fifth Floor, Boston, # MA 02110-1301, USA. #sudo apt-get install libxml2-utils #sudo apt-get install xsltproc #tuxcrafter: make a habit of wrapping array expansion in quotes. #try to reduce things like grep | head | cut #try to reduce UUOC and UUOE #oldfile command | command2 - | command3 - | command4 - > tmpfile && mv tmpfile oldfile #[ "$name" ] || { echo "Missing name."; continue; } #if [ ! -n "$name" ] || [ ! -n "$command" ] || [ ! -n "$description" ] ; then echo "error, empty necessary option, continuing with next script"; continue; fi <-- nasty usability problem #cat "$xmlfile" > "$tempfile" #the greatest speedup will come from reducing those grep | head | cut pipelines #you'll gain better security from getting rid of tempfiles #xmllint will not output malformed xml cecho() { if [ "$1" = "black" ]; then echo -ne "\E[30m" elif [ "$1" = "red" ]; then echo -ne "\E[31m" elif [ "$1" = "green" ]; then echo -ne "\E[32m" elif [ "$1" = "yellow" ]; then echo -ne "\E[33m" elif [ "$1" = "blue" ]; then echo -ne "\E[34m" elif [ "$1" = "magenta" ]; then echo -ne "\E[35m" elif [ "$1" = "cyan" ]; then echo -ne "\E[36m" fi echo "$2" "$3" "$4" tput sgr0 } location="/usr/share/pct-thunar-scripts/scripts/" xmlfile="$HOME/.config/Thunar/uca.xml" killall Thunar ; killall thunar #whereis xmlstarlet if [ ! -e /usr/bin/xmlstarlet ] then cecho "red" "xmlstarlet not found, installing now" sudo apt-get install xmlstarlet fi #whereis xmllint if [ ! -e /usr/bin/xmllint ] then cecho "red" "xmllint not found, installing now" sudo apt-get install libxml2-utils fi if [ ! -d "$location" ] then cecho "red" "pct-thunar-scripts not found, installing now" sudo apt-get install pct-thunar-scripts #~ mkdir "$HOME/.scripts" #~ cp --recursive --verbose "$source" "$location" fi # workaround for but in Thunar generated uca.xml header xml_memory=$(awk 'BEGIN{ RS=">" } NR==1{ if(!/^<\?xml[^?]*\?/) printf("%s>", $0); RS=""; next } {print}' "$xmlfile") if [ ! -n "$xml_memory" ] then cecho "red" "xmlfile not found or empty, creating new structure" xml_memory='' fi cecho "green" "validating xmlfile " xml_memory=$(xmllint --encode 'UTF-8' - <<< "$xml_memory") count=1 install=false uninstall=false declare -a array_index declare -a array_filename declare -a array_scriptname declare -a array_description cecho "green" "Building list of available scripts ..." echo -e "\t\E[34m[\E[31mX\E[34m]\E[31m = \E[32minstalled" for file in $location/* do if [ -f "$file" ] then file_memory=$(<"$file") index="$count" array_index["$index"]="$index" array_filename["$index"]="$file" array_scriptname["$index"]=$(awk -F: '/#Name:/{p="[ \t]*";sub("^"p,"",$2);sub(p"$","",$2);print$2;exit}' <<< "$file_memory") array_description["$index"]=$(awk -F: '/#Description:/{p="[ \t]*";sub("^"p,"",$2);sub(p"$","",$2);print$2;exit}' <<< "$file_memory") array_installed["$index"]=$(xmlstarlet sel --text --template --match "/actions/action[name='${array_scriptname[$index]}']" --value-of "1" - <<< "$xml_memory") ((count++)) echo -ne "\E[34m[\E[31m$index\E[34m]\t" if [ -n "${array_installed[$index]}" ] then echo -ne "\E[34m[\E[31mX\E[34m]" else echo -ne "\E[34m[\E[31m-\E[34m]" fi cecho "green" -n " ${array_scriptname[$index]}" echo "${array_description[$index]}" fi done cecho "magenta" "-- -- -- -- -- -- -- -- -- -- --" cecho "green" "choose a option and press enter" cecho "red" "[1] install all scripts" cecho "red" "[2] uninstall all scripts" cecho "red" "[3] install scripts (selection will be made next)" cecho "red" "[4] uninstall scripts (selection will be made next))" cecho "red" "[5] remove current xmlfile configuration" cecho "red" "[6] exit" read -p "Pleae make your slection and press enter: " option if [ "$option" = "1" ] then cecho "green" "install all scripts" install="true" option_query=${array_index[@]} fi if [ "$option" = "2" ] then cecho "green" "uninstall all scripts" uninstall=true option_query=${array_index[@]} fi if [ "$option" = "3" ] then cecho "green" "install scripts ..." cecho "green" "please make your selection, from the above displayed list of scripts (example: 6,7,9,21,11)" IFS=, read -p "Enter your selection seperated by commas ',': " -a option_query unset IFS install=true fi if [ "$option" = "4" ] then cecho "green" "uninstall scripts ..." cecho "green" "please make your selection, from the above displayed list of scripts (example: 6,7,9,21,11)" IFS=, read -p "Enter your selection seperated by commas ',': " -a option_query unset IFS uninstall=true fi if [ "$option" = "5" ] then cecho "green" "removing configuration file: $xmlfile" rm --verbose "$xmlfile" exit fi if [ "$option" = "6" ] then cecho "green" "exiting now" exit fi cecho "green" "parsing selection ..." if [ ! -n "$option_query" ] then cecho "red" "error: no valid option found, exiting now" exit fi echo_option_query() { for value in ${option_query[*]} do cecho "green" -n "${array_index[$value]}:" echo $'\t' "${array_scriptname[$value]}" done } if [ "$install" = "true" ] then cecho "magenta" "installing the following scripts: " echo_option_query cecho "green" "installing, please standby ..." for value in ${option_query[*]} do file=${array_filename["$value"]} echo -n "processing: $file " name=${array_scriptname["$value"]} description=${array_description["$value"]} installed=${array_installed["$value"]} file_memory=$(<"$file") icon=$(awk -F: '/#Icon:/{p="[ \t]*";sub("^"p,"",$2);sub(p"$","",$2);print$2;exit}' <<< "$file_memory") command=$(awk -F: '/#Command:/{p="[ \t]*";sub("^"p,"",$2);sub(p"$","",$2);print$2;exit}' <<< "$file_memory") patterns=$(awk -F: '/#Patterns:/{p="[ \t]*";sub("^"p,"",$2);sub(p"$","",$2);print$2;exit}' <<< "$file_memory") directories=$(awk -F: '/#Directories:/{p="[ \t]*";sub("^"p,"",$2);sub(p"$","",$2);print$2;exit}' <<< "$file_memory") audio_files=$(awk -F: '/#Audio Files:/{p="[ \t]*";sub("^"p,"",$2);sub(p"$","",$2);print$2;exit}' <<< "$file_memory") image_files=$(awk -F: '/#Image Files:/{p="[ \t]*";sub("^"p,"",$2);sub(p"$","",$2);print$2;exit}' <<< "$file_memory") other_files=$(awk -F: '/#Other Files:/{p="[ \t]*";sub("^"p,"",$2);sub(p"$","",$2);print$2;exit}' <<< "$file_memory") text_files=$(awk -F: '/#Text Files:/{p="[ \t]*";sub("^"p,"",$2);sub(p"$","",$2);print$2;exit}' <<< "$file_memory") video_files=$(awk -F: '/#Video Files:/{p="[ \t]*";sub("^"p,"",$2);sub(p"$","",$2);print$2;exit}' <<< "$file_memory") if [ -n "$installed" ] then cecho "green" "already installed" continue fi if [ ! -n "$name" ] then echo "error, field name: $name is empty, continuing with next script" continue fi if [ ! -n "$command" ] then echo "error, field command: $command is empty, continuing with next script" continue fi if [ ! -n "$description" ] then echo "error, field description: $description is empty, continuing with next script" continue fi xml_memory=$(xmlstarlet ed --subnode "/actions" --type elem -n action -v "" <<< "$xml_memory") xml_memory=$(xmlstarlet ed --subnode '/actions/action[last()]' --type elem -n name -v "$name" <<< "$xml_memory") xml_memory=$(xmlstarlet ed --subnode '/actions/action[last()]' --type elem -n command -v "$command" <<< "$xml_memory") xml_memory=$(xmlstarlet ed --subnode '/actions/action[last()]' --type elem -n description -v "$description" <<< "$xml_memory") xml_memory=$(xmlstarlet ed --subnode '/actions/action[last()]' --type elem -n patterns -v "$patterns" <<< "$xml_memory") xml_memory=$(xmlstarlet ed --subnode '/actions/action[last()]' --type elem -n patterns -v "$patterns" <<< "$xml_memory") if [ "$directories" = "True" ] then xml_memory=$(xmlstarlet ed --subnode '/actions/action[last()]' --type elem -n directories -v "" <<< "$xml_memory") fi if [ "$audio_files" = "True" ] then xml_memory=$(xmlstarlet ed --subnode '/actions/action[last()]' --type elem -n audio-files -v "" <<< "$xml_memory") fi if [ "$image_files" = "True" ] then xml_memory=$(xmlstarlet ed --subnode '/actions/action[last()]' --type elem -n image-files -v "" <<< "$xml_memory") fi if [ "$other_files" = "True" ] then xml_memory=$(xmlstarlet ed --subnode '/actions/action[last()]' --type elem -n other-files -v "" <<< "$xml_memory") fi if [ "$text_files" = "True" ] then xml_memory=$(xmlstarlet ed --subnode '/actions/action[last()]' --type elem -n text-files -v "" <<< "$xml_memory") fi if [ "$video_files" = "True" ] then xml_memory=$(xmlstarlet ed --subnode '/actions/action[last()]' --type elem -n video-files -v "" <<< "$xml_memory") fi cecho "green" "installed" done xml_memory=$(xmllint --encode 'UTF-8' - <<< "$xml_memory") echo "$xml_memory" > "$xmlfile" cecho "green" "finished installing scritps" fi if [ "$uninstall" = "true" ] then cecho "magenta" "uninstalling the following scripts: " echo_option_query cecho "green" "uninstalling, please standby ..." for value in ${option_query[*]} do file=${array_filename["$value"]} name=${array_scriptname["$value"]} installed=${array_installed["$value"]} description=${array_description["$value"]} echo -n "processing: $file " if [ ! -n "$installed" ] then cecho "green" "not installed" continue fi if [ ! -n "$name" ] then echo "error, field name: $name is empty, continuing with next script" continue fi xml_memory=$(xmlstarlet ed --delete "/actions/action[name=\"$name\"]" <<< "$xml_memory") cecho "green" "uninstalled" done xml_memory=$(xmllint --encode 'UTF-8' - <<< "$xml_memory") echo "$xml_memory" > "$xmlfile" cecho "green" "finished uninstalling scritps" fi exit