#!/bin/sed -f
# =====================================================================
# csa-genkey: build a valid table record key from arbitrary data.
#
# Copyright (c) 2002,2008 Carlo Strozzi
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 dated June, 1991.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# =====================================================================
# $Id: csa-genkey 49 2009-08-03 15:50:05Z carlo $

# Remove leading and trailing blanks and tabs.
s/^[ 	]*//
s/[ 	]*$//

# Squeeze middle blanks and tabs.
s/[	]\+/ /g

# Turn disallowed characters into 'x'.
s/[^A-Za-z0-9]/x/g

# Turn to lower-case what is left.
y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/

# Pad with 'x' if the resulting string is shorter than 2.
/^.\{0,1\}$/{
  s/$/xx/
}

# Trim key to 32 chars max.
s/^\(.\{32\}\).*$/\1/

# Print the actual key.
p

# Spell-out each key character individually, for key-clustering purposes.
s/\(.\)/\1 /g

# Quit at the first newline.
q

# End of program.
