Skip to content

Instantly share code, notes, and snippets.

@nhomble
Last active August 29, 2015 14:04
Show Gist options
  • Save nhomble/75e55465e5520b6c6dd1 to your computer and use it in GitHub Desktop.
Save nhomble/75e55465e5520b6c6dd1 to your computer and use it in GitHub Desktop.
at long last, a dumb utility to move files from my computer to my Galaxy S3
#!/usr/bin/env bash
DIR_NAME="/home/nicolas/phone-sync/"
IFS=$'\n'
function main() {
DIR_KEY=$(mtp-folders | grep "sync" | sed 's/\([0-9]\+\)\s.*/\1/g')
loadLocalFiles
loadPhoneFiles
declare -a toAdd
declare -a toDelete
local j=0
local k=0
for (( i = 0; i < ${#localFiles[*]}; i++ )); do
f=${localFiles[$i]}
flag=false
for (( k = 0; k < ${#phoneFiles[*]}; k++ )); do
comp=${phoneFiles[$k]}
echo "compare $f to $comp"
if [[ "$f" == "$comp" ]]; then
echo "true"
flag=true
break
fi
done
if [[ $flag = false ]]; then
toAdd[$j]="$f"
let j+=1
fi
done
local j=0
for (( i = 0; i < ${#phoneFiles[*]}; i++ )); do
f=${phoneFiles[$i]}
flag=false
for (( k = 0; k < ${#localFiles[*]}; k++ )); do
comp=${localFiles[$k]}
if [[ "$f" == "$comp" ]]; then
flag=true
break
fi
done
if [[ $flag = false ]]; then
toDelete[$j]="${phoneFiles[$j]}"
let j+=1
fi
done
for f in "${toAdd[@]}"; do
echo "send $f"
mtp-sendfile "$f" $DIR_KEY
done
for f in "${toDelete[@]}"; do
echo "delete $f"
mtp-delfile -f "$f"
done
}
localFiles=[]
function loadLocalFiles() {
local i=0
for f in $(find "$DIR_NAME" -type f); do
localFiles[$i]="$(basename $f)"
let i+=1
done
}
phoneFiles=[]
function loadPhoneFiles() {
local i=0
mtp-files | grep -B 3 $DIR_KEY > data
while read -r line; do
# Filename
if [[ ! -z "$(echo $line | grep "Filename")" ]]; then
phoneFiles[$i]="$(echo $line | sed 's/\s*Filename:\s*\(.*\)/\1/g')"
echo ${phoneFiles[$i]}
fi
let j+=1
if [[ "$line" == "--" ]]; then
let i+=1
j=0
fi
done < data
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment