3 BROADSYNCDIR=~/.broadsync
 
   4 SYNCLINEFILE="synclines"
 
   7     fctime=$(stat -c %Y "$1")
 
   8     if [ -z "$2" ] || [ $fctime -gt $2 ]; then
 
  15 if [ ! -d "$BROADSYNCDIR" ]; then
 
  16     mkdir "$BROADSYNCDIR" || exit 1
 
  17     echo "Created directory $BROADSYNCDIR."
 
  20 if [ ! -f "$BROADSYNCDIR/$SYNCLINEFILE" ]; then
 
  21     touch "$BROADSYNCDIR/$SYNCLINEFILE" || exit 1
 
  22     chmod 600 "$BROADSYNCDIR/$SYNCLINEFILE" || \
 
  23         rm "$BROADSYNCDIR/$SYNCLINEFILE"
 
  25 # user:password:port:/remote_directory:/local_directory" > \
 
  26     "$BROADSYNCDIR/$SYNCLINEFILE"
 
  27     echo "Created syncline file $BROADSYNCDIR/$SYNCLINEFILE."
 
  30 if [ $(cat "$BROADSYNCDIR/$SYNCLINEFILE" | grep -v "#" | wc -l) -eq 0 ]; then
 
  31     echo "Your syncline file $BROADSYNCDIR/$SYNCLINEFILE is empty."
 
  32     echo "You have to add at least one syncline to run broadsync!"
 
  35 tmpdir="$(mktemp -p /tmp -d broadsync-XXXXXXX)" || exit 1
 
  38     sed -En 's/.*inet (([0-9]*\.){3}[0-9]*)\/.*/\1/p' | \
 
  39     grep -v '127.0.0.1' | \
 
  40     sed -En 's/(([0-9]*\.){3})[0-9]*/\1/p')
 
  42 echo "Subnets to probe: $(echo "$subnets" | tr '\n' ' ')"
 
  44 while read syncline; do
 
  45     syncline_user=$(echo "$syncline" | cut -f1 -d":")
 
  46     syncline_pass=$(echo "$syncline" | cut -f2 -d":")
 
  47     syncline_port=$(echo "$syncline" | cut -f3 -d":")
 
  48     syncline_rdir=$(echo "$syncline" | cut -f4 -d":")
 
  49     syncline_ldir=$(eval echo "$(echo "$syncline" | cut -f5 -d":")")
 
  51     syncline_hash=$(echo "$syncline_user:$syncline_port:$syncline_rdir" | \
 
  52         md5sum | cut -f1 -d" ")
 
  54     echo "Processing syncline $syncline_user:$syncline_port:$syncline_rdir"
 
  56     if [ ! -d "$syncline_ldir" ]; then
 
  57         echo "Creating local syncdir $syncline_ldir"
 
  58         mkdir "$syncline_ldir" || exit 1
 
  61     statdir="$BROADSYNCDIR/$syncline_hash.stat"
 
  62     if [ ! -d "$statdir" ]; then
 
  63         echo "Creating statdir $statdir"
 
  64         mkdir "$statdir" || exit 1
 
  67     if [ -f "$statdir/newestdate" ]; then
 
  68         newestdate=$(cat "$statdir/newestdate")
 
  69         echo "Using newestdate $(date +"%T %B %d %Y" -d @$newestdate)"
 
  74     for subnet in $subnets; do
 
  75         echo "Scanning for potential hosts in subnet $subnet"
 
  77         for i in $(seq 1 254); do
 
  78             (nc -w 5 -z "$subnet$i" "$syncline_port"; echo $? > "$tmpdir/nc-$subnet-$i") &
 
  82         for i in $(seq 1 254); do
 
  83             if [ $(cat "$tmpdir/nc-$subnet-$i") -eq 0 ]; then
 
  84                 echo "Trying to sync with potential host $subnet$i"
 
  86                 while read logline; do
 
  87                     [ -n "$lastfile" ] && newestdate=$(returnnewer "$syncline_ldir/$lastfile" "$newestdate")
 
  90                     file=$(echo "$logline" | sed -En 's/Transferring file .(.*)./\1/p')
 
  96                 done < <(lftp -p "$syncline_port" \
 
  97                     -u "$syncline_user,$syncline_pass" \
 
  98                     -e "mirror -v -N \"$(date +"%T %B %d %Y" -d @$newestdate)\" \"$syncline_rdir\" \"$syncline_ldir\"" \
 
 101                 [ -n "$lastfile" ] && newestdate=$(returnnewer "$syncline_ldir/$lastfile" "$newestdate")                
 
 106     echo $newestdate > "$statdir/newestdate"
 
 108 done < <(cat "$BROADSYNCDIR/$SYNCLINEFILE" | grep -v '#')