#!/usr/local/bin/perl
#
# Copyright (C) Zeus Technology Ltd. 1998
#
# Subserver website hashing
#
# Input, hostname to hash (e.g. www.mysite.com)
# Output, hashed hostname  (e.g. ??/??/www.mysite.com) where ?? are 
# selected from [A-Za-z0-9-_]
#
# This software is provided "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 
# Zeus Technology Ltd. be liable for any direct, indirect, incidental,
# special, exemplary, or consequential damaged (including, but not
# limited to, procurement of substitute good or services; loss of use,
# data, or profits; or business interruption) however caused and on
# 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

$site = shift || die "Usage: subserver_hash website\n";
print hash2( $site ) . "\n";
exit 0;

# ----------------------------------------------------------
# Returns a hashed version of the website.

sub hash2
{
    my $site = shift;
    my $h = 0;
    my $res = "";

    my @prefix = ( 'x','x','/','x','x','/' );
    my @conv   = (
		  'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
		  'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 
		  'Y', 'Z',
		  'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
		  'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
		  'y', 'z',
		  '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
		  '-', '_'
		  );

    my @chars = split //, $site;

    use integer;
    my $i;
    for( $i = 0; $i < $#chars + 1; $i++ ) {
	$h *= 129;
	$h += ord( $chars[$i] );
	$h += 987654321;
    }
    if( $h == 0 ) { $h = 1; }

    $prefix[0] = $conv[$h & 0x3f]; $h = $h >> 6;
    $prefix[1] = $conv[$h & 0x3f]; $h = $h >> 6;
    $prefix[3] = $conv[$h & 0x3f]; $h = $h >> 6;
    $prefix[4] = $conv[$h & 0x3f];

    return (join '', @prefix) . "$site";
}

