#!/bin/ash
# *********************************************************************
#  Written by and copyright Carlo Strozzi <carlos@linux.it>.
#
#  filetolist: turn a file into a one-entry NoSQL list.
#  The file content is encoded in base64 format.
#  Copyright (C) 2001 Carlo Strozzi <carlos@linux.it>
#
#  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; either version 2 of the License, or
#  (at your option) any later version.
#
#  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.
#
#  2001-05-24 Alpha code
#
#  $Id$
# *********************************************************************

chomp="0"
chop="0"

while [ $# -gt 0 ]
do
   case $1 in
	-c|--chomp) chomp="1" ;;
	-C|--chop)  chop="1" ;;
	-M|--mime)  shift; mime_defs="$1" ;;
	-h|--help)
	     grep -v '^#' @NOSQLPATH@/nosql/help/filetolist.txt
	     exit 1
	;;
	-*) ;;				# Skip unknown options.
	*) args="$args $1" ;;
   esac
   shift
done

set - $args

if [ $# != 2 ]
then
   echo Usage: filetolist [options] column file
   exit 1
fi

set - $args $mime_defs

mimencode $2 | mawk '
   BEGIN { if (!'$chop') printf("\n")
   if ("'"$4"'" != "") {
   printf("MIME_%s\tMIME-Version: 1.0\n Content-Type: %s; charset=\"%s\"\n Content-Transfer-Encoding: base64\n", "'"$1"'", "'"$3"'", "'"$4"'")
   }}
   NR == 1 { print "'$1'\t" $0 }
   NR > 1 { print " " $0 }
   END { if (!'$chomp') printf("\n") }'

#
# End of program.
#
