Files
q-tools/web/rclone-mount
2024-09-08 20:05:44 +03:00

135 lines
3.0 KiB
Bash
Executable File

#!/bin/bash
_helpexit() {
printf "Usage: %s [-u] remote:[path]
remote from rclone config. Note, all remotes end with :
-u will unmount all FUSE.rclone paths if no path given!
" "$( basename $0 )"
_current_mounts
exit 1
}
_current_mounts() {
echo Current mounts:
cat /proc/mounts | grep fuse.rclone | awk '{ print $1 " " $2 }' | sed "s,$HOME,~," | xargs printf "%15s %s\n"
}
_menu() {
_current_mounts
_askpass
choice=$( rclone --ask-password=false listremotes | \
sort | \
smenu -t 1 -a c:0/2 i:3 -n 25 -m "Select remote" \
-N -D n:1 i:1 )
echo $choice
if [[ -z "$choice" ]]; then
_helpexit
fi
remote="$choice"
}
_askpass() {
if [[ -n "$RCLONE_CONFIG_PASS" ]]; then
# password already set
return
fi
if grep -q ^"# Encrypted rclone configuration File" "$HOME/.config/rclone/rclone.conf"; then
read -s -p "rclone config password: " RCLONE_CONFIG_PASS
export RCLONE_CONFIG_PASS
else
RCLONE_CONFIG_PASS="not-required"
true
fi
}
_check_mount() {
if [[ -d ~/mnt/"$valid_name" ]]; then
device1=$( stat -c "%d" ~/mnt/"$valid_name" )
device2=$( stat -c "%d" ~/mnt )
else
device1=valid
device2=valid
fi
}
[[ -z "$1" ]] && _menu
for (( i=1; i<=$#; i++ )); do
[[ ${!i} = "-h" ]] && _helpexit
[[ ${!i} = "--help" ]] && _helpexit
done
unmount=false
for (( i=1; i<=$#; i++ )); do
[[ ${!i} = "-u" ]] && { unmount=true; continue; }
if [[ -z "$remote" ]]; then
remote="${!i}"
fi
done
mkdir -p ~/mnt
valid_name=$( echo "${remote%:}" | sed -e 's/:/-/g' | sed -e 's/[^A-Za-z0-9._@-]//g' )
if [[ "$unmount" = true ]]; then
cd ~/mnt
if [[ -n "$remote" ]]; then
echo Unmounting ~/mnt/$valid_name
fusermount -u -z ~/mnt/$valid_name
rmdir --ignore-fail-on-non-empty ~/mnt/$valid_name &>/dev/null
else
# no path, unmount all
cat /proc/mounts | grep fuse.rclone | awk '{ print $2 }' | while read dir; do
echo Unmounting $dir
fusermount -u -z "$dir"
rmdir --ignore-fail-on-non-empty "$dir" &>/dev/null
done
fi
exit
fi
if [[ -z "$remote" ]]; then
echo "No remote given"
_helpexit
fi
_check_mount
tries=20
if [[ $device1 = $device2 ]]; then
echo "Mounting $remote in ~/mnt/$valid_name"
_askpass
cd ~/mnt
mkdir -p "$valid_name"
echo ""
rclone --ask-password=false mount --daemon "${remote}" ~/mnt/"$valid_name"
if [[ $? -gt 0 ]]; then
tries=1
fi
echo "Waiting for mount to finish ..."
else
echo "~/mnt/$valid_name is already mounted"
exit
fi
while [[ $tries -gt 0 ]]; do
read -t 1 -n 1 key
_check_mount
if [[ $device1 = $device2 ]]; then
tries=$(( $tries - 1 ))
else
df -h "$valid_name"
exit 0
fi
done
echo $remote was not mounted
fusermount -u -z ~/mnt/$valid_name &>/dev/null
rmdir --ignore-fail-on-non-empty ~/mnt/$valid_name &>/dev/null
exit 1