#!/bin/bash
#                       /usr/local/bin/vcardbreak
# https://crystalfaeries.net/posix/bin/vcardbreak
# celeste@crystalfaeries.net VCARDBREAK 2020-02-02 04:24:05+00:00
# break the combined .vcf $1 into individual .vcf files in current directory
dos2unix "${1}"	2>/dev/null	# import from god-knows-what-environment
let linenum=1		# from Beginning of File
let lastfile=-1		# default no last file (we haven't processed any yet)
let filename=0		# from Beginning of contained files
recbegin="BEGIN:VCARD"	# Beginning of contained file
recend="END:VCARD"	# End	    of contained file
inline=""		# the line we're processing of combined .vcf
until [ "${filename}" == "${lastfile}" ] ; do		# yes we found something to process
	until [ "${inline}" == "${recend}" ] ; do	# have non null input
#		tail -n +"$linenum" "$1" | head -n 1 | grep -v PHOTO	>> "${filename}".vcf
		tail -n +"$linenum" "$1" | head -n 1			>> "${filename}".vcf
		let linenum=1+$linenum			# done with line, get next line:
		inline="$(tail -n +${linenum}	"${1}"  | head -n 1)"
		if [ "${inline}" == "" ]
		then
			echo	"${inline}"				>> "${filename}".vcf
			exit 0				# EOf input file OR hit a blank line
		fi
	done	# now output the closing recend
	echo	"${inline}"						>> "${filename}".vcf
	let linenum=1+${linenum}			# next input line
	inline="$(tail -n +${linenum}	"${1}"  | head -n 1)"
	let lastfile=$filename				# track our progress
	let filename=1+${lastfile}			# next output file
done
exit	$?
