scripts

misc scripts and tools
git clone git://git.2f30.org/scripts.git
Log | Files | Refs

itor (1199B)


      1 #!/bin/sh
      2 
      3 host=sin@destiny.2f30.org
      4 
      5 # remote directory - you can override it
      6 # with the RDIR environment variable
      7 rdir=$(test -n "$RDIR" && echo "$RDIR" || echo /home/sin/torrents)
      8 
      9 # local directory - you can override it
     10 # with the LDIR environment variable
     11 ldir=$(test -n "$LDIR" && echo "$LDIR" || echo $HOME/torrents)
     12 
     13 mkdir -p $ldir
     14 
     15 listing=$(mktemp)
     16 
     17 trap 'rm -f $listing; exit 0' 0 2 15
     18 
     19 ssh $host "ls $rdir" > $listing
     20 if [ ! $? -eq 0 ]; then
     21 	exit 1
     22 fi
     23 
     24 idx=0
     25 while read f; do
     26 	echo "$idx: $f"
     27 	idx=$((idx+1))
     28 done < $listing
     29 
     30 if [ $idx -eq 0 ]; then
     31 	echo "No files to serve"
     32 	exit 0
     33 fi
     34 
     35 isnum() { test "$1" && printf '%d' "$1" >/dev/null 2>/dev/null; }
     36 
     37 while :; do
     38 	echo -n "Your selection: "; read index
     39 	isnum $index
     40 	if [ $? -eq 0 ]; then
     41 		if [ $index -ge $idx ]; then
     42 			echo "invalid selection: $index"
     43 			continue
     44 		fi
     45 	else
     46 		echo "not a number"
     47 		continue
     48 	fi
     49 	break
     50 done
     51 
     52 idx=0
     53 while read f; do
     54 	if [ $index -eq $idx ]; then
     55 		break
     56 	fi
     57 	idx=$((idx+1))
     58 done < $listing
     59 
     60 # Taken from: http://www.etalabs.net/sh_tricks.html
     61 quote() { printf %s\\n "$1" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/'/" ; }
     62 
     63 quoted=$(quote "$rdir/$f")
     64 rsync --progress -av -e ssh $host:"$quoted" "$ldir"