#!/bin/sh

usage() {
cat >&2 <<EOF
Usage: `basename $0` -lock|-smnt  -oldurl oldurl -newurl newurl
  -lock			lock individual bugs before making changes
  -smnt			Shutdown ClearDDTS before making changes
  -oldurl oldurl	Specify the old webddts url
  -newurl newurl	Specify the new webddts url
EOF
exit 1
}

ddtshome=`/bin/echo 'echo ~ddts' | /bin/csh -sf`
if test $? != 0
        then
        x=`echo 'echo ~ddts' | csh -sf`
        if test $? != 0
                then
                echo "Can't set up PATH variable" 
                exit 1
        fi
fi
PATH=$ddtshome/`$ddtshome/bin/ddts_archdir.sh`:$PATH; export PATH

# Now run `ddtshome` in case DDTSHOME is pointing elsewhere.
ddtshome=`ddtshome`
if [ "$ddtshome" = "" ]; then
    echo "Unable to find the ddts home directory.  Maybe the ddts bin dir is not in your path." 1>&2
    exit 1
fi

# parse arguments.
bug_lock=0
system_smnt=0
if [ $# -ne 0 ]; then
    while [ $# -gt 0 ]; do
	arg=$1
	shift
    	case "$arg" in
	    -lock)
	    	if [ $system_smnt -eq 1 ]; then
		    usage
		fi
		bug_lock=1
	    	;;
	    -smnt)
	    	if [ $bug_lock -eq 1 ]; then
		    usage
		fi
		system_smnt=1
	    	;;		
	    -oldurl)
	    	if [ $# -gt 0 ]; then
		    oldurl=$1
		    shift
		else
		    usage
		fi
		;;
	    -newurl)
	    	if [ $# -gt 0 ]; then
		    newurl=$1
		    shift
		else
		    usage
		fi
		;;
	    *)
	    	usage
		;;	
	esac
    done
else
    usage
fi

# Ensure at least one variable has been set.  The argument parsing ensures both
# cannot be set.
if [ $system_smnt -eq 0 -a  $bug_lock -eq 0 ]; then
    usage
fi

cd $ddtshome

if [ ! -d allbinaries ]; then
    echo "No binary attachments to fix."
    exit 0
fi

while [ -z "$oldurl" ]; do
    echo "Please specify the old webddts url."
    echo "For example, this would be an entry like http://<old_web_server>/ddts/ddts_main"
    read oldurl 
done

while [ -z "$newurl" ]; do
    echo "Please specify the new webddts url."
    echo "For example, this would be an entry like http://<new_web_server>/ddts/ddts_main"
    read newurl
done

# Extract web server names from oldurl and newurl
# Remove leading http:// and trailing /<ScriptAlias>/ddts_main<optional characters>
# For example http://server:port/ddts/ddts_main will become server:port
oldurl=`echo $oldurl | sed 's#^http://##' | sed 's#/.*/ddts_main.*$##'`
newurl=`echo $newurl | sed 's#^http://##' | sed 's#/.*/ddts_main.*$##'`

# We are finally ready to fix the bugs.  Put the system in maintainence if need be.
if [ $system_smnt -eq 1 ]; then
    adminbug smnt
fi

#  For each bug that has an attachment
for bugid in `/bin/ls allbinaries`
do
    search_pattern="^ <A HREF=\"http://$oldurl/all-bin/$bugid/"
    # Find the bug and fix it.  Only attempt fixing those files that contain the pattern
    # to fix.
    bdir=`echo $bugid | cut -c9,10`
    grep "$search_pattern" allbugs/$bdir/$bugid 2> /dev/null 1>&2
    if [ $? -eq 0 ]; then
	if [ $bug_lock -eq 1 ]; then
	    echo "Acquiring lock for $bugid"
	    until ddtslock $bugid
	    do
		echo "Trying to re-acquire lock for $bugid"
	    done 
	fi
	sed "s#$search_pattern# <A HREF=\"http://$newurl/all-bin/$bugid/#g" allbugs/$bdir/$bugid > /tmp/$bugid.$$
	mv /tmp/$bugid.$$ allbugs/$bdir/$bugid

	if [ $bug_lock -eq 1 ]; then
	    echo "Releasing lock for $bugid"
	    ddtslock -u $bugid
	fi
	echo "$bugid" >> $ddtshome/tmp/fix_attachments.bbox.$$
    fi
done

# Restart daemons if we forced them into maintainence mode
if [ $system_smnt -eq 1 ]; then
    adminbug emnt
fi

if [ -f $ddtshome/tmp/fix_attachments.bbox.$$ ]; then
	echo "Attachments on your local site have been fixed."
	echo "Run batchbug [-n] -b $ddtshome/tmp/fix_attachments.bbox.$$ to update the local"
	echo "database and fix attachments on remote sites. Make sure you restore your"
	echo "connections and subscriptions to/from the remote sites before you run this command."
else
	echo "`basename $0` did not find the old URL to attachments in any of the defects."
fi	

exit 0