#!/bin/sh
# A small shell script that can be used to migrate a 1.1.X (or earlier)
# to 1.2.

if [ $# -lt 2 ]; then
  echo "Usage: migrate.sh url_list newport oldport [newserver oldserver]"
  exit 1
fi

url_list=$1
newport=$2
oldport=$3
newserver=$4
oldserver=$5
if [ -z "$oldport" ]; then
  oldport=3128
fi
if [ -z "$newserver" ]; then
  newserver=localhost
fi
if [ -z "$oldserver" ]; then
  oldserver=localhost
fi

# 1. Split the url_list in suitable chunks
rm -f tmpurl*
split "$url_list" tmpurl

for url_list in tmpurl*; do
  # 2. Move the cached objects to the new port
  echo "Getting objects"
  cat $url_list | tcp-banger2 -n 5 -h $newserver -p $newport
  # 3. Purge the cached objects from the old cache
  if [ "$oldport" != "-" ]; then
    echo "Purging objects"
    cat $url_list | awk '{print "PURGE",$0}' | tcp-banger2 -n 5 -h $oldserver -p $oldport
  fi
done

