/*
 * #%L
 * ImageJ software for multidimensional image processing and analysis.
 * %%
 * Copyright (C) 2009 - 2013 Board of Regents of the University of
 * Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
 * Institute of Molecular Cell Biology and Genetics.
 * %%
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 * 
 * The views and conclusions contained in the software and documentation are
 * those of the authors and should not be interpreted as representing official
 * policies, either expressed or implied, of any organization.
 * #L%
 */

package imagej.updater.core;

import imagej.updater.util.Util;

/**
 * Update sites for the updater.
 * 
 * <p>
 * An update site is a set of files served by a web server that conforms to a
 * certain set of rules defined by the ImageJ updater: a <i>db.xml.gz</i> file
 * in the root directory contains detailed information about available file
 * versions, past file versions, descriptions, authors, links, etc.
 * 
 * @author Johannes Schindelin
 */
public class UpdateSite implements Cloneable, Comparable<UpdateSite> {

	boolean active;
	private String name;
	private String url;
	private String sshHost;
	private String uploadDirectory;
	private String description;
	private String maintainer;
	private long timestamp;
	int rank;

	public UpdateSite(final String name, String url, final String sshHost, String uploadDirectory,
		final String description, final String maintainer, final long timestamp)
	{
		setName(name);
		setURL(url);
		setUploadDirectory(uploadDirectory);
		setHost(sshHost);
		setDescription(description);
		setMaintainer(maintainer);
		setTimestamp(timestamp);
	}

	public boolean isActive() {
		return active;
	}

	public void setActive(final boolean active) {
		this.active = active;
	}

	public String getName() {
		return name;
	}

	public void setName(final String name) {
		this.name = name;
	}

	public String getURL() {
		return url;
	}

	public void setURL(final String url) {
		if (url == null || url.equals("") || url.endsWith("/")) this.url = url;
		else this.url = url + "/";
	}

	public String getHost() {
		return sshHost;
	}

	public void setHost(final String host) {
		this.sshHost = host;
	}

	public String getUploadDirectory() {
		return uploadDirectory;
	}

	public void setUploadDirectory(final String uploadDirectory) {
		if (uploadDirectory == null || uploadDirectory.equals("") || uploadDirectory.endsWith("/")) this.uploadDirectory = uploadDirectory;
		else this.uploadDirectory = uploadDirectory + "/";
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(final String description) {
		this.description = description;
	}

	public String getMaintainer() {
		return maintainer;
	}

	public void setMaintainer(final String maintainer) {
		this.maintainer = maintainer;
	}

	public long getTimestamp() {
		return timestamp;
	}

	public void setTimestamp(long timestamp) {
		this.timestamp = timestamp;
	}

	public int getRank() {
		return rank;
	}

	@Override
	public Object clone() {
		return new UpdateSite(name, url, sshHost, uploadDirectory, description, maintainer, timestamp);
	}

	public boolean isLastModified(final long lastModified) {
		return timestamp == Long.parseLong(Util.timestamp(lastModified));
	}

	public void setLastModified(final long lastModified) {
		timestamp = Long.parseLong(Util.timestamp(lastModified));
	}

	public boolean isUploadable() {
		return (uploadDirectory != null && !uploadDirectory.equals("")) ||
				(sshHost != null && sshHost.indexOf(':') > 0);
	}

	@Override
	public String toString() {
		return url + (sshHost != null ? ", " + sshHost : "") +
			(uploadDirectory != null ? ", " + uploadDirectory : "");
	}

	@Override
	public int compareTo(UpdateSite other) {
		return rank - other.rank;
	}

	@Override
	public boolean equals(Object other) {
		if (other instanceof UpdateSite)
			return rank == ((UpdateSite)other).rank;
		return false;
	}

	@Override
	public int hashCode() {
		return rank;
	}

	public String getUploadProtocol() {
		if (sshHost == null)
			throw new RuntimeException("Missing upload information for site " + url);
		final int at = sshHost.indexOf('@');
		final int colon = sshHost.indexOf(':');
		if (colon > 0 && (at < 0 || colon < at)) return sshHost.substring(0, colon);
		return "ssh";
	}
}
@@ -2419,20 +2419,17 @@
  String 
-sshH
+h
 ost;%0A%09pr
@@ -3372,20 +3372,17 @@
 %09return 
-sshH
+h
 ost;%0A%09%7D%0A
@@ -3431,20 +3431,17 @@
 %0A%09%09this.
-sshH
+h
 ost = ho
@@ -4319,20 +4319,17 @@
 e, url, 
-sshH
+h
 ost, upl
@@ -4740,20 +4740,17 @@
 %7C%7C%0A%09%09%09%09(
-sshH
+h
 ost != n
@@ -4756,20 +4756,17 @@
 null && 
-sshH
+h
 ost.inde
@@ -4839,20 +4839,17 @@
  url + (
-sshH
+h
 ost != n
@@ -4861,20 +4861,17 @@
  %22, %22 + 
-sshH
+h
 ost : %22%22
@@ -5270,20 +5270,17 @@
 %7B%0A%09%09if (
-sshH
+h
 ost == n
@@ -5378,20 +5378,17 @@
 nt at = 
-sshH
+h
 ost.inde
@@ -5417,20 +5417,17 @@
 colon = 
-sshH
+h
 ost.inde
@@ -5486,20 +5486,17 @@
  return 
-sshH
+h
 ost.subs

rename the 'sshHost' field to 'host'