tildemerge/sshfs_gui.sh

180 lines
4.4 KiB
Bash
Raw Normal View History

2018-11-20 06:36:24 +00:00
#!/bin/sh
# Copyright (c) 2018-2019 TildeLinux Maintainers
#
# 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.
#
# Email: tildelinux@tildeverse.org
2018-11-20 06:36:24 +00:00
2018-11-22 05:41:39 +00:00
setup_script_path="$HOME/.local/bin/setup_gui.sh"
2018-11-20 06:36:24 +00:00
eval "$(xdotool getmouselocation --shell)"
mouse_x=$X
mouse_y=$Y
ssh_config_file="$HOME/.ssh/config"
server_name=$(grep -E "[H,h]ost " "$ssh_config_file" \
| cut -f 2 -d ' ' \
| tr '\n' '!' \
| sed 's/!$//') # This only works in posix sh
2019-01-20 05:05:30 +00:00
sshfs_local_path="${HOME}/Remote/myfolder"
2018-11-20 06:36:24 +00:00
sshfs_remote_path="$HOME"
2019-01-20 05:05:30 +00:00
get_field() {
2018-11-20 06:36:24 +00:00
field_num="$1"
info="$2"
2019-01-20 05:05:30 +00:00
printf "%s" "$info" | cut -f "$field_num" -d '|'
2018-11-20 06:36:24 +00:00
}
2019-01-20 05:05:30 +00:00
error_box() {
2018-11-20 06:36:24 +00:00
error_text="$1"
yad \
--posx="$mouse_x" \
--posy="$mouse_y" \
--width 250 \
--image "dialog-error" \
--title "Error!" \
--text "$error_text" \
--button="OK":0
}
2019-01-20 05:05:30 +00:00
succ_box() {
2018-11-20 06:36:24 +00:00
succ_text="$1"
yad \
--posx="$mouse_x" \
--posy="$mouse_y" \
--width 250 \
--image "dialog-info" \
--title "Success!" \
--text "$succ_text" \
--button="OK":0
}
2019-01-20 05:05:30 +00:00
dec_box() {
2018-11-20 06:36:24 +00:00
error_text="$1"
yad \
--posx="$mouse_x" \
--posy="$mouse_y" \
--width 250 \
--image "dialog-error" \
--title "Error!" \
--text "$error_text" \
--button="No":1 \
--button="Yes":0
return $?
}
2019-01-20 05:05:30 +00:00
info_box() {
2018-11-20 06:36:24 +00:00
info_text="$1"
yad \
--posx="$mouse_x" \
--posy="$mouse_y" \
--no-buttons \
--width 250 \
--image "dialog-info" \
--title "Info" \
--text "$info_text"
}
get_info() {
error_num=0
error_msg=""
info=$(yad \
--posx="$mouse_x" \
--posy="$mouse_y" \
2018-11-20 08:03:53 +00:00
--width=400 \
2018-11-20 06:36:24 +00:00
--title="New remote directory" \
--text="Link to a remote directory" \
--form \
--field="SSH Config file":FL \
--field="Server Shortname":CB \
--field="Local Directory" \
--field="Remote Directory" \
2018-11-22 05:41:39 +00:00
--button="Configure Servers...":2 \
2018-11-20 06:36:24 +00:00
--button="Cancel":1 \
--button="Continue":0 \
"$ssh_config_file" "$server_name" "$sshfs_local_path" "$sshfs_remote_path")
2018-11-22 05:41:39 +00:00
info_return="$?"
2019-01-20 05:05:30 +00:00
if [ $info_return -eq 1 ]
2018-11-22 05:41:39 +00:00
then
exit
elif [ $info_return -eq 2 ]
then
if ! "$setup_script_path"
then
2019-01-20 05:05:30 +00:00
error_box "${setup_script_path} did not finish successfully!"
2018-11-22 05:41:39 +00:00
get_info
fi
fi
2018-11-20 06:36:24 +00:00
server_name=$(get_field 2 "$info")
sshfs_local_path=$(get_field 3 "$info")
sshfs_remote_path=$(get_field 4 "$info")
if test -z "$sshfs_local_path"
then
error_msg="No local path provided!"
2019-01-20 05:05:30 +00:00
error_num=$((error_num + 1))
2018-11-20 06:36:24 +00:00
fi
if test -z "$sshfs_remote_path"
then
2019-01-20 05:05:30 +00:00
error_msg="${error_msg}\\nNo remote path provided!"
error_num=$((error_num + 1))
2018-11-20 06:36:24 +00:00
fi
if [ $error_num -gt 0 ]
then
error_box "$error_msg" \
&& get_info
fi
configure_mount
}
configure_mount() {
info_box "Attempting to link directories..." &
info_pid=$(( $! + 2 )) # yikes, is this safe?
2018-11-20 08:03:53 +00:00
test -e "$sshfs_local_path" \
|| mkdir -p "$sshfs_local_path"
2018-11-20 06:36:24 +00:00
2019-01-20 05:05:30 +00:00
if ! ssh "$server_name" "test -d \${sshfs_remote_path}"
2018-11-20 06:36:24 +00:00
then
if dec_box "Directory $sshfs_remote_path does not exist on remote host, create?"
2018-11-20 06:36:24 +00:00
then
2019-01-20 05:05:30 +00:00
if ! error_msg=$(ssh "$server_name" "mkdir \${sshfs_remote_path}" 2>&1)
2018-11-20 06:36:24 +00:00
then
2019-01-20 05:05:30 +00:00
error_box "Failed to create remote directory. ${error_msg}"
2018-11-20 06:36:24 +00:00
fi
fi
fi
if error_msg=$(sshfs \
2019-01-20 05:05:30 +00:00
"${server_name}:${sshfs_remote_path}" \
"$sshfs_local_path" \
-oauto_cache,reconnect,Compression=no 2>&1)
2018-11-20 06:36:24 +00:00
then
kill $info_pid
2019-01-20 05:05:30 +00:00
succ_box "Successfuly linked ${sshfs_local_path} to ${sshfs_remote_path} on ${server_name}"
2018-11-20 06:36:24 +00:00
else
kill $info_pid
2019-01-20 05:05:30 +00:00
error_box "Failed to link ${sshfs_local_path} to ${sshfs_remote_path} on ${server_name}\\n${error_msg}"
2018-11-20 06:36:24 +00:00
fi
}
get_info