Permalink
Switch branches/tags
Nothing to show
Find file Copy path
965ae69 May 22, 2014
1 contributor

Users who have contributed to this file

400 lines (377 sloc) 12.6 KB
#! /bin/bash
licence="
# +------------------------------------------------------------------------------------------------------------+
# |
# |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 3 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.
# |
# +------------------------------------------------------------------------------------------------------------+"
mini_changelog="Nothing works for now, just playing with yad widgets..."
git_address="https://github.com/Mermouy/inosync-gui"
git_link="<a href=\"${git_address}\">${git_address}</a>"
summary="<b>Simple gui to sync directories with inosync &amp; yad</b>\n\nThe project is on github and should take long to upgrade as I'm not a real developper...\nIf you are developper, I will be happy to learn advices from you but, please do not give all answers without <i>child-level explanations</i>!\nIf you're a beginner as I am feel free to participate to this small project.\nPlease contact me if you need help like \"where to start\"\nGithub repo: ${git_link}\n"
author="MerMouY [mermouy [at] gmail .com]"
licence_link="<a href=\"http://www.gnu.org/licenses/gpl.html\">Gpl Official page</a>"
version="0.0.1"
app_name="inosync-gui"
yad=$(which yad)
ino_dir="$HOME/.config/inosync-gui"
install_path="/usr/local/bin"
yadopt="--title=Inosync-gui --always-print-result --center --window-icon=icons/inosync-gui_32.png"
app_name_error="gtk-help"
app_name_ok="gtk-ok"
ino_conf_dir="${ino_dir}/conf.d"
ino_icons_dir="${ino_dir}/icons"
tmp="/tmp/inosync-gui"
#All aliases for core-utils as they are often updated by user/distrib profile?
alias ls=$(which ls)
##
# add handler to manage process shutdown
# on_exit() {
# rm -f ${ino_icons_dir}/${name}.png
# }
# trap on_exit EXIT
###FUNCTIONS
# If yad not present what other choice do we have?
search_dialog()
{
for i in $(which kdialog zenity enity shellementary Xdialog 2> /dev/null); do
if [ -n ${i} ]; then
DIALOG=${i}
return;
fi
done
}
# What sudo dialog to use
search_sudo()
{
for i in $(which xdg-sudo xdg-su kdesu gksudo gksu esudo 2> /dev/null); do
if [ -n ${i} ]; then
SUDO=${i}
return
else #TODO: use ${DIALOG} to read sudo password
message error "Error please install either kdesu, gksudo or esudo manually and restart the installer"
exit 1
fi
done
}
# Messages system
message()
{
# More than an argument
if [ ${#} -gt 1 ]; then
case "${1}" in
# Case error style message
"error")
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $@" >&2
${DIALOG} ${yadopt} --text="<span color=\"red\"><b>${2}</b></span>" --image="${app_name_error}" --button=Quit ${3}
;;
# Case error style message + exit 1
"xerror")
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $@" >&2
${DIALOG} ${yadopt} --text="<span color=\"red\"><b>${2}</b></span>" --image="${app_name}_error" --button=Quit
exit 1
;;
# Case warning style message
"warning")
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $@" >&2
${DIALOG} ${yadopt} --text="<b>${2}</b>" --image="${app_name}\_warning" --button=Ok ${3}
;;
# Anything else
"*")
${DIALOG} ${yadopt} --text="<span color=\"green\"><b>${1}</b></span>" --image="${app_name}_ok" --button=Ok
;;
esac
else
# Only one argument
${DIALOG} --text="<span color=\"red\"><b>$1</b></span>" --image="${app_name}_error" --button=Quit
fi
}
# About & help dialog
about_ui()
{
echo -e "${licence}" | ${DIALOG} ${yadopt} --text-info --width=500 --height=650 --image="gtk-help" \
--text="<b>${app_name}</b>\n\nversion $version is slowly and poorly developped by <b>${author}</b>\n\n${summary}\n\n<b>Mini changelog:\n\n<span color=\"green\"><tt>${mini_changelog}</tt></span>\n\nFollow or participe on this small project at:\n${git_link}\n\nLicence:</b>" \
--button=Quit:1 --image-on-top
}
# Install Script TODO: use of bash loader?
install_it()
{
act_dir=$(pwd)
# Sudo verification
if [ -z $(which kdesu) ]; then
search_sudo
else SUDO=$(which kdesu)
fi
# Cloning in /usr/local/src
cd /usr/local/src || message xerror "Unable to <tt>cd /usr/local/src</tt>\n\nExiting..."
# remove existing clone TODO git pull
if [ ! -d ${app_name} ]; then
# Clone repo if not cloned yet
${SUDO} git clone ${git_address} ${app_name} || message xerror "Unable to clone repo:\n${git_link}\n\nExiting.."
cd ${app_name} || message xerror "Unable to cd ${app_name}\n\nExiting.."
else
cd ${app_name} || message xerror "Unable to cd ${app_name}\n\nExiting.."
${SUDO} git pull || message xerror "Unable to fetch repo:\n${git_link}\n\nExiting.."
fi
#List executables & copy them to ${install_path}
exe_list=$(ls -F | grep '*')
for f in "${exe_list}"; do
${SUDO} rm -f ${install_path}/
${SUDO} ln -s $(pwd)/ ${install_path}/ || message xerror "Unable to :\n<tt><span color=\"black\">link $(pwd)/</span></tt> to <tt><span color=\"black\">${install_path}/</span></tt>"
done
mkdir -pv ${ino_conf_dir}
cat > ${ino_conf_dir}/MyFirstSync.desktop << EOF
[Desktop Entry]
version=0.1
Encoding=UTF-8
Name=My first sync
Comment=Just an example of Synchronization, you can safely delete
Exec=inosync-tray --about
StartupNotify=true
Terminal=false
Icon=view-refresh
EOF
echo -e "
## Edit to your needs:
inosync=\"/usr/local/bin/inosync.py\"
rsync=$(which rsync)
SUDO=\"${SUDO}\"
ino_dir="${ino_dir}"
install_path="${install_path}"
yadopt="--title=Inosync-gui --always-print-result --center --window-icon=icons/inosync-gui_32.png"
" > ${ino_dir}/${app_name}.conf
cd ${act_dir}
}
CreateConf()
{
if [ -z "${UserConf[0]}" ]; then oIFS=$IFS
IFS=|
UserConf=($(${DIALOG} --title="Inosync-gui" --always-print-result --center --window-icon="icons/inosync-gui_32.png" --text="<b>Edit your Synchronized Folders: </b>" --form \
--field="Name of this Synchronization: <sub>*</sub>" 'MySync' \
--field="Folder to watch and Sync from: <sub>*</sub>":dir "/" \
--field="Folder to sync to (Node): <sub>*</sub>":dir '/' \
--field="Description you may need: " 'Your tiny description' \
--field="An icon may also help: ":fl '/usr/share/icons/gnome/' \
--field="Simulate?":CHK 'FALSE'))
case $? in
1|252) message xerror "Cancelled by user" ;;
esac
fi
IFS=${oIFS}
name="${UserConf[0]}"
fileverif=$(file -b --mime-type ${UserConf[4]})
case ${fileverif} in
*png|*.jpeg) convert ${UserConf[4]} -resize x64 ${ino_icons_dir}/${name}.png && UserConf[4]="${ino_icons_dir}/${name}.png" ;;
*) message warning "Icon must be an image...\nUsing default icon..." && UserConf[4]="gtk-help" ;;
esac
if [ -f "${ino_conf_dir}/${name}.desktop" ]; then
${DIALOG} --center --text="<span color=\"green\"><b>${name}.desktop exist\n\nShould I rename it in :</b></span>" --entry-text="${name}.1.desktop" --entry --image="${app_name}_ok" --button=Overwrite:1 --button=Rename:0 --button="Add Node":2 --button=Quit:3
case $? in
2) message warning "Soon..." # TODO
;;
0) name="${name}.1.desktop"
;;
1) name="${UserConf[0]}"
;;
*) rm -f ${ino_icons_dir}/${name}.png && exit 1
;;
esac
fi
if ${UserConf[5]}; then
${inoptions}='--norsync'
fi
echo -e "wpaths = [
\"${UserConf[1]}\"
]
#rexcludes = [
# \"/localhost\",
# ]
rpaths = [
\"${UserConf[1]}\"
]
rnodes = [
\"${UserConf[2]}\",
]
#extra = \"--rsh=ssh -a\"
#rspeed = 0
#emask = [
# \"IN_CLOSE_WRITE\",
# \"IN_CREATE\",
# \"IN_DELETE\",
# \"IN_MOVED_FROM\",
# \"IN_MOVED_TO\",
#]
#edelay = 10
rsync = \"$rsync\"
Inoptions = \"$i{noptions}\"
SyncIcon = \"${UserConf[4]}\"
SyncDescription = \"${UserConf[3]}\"
" > ${ino_conf_dir}/${name}.conf
echo -e "#!/usr/bin/env xdg-open
[Desktop Entry]
Version=${version}
Encoding=UTF-8
Type=Application
Name=${name}
Comment=${UserConf[3]}
Exec=inosync-tray ${ino_conf_dir}/${name}.conf
StartupNotify=true
Terminal=false
Icon=${UserConf[4]}
" > ${ino_conf_dir}/${name}.desktop
}
Sync()
{
echo "prout"
}
Edit_ui()
{
#Get actives synchronistaions
if [[ ! -f ${tmp}/.activ_list ]];then
for d in ${ino_conf_dir}/*.conf;do
grep 'SyncEnable="true"' "${d}" >/dev/null && echo "$d" >> ${tmp}/.activ_list
done
fi
# Retrieve list of configured syncs
sync_List=$(for f in ~/.config/inosync-gui/conf.d/*.desktop ; do \
echo "$(basename ${f} .desktop)"
grep -m 1 -is 'Icon=' ${f} | sed 's/Icon=//'
grep -m 1 -is 'Icon=' ${f} | sed 's/Icon=//'
echo "${f}"
grep 'SyncEnable="true"' ${ino_conf_dir}/$(basename ${f} .desktop).conf >>/dev/null && echo "gtk-ok" || echo "gtk-cancel"; done)
# Dialog to them
file2edit=$(echo "${sync_List}" | ${DIALOG} ${yadopt} --list --width=800 --height=450 \
--text="<big><b>Select synchronisation config to edit/remove: </b></big>\nDouble click to edit file." \
--column="Name: " --column="Icon: ":img --column="Icon_path":hd --column="File: " --column="Enable: ":img \
--button='Remove!gtk-remove!Remove selected Sync':2 \
--button='Modify!gtk-edit!Modify selected Sync':0 \
--button='Add!gtk-add!Add a new synchronization':3 \
--button='Run!gtk-exec!Run selected synchronization':4 \
--button='Cancel!gtk-cancel!Cancel and go back to main menu':1 \
--image=icons/inosync-gui_128.png --buttons-layout=spread --separator=" ")
case $? in
4) run_conf ${file2edit} ; shift;;
3) CreateConf ;;
2) Rm_User_Conf ${file2edit}; ./$0 ;;
0) Edit_User_Conf ${file2edit} ;;
*) exit 0;;
esac
}
run_conf() {
file2edit="$3"
`grep '^Exec' ${file2edit} | tail -1 | sed 's/^Exec=//' | sed 's/%.//'` &
}
parse_conf() {
conf_file="${1}"
sed 's/ //g' "${conf_file}" > "${temp_file}"
}
Edit_User_Conf() {
if [[ -z "${3}" ]]; then message xerror "Error getting desktop file arguments: ${1}"; fi
name="${1}"
icon="${2}"
deskfile="${3}"
# Retrieve config files from desktop & conf files
conf_file="${deskfile/.desktop/.conf}"
# FIXME parse conf file to get rnodes list
rnodes=$(sed -e '/rnodes/,/]/!d;/,/!d;s/"//g;s/,//g' ${conf_file})
wpaths=$(sed -e '/wpaths/,/]/!d;/"/!d;s/"//g;s/,//g' ${conf_file})
description=$(grep -i "SyncDescription" ${conf_file} | cut -d \" -f2-)
icon=$(grep -i "SyncIcon" ${conf_file} | sed 's/SyncIcon = //;s/"//g')
oIFS=$IFS
IFS="|"
UserConf=($(${DIALOG} --title=Inosync-gui --always-print-result --center --window-icon=icons/inosync-gui_32.png --text="<b>Edit your Synchronized Folders: </b>" --form \
--field="Name of this Synchronization:\nIf you change name it will create a new conf file\nand you will have to remove the old one." ${name} \
--field="Folder to watch and Sync from: ":dir ${wpaths} \
--field="Folder(s) to sync to (Nodes): " ${rnodes} \
--field="Description you may need: " ${description} \
--field="An icon may also help: ":fl $icon \
--field="Simulation: ":CHK "FALSE" \
--field="Enable: ":CHK "TRUE" \
--image=$(grep -i "SyncIcon" ${conf_file} | sed 's/SyncIcon = //;s/"//g') --image-on-top --button=Ok:1
))
IFS="${oIFS}"
if [ -n "${UserConf[0]}" ]; then export UserConf && CreateConf; fi
}
export -f Edit_User_Conf
Rm_User_Conf() {
file2rm="${3}"
if [ -n "${file2rm}" ]; then name=$(basename ${file2rm} .desktop)
file3rm="${ino_conf_dir}/${name}.conf"
icon2rm="${ino_icons_dir}/${name}.png"
${DIALOG} ${yadopt} --image=gtk-dialog-question --width=700 \
--text="<b>Do you really want to delete:\n\n<tt>${name}.desktop\n${name}.conf\n</tt>And\n<tt>${name}.png</tt>?</b>"
case $? in
1|252) return 1 ;;
*) rm -f {${file2rm},${file3rm},${icon2rm}}
return 0;;
esac
else return 1
fi
}
#
# main_ui()
# {
# choice=$(${DIALOG} ${yadopt} --icons --width=750 --height=350 \
# --read-dir="${ino_conf_dir}" --sort-by-name --item-width=128 \
# --text="<b><big>Double-click on icon to run the configured Sync.</big></b>\n\n \
# You can also use buttons below to <i>add</i> or <i>edit</i>, remove configurations." \
# --buttons-layout=spread \
# --button="Remove/Edit!gtk-edit!Remove or modify configured Sync":2 \
# --button="Add Sync!gtk-add!Add a new Sync configuration":0 \
# --button="Quit!gtk-close!Close $0":1 \
# --image=icons/inosync-gui_128.png)
# case $? in
# 252|1) exit 0
# ;;
# 0) CreateConf
# ;;
# 2) Edit_ui
# ;;
# esac
# }
# export -f main_ui
# Dialog Verifications
verify()
{
if [ -z "${yad}" ]; then
search_dialog
if [ -z ${DIALOG} ]; then
echo -e "No dialog command found.\nPlease install kdialog, zenity, xdialog or dialog for a better interface."
else message "The yad dialog is not present.\n${app_name} should be soon adapted to ${DIALOG}."
exit 0
fi
else DIALOG="${yad}"
fi
if [ -f ${ino_dir}/${app_name}.conf ]; then
source ${ino_dir}/${app_name}.conf
else
install_it
fi
}
main()
{
case ${1} in
"--update") install_it
exit 0
;;
"--add") CreateConf
exit 0
;;
"--about") about_ui
exit 0
;;
"") Edit_ui
exit 0
;;
esac
}
verify
main