diff --git a/sync-iPhone.sh b/sync-iPhone.sh index cc1b161..8ee25d5 100755 --- a/sync-iPhone.sh +++ b/sync-iPhone.sh @@ -1,35 +1,41 @@ -#!/bin/sh +#!/bin/bash + +# Define required programs and libraries program_list=("ifuse" "ldconfig" "rsync") library_list=("libimobiledevice") + sync_folder="$HOME/iPhone" sync_mount="${sync_folder}/.mnt" sync_data="${sync_folder}/data" -# iteration thru list of programs -for program in "${program_list[@]}" -do +# Check if required programs are installed +for program in "${program_list[@]}"; do if command -v "${program}" >/dev/null 2>&1 then echo "OK, ${program} exists." else - echo "Sorry, ${program} does not exists," - echo "you have to install all of these programs first: ${program_list[@]}" + echo "Error: ${program} is missing. Please install: ${program_list[*]}" exit 1 fi done # iteration thru list of libraries -for library in "${library_list[@]}" -do +for library in "${library_list[@]}"; do if ldconfig -p | grep ${library} >/dev/null 2>&1 then echo "OK, ${library} exists." else - echo "Sorry, the library ${library} is not installed." - echo "You have to install the libraries first: ${library_list[@]}" + echo "Error: Library ${library} is missing. Please install: ${library_list[*]}" exit 2 fi done + +# Clear older dead mounts if they block the directory +if [ -d "${sync_mount}" ]; then + fusermount -u "${sync_mount}" >/dev/null 2>&1 || umount -l "${sync_mount}" >/dev/null 2>&1 + rmdir "${sync_mount}" >/dev/null 2>&1 +fi + echo "Create the folder ${sync_mount} ..." if mkdir -p ${sync_mount} >/dev/null 2>&1 then @@ -43,36 +49,55 @@ if ifuse ${sync_mount} then echo "ok." else - echo "did not work. Check your connection to the iPhone." + echo "did not work. Check your connection to the iPhone. Check PIN on the iPhone." exit 3 fi -echo "RSYNC the iPhone ..." -FROM="$sync_mount"/ -TO="$sync_data"/ +# Display storage capacity of the iPhone +echo -e "\n--- IPHONE STORAGE USAGE ---" +df -h "${sync_mount}" +echo -e "----------------------------\n" -while true -do +echo "RSYNC the iPhone ..." +FROM="${sync_mount}/" +TO="${sync_data}/" + +while true; do echo "** Synchronization from ${FROM} to ${TO} **" - read -n 1 -p"Sync iPhone and delete old files (y)es/(n)o/(c)ancel? " response + read -n 1 -p "Action: (s)ync & delete / (n)ormal sync / (m)ount only & exit / (c)ancel? " response +# read -n 1 -p"Action: Sync iPhone and delete old files (y)es/(m)ount only/(n)o/(c)ancel? " response echo case ${response} in - [Nn]* ) rsync -avP "${FROM}" "${TO}"; break;; - [YyJj]* ) rsync -avP --delete "${FROM}" "${TO}"; break;; - [Cc]* ) break;; - * ) echo "Please answer yes, no or cancel.";; + [Nn]* ) + mkdir -p "${sync_data}" + rsync -avP "${FROM}" "${TO}" + break + ;; + [Ss]* ) + mkdir -p "${sync_data}" + rsync -avP --delete "${FROM}" "${TO}" + break + ;; + [Mm]* ) + echo "iPhone remains mounted at ${sync_mount}. Remember to unmount manually later!" + exit 0 + ;; + [Cc]* ) + echo "Synchronization skipped by user." + break + ;; + * ) + echo "Invalid selection. Please choose y, n, m, or c." + ;; esac done -echo "Memory usage on the iPhone:" && du -hs ${sync_mount} -echo "Unmount the iPhone ..." -if umount ${sync_mount} -then - echo "done." +echo "Unmounting iPhone..." +if fusermount -u "${sync_mount}" 2>/dev/null || umount "${sync_mount}" 2>/dev/null; then + echo "Successfully unmounted." + rmdir "${sync_mount}" else - echo "The umounting procedure did not work." + echo "WARNING: Unmount failed. A process might still be accessing the mount directory." fi -echo "Remove temporary mount point ..." -rmdir ${sync_mount} -echo "done." -echo "Disk usage:" && du -hs ${sync_folder} + +echo "Process finished."