--- /dev/null
+#!/bin/bash
+
+#Config file:
+source ~/.sync.d/sync.conf;
+#Script file:
+which notify-send
+isNotifySend=$?
+
+#Function section:
+
+function is_ldir_a_git_repo() {
+ cd ${LDIR}
+ git status > /dev/null 2>&1;
+ return $?;
+}
+
+function is_ldir_a_rdir_clone() {
+ cd ${LDIR};
+ git remote get-url origin | grep -q ${RDIR}
+ return $?;
+}
+
+function update_rdir() {
+ cd ${LDIR};
+ git add --all;
+ git commit -m "Pushing changes.";
+ git push -u origin main;
+ if [ $? -eq 0 ]; then
+ output "Remote directory is now up to date." "ok";
+ return 0;
+ else
+ output "Problem ocurred when trying update remote directory" "warn";
+ return 1;
+ fi
+}
+
+function initialize_ldir_git() {
+ cd ${LDIR};
+ git init -b main;
+ git remote add origin ssh://${RUSER}@${RSERVER}${RDIR};
+ return $?;
+}
+
+function clone_rdir() {
+ git clone ssh://${RUSER}@${RSERVER}${RDIR} ${LDIR}
+ if [ $? -eq 0 ]; then
+ output "Local directory was already cloned from remote directory." "ok";
+ else
+ output "Problem ocurred when trying to clone remote directory." "warn";
+ fi
+}
+
+function get_update_info() {
+ cd ${LDIR};
+ git remote update > /dev/null 2>&1;
+}
+
+function is_ldir_need_to_update() {
+ cd ${LDIR};
+ git status | grep -q 'git pull';
+ return $?;
+}
+
+function is_rdir_need_to_update() {
+ cd ${LDIR};
+ git status | grep -Eq 'git add|git push'
+ return $?;
+}
+
+function update_ldir() {
+ cd ${LDIR};
+ git pull > /dev/null 2>&1;
+ if [ $? -eq 0 ]; then
+ output "Local directory now is up to date." "ok";
+ else
+ output "Problem occured when trying update local directory." "warn";
+ fi
+}
+
+function is_the_conflict() {
+ cd ${LDIR};
+ git push -u origin main | grep -q 'rejected';
+ if [ $? -eq 0 ]; then
+ output "Conflict ocurred. There are significant diffrences betwen local and remote dirs. Move changes outside local directory and delete him. Try synchronize dirs once again and put changes back" "bad";
+ else
+ output "Problem ocurred when trying update remote directory." "warn";
+ fi
+}
+
+function output() {
+ argv1=$@;
+ icon=$(echo $argv1 | sed 's,\ ,\n,g' | tail -1);
+ msg=$(echo $argv1 | sed -e "s,\ $icon,,g" -e 's/^[[:space:]]*//g');
+
+ if [ "$icon" = "ok" ]; then
+ nsIcon="emblem-synchronizing";
+ elif [ "$icon" = "warn" ]; then
+ nsIcon="dialog-warning";
+ elif [ "$icon" = "bad" ]; then
+ nsIcon="process-stop";
+ fi
+
+ if [ $isNotifySend -eq 0 ]; then
+ notify-send "Sync" "$msg" --icon=$nsIcon
+ else
+ echo "$msg";
+ fi
+}
+
+if [ ! "$KEYFILE" ]; then
+ ssh-keygen -f ${HOME}/id_rsa
+ ssh-copy-id ${SSHOPTS} -i ${HOME}/id_rsa ${RUSER}@${RSERVER}
+fi
+
+ssh ${SSHOPTS} ${RUSER}@${RSERVER} "[ -d ${RDIR} ]";
+if [ $? -ne 0 ]; then
+ ssh ${SSHOPTS} ${RUSER}@${RSERVER} "mkdir -p ${RDIR}"
+ ssh ${SSHOPTS} ${RUSER}@${RSERVER} "cd ${RDIR} && git init --bare -b main";
+ empty=0
+fi
+
+if [ ! -d ${LDIR} ] && [ "$empty" ]; then
+ mkdir -p ${LDIR}
+ initialize_ldir_git;
+ output "Local directory was already created. Remote directory seems to be empty. Nothing to do. Exiting." "warn";
+ exit 0;
+elif [ -d ${LDIR} ] && [ "$empty" ]; then
+ is_ldir_a_git_repo;
+ if [ $? -eq 0 ]; then
+ is_ldir_a_rdir_clone;
+ if [ $? -eq 0 ]; then
+ update_rdir;
+ exit 0;
+ else
+ output "Local directory is other repository than remote directory." "bad";
+ exit 1;
+ fi
+ else
+ initialize_ldir_git;
+ update_rdir;
+ exit 0;
+ fi
+elif [ ! -d ${LDIR} ] && [ ! "$empty" ]; then
+ clone_rdir;
+ exit 0;
+fi
+
+get_update_info;
+is_ldir_need_to_update;
+ldir_update=$?;
+is_rdir_need_to_update;
+rdir_update=$?;
+if [ $ldir_update -eq 0 ]; then
+ update_ldir;
+elif [ $rdir_update -eq 0 ]; then
+ update_rdir;
+ if [ $? -ne 0 ]; then
+ is_ther_conflict;
+ fi
+else
+ output "Everything is up to date." "ok";
+fi