18462SApril.Chin@Sun.COM#!/usr/bin/ksh93
28462SApril.Chin@Sun.COM
38462SApril.Chin@Sun.COM#
48462SApril.Chin@Sun.COM# CDDL HEADER START
58462SApril.Chin@Sun.COM#
68462SApril.Chin@Sun.COM# The contents of this file are subject to the terms of the
78462SApril.Chin@Sun.COM# Common Development and Distribution License (the "License").
88462SApril.Chin@Sun.COM# You may not use this file except in compliance with the License.
98462SApril.Chin@Sun.COM#
108462SApril.Chin@Sun.COM# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
118462SApril.Chin@Sun.COM# or http://www.opensolaris.org/os/licensing.
128462SApril.Chin@Sun.COM# See the License for the specific language governing permissions
138462SApril.Chin@Sun.COM# and limitations under the License.
148462SApril.Chin@Sun.COM#
158462SApril.Chin@Sun.COM# When distributing Covered Code, include this CDDL HEADER in each
168462SApril.Chin@Sun.COM# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
178462SApril.Chin@Sun.COM# If applicable, add the following below this CDDL HEADER, with the
188462SApril.Chin@Sun.COM# fields enclosed by brackets "[]" replaced with your own identifying
198462SApril.Chin@Sun.COM# information: Portions Copyright [yyyy] [name of copyright owner]
208462SApril.Chin@Sun.COM#
218462SApril.Chin@Sun.COM# CDDL HEADER END
228462SApril.Chin@Sun.COM#
238462SApril.Chin@Sun.COM
248462SApril.Chin@Sun.COM#
25*10898Sroland.mainz@nrubsig.org# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
268462SApril.Chin@Sun.COM# Use is subject to license terms.
278462SApril.Chin@Sun.COM#
288462SApril.Chin@Sun.COM
298462SApril.Chin@Sun.COM# Solaris needs /usr/xpg6/bin:/usr/xpg4/bin because the tools in /usr/bin are not POSIX-conformant
308462SApril.Chin@Sun.COMexport PATH=/usr/xpg6/bin:/usr/xpg4/bin:/bin:/usr/bin
318462SApril.Chin@Sun.COM
328462SApril.Chin@Sun.COM# Make sure all math stuff runs in the "C" locale to avoid problems
338462SApril.Chin@Sun.COM# with alternative # radix point representations (e.g. ',' instead of
348462SApril.Chin@Sun.COM# '.' in de_DE.*-locales). This needs to be set _before_ any
358462SApril.Chin@Sun.COM# floating-point constants are defined in this script).
368462SApril.Chin@Sun.COMif [[ "${LC_ALL}" != "" ]] ; then
378462SApril.Chin@Sun.COM    export \
388462SApril.Chin@Sun.COM        LC_MONETARY="${LC_ALL}" \
398462SApril.Chin@Sun.COM        LC_MESSAGES="${LC_ALL}" \
408462SApril.Chin@Sun.COM        LC_COLLATE="${LC_ALL}" \
418462SApril.Chin@Sun.COM        LC_CTYPE="${LC_ALL}"
428462SApril.Chin@Sun.COM        unset LC_ALL
438462SApril.Chin@Sun.COMfi
448462SApril.Chin@Sun.COMexport LC_NUMERIC=C
458462SApril.Chin@Sun.COM
468462SApril.Chin@Sun.COMfunction fatal_error
478462SApril.Chin@Sun.COM{
488462SApril.Chin@Sun.COM	print -u2 "${progname}: $*"
498462SApril.Chin@Sun.COM	exit 1
508462SApril.Chin@Sun.COM}
518462SApril.Chin@Sun.COM
528462SApril.Chin@Sun.COMfunction encode_multipart_form_data
538462SApril.Chin@Sun.COM{
548462SApril.Chin@Sun.COM	nameref formdata="$1"
558462SApril.Chin@Sun.COM	nameref content="formdata.content"
568462SApril.Chin@Sun.COM	integer numformelements=${#formdata.form[*]}
578462SApril.Chin@Sun.COM	integer i
588462SApril.Chin@Sun.COM	typeset tmp
598462SApril.Chin@Sun.COM
608462SApril.Chin@Sun.COM	content=""
618462SApril.Chin@Sun.COM
628462SApril.Chin@Sun.COM	# todo: add support to upload files
638462SApril.Chin@Sun.COM	for (( i=0 ; i < numformelements ; i++ )) ; do
648462SApril.Chin@Sun.COM		nameref element="formdata.form[${i}]"
658462SApril.Chin@Sun.COM
668462SApril.Chin@Sun.COM		content+="--${formdata.boundary}\n"
678462SApril.Chin@Sun.COM		content+="Content-Disposition: form-data; name=\"${element.name}\"\n"
688462SApril.Chin@Sun.COM		content+="\n"
698462SApril.Chin@Sun.COM		# make sure we quote the '\' properly since we pass these data to one instance of
708462SApril.Chin@Sun.COM		# "print" when putting the content on the wire.
718462SApril.Chin@Sun.COM		content+="${element.data//\\/\\\\}\n" # fixme: may need encoding for non-ASCII data
728462SApril.Chin@Sun.COM	done
738462SApril.Chin@Sun.COM
748462SApril.Chin@Sun.COM	# we have to de-quote the content before we can count the real numer of bytes in the payload
758462SApril.Chin@Sun.COM	tmp="$(print -- "${content}")"
768462SApril.Chin@Sun.COM	formdata.content_length=${#tmp}
778462SApril.Chin@Sun.COM
788462SApril.Chin@Sun.COM	# add content tail (which MUST not be added to the content length)
798462SApril.Chin@Sun.COM	content+="--${formdata.boundary}--\n"
808462SApril.Chin@Sun.COM
818462SApril.Chin@Sun.COM	return 0
828462SApril.Chin@Sun.COM}
838462SApril.Chin@Sun.COM
848462SApril.Chin@Sun.COM# parse HTTP return code, cookies etc.
858462SApril.Chin@Sun.COMfunction parse_http_response
868462SApril.Chin@Sun.COM{
878462SApril.Chin@Sun.COM	nameref response="$1"
888462SApril.Chin@Sun.COM	typeset h statuscode statusmsg i
898462SApril.Chin@Sun.COM
908462SApril.Chin@Sun.COM	# we use '\r' as additional IFS to filter the final '\r'
918462SApril.Chin@Sun.COM	IFS=$' \t\r' read -r h statuscode statusmsg  # read HTTP/1.[01] <code>
928462SApril.Chin@Sun.COM	[[ "$h" != ~(Eil)HTTP/.* ]]         && { print -u2 -f $"%s: HTTP/ header missing\n" "$0" ; return 1 ; }
938462SApril.Chin@Sun.COM	[[ "$statuscode" != ~(Elr)[0-9]* ]] && { print -u2 -f $"%s: invalid status code\n"  "$0" ; return 1 ; }
948462SApril.Chin@Sun.COM	response.statuscode="$statuscode"
958462SApril.Chin@Sun.COM	response.statusmsg="$statusmsg"
968462SApril.Chin@Sun.COM
978462SApril.Chin@Sun.COM	# skip remaining headers
988462SApril.Chin@Sun.COM	while IFS='' read -r i ; do
998462SApril.Chin@Sun.COM		[[ "$i" == $'\r' ]] && break
1008462SApril.Chin@Sun.COM
1018462SApril.Chin@Sun.COM		# strip '\r' at the end
1028462SApril.Chin@Sun.COM		i="${i/~(Er)$'\r'/}"
1038462SApril.Chin@Sun.COM
1048462SApril.Chin@Sun.COM		case "$i" in
1058462SApril.Chin@Sun.COM			~(Eli)Content-Type:.*)
1068462SApril.Chin@Sun.COM				response.content_type="${i/~(El).*:[[:blank:]]*/}"
1078462SApril.Chin@Sun.COM				;;
1088462SApril.Chin@Sun.COM			~(Eli)Content-Length:[[:blank:]]*[0-9]*)
1098462SApril.Chin@Sun.COM				integer response.content_length="${i/~(El).*:[[:blank:]]*/}"
1108462SApril.Chin@Sun.COM				;;
1118462SApril.Chin@Sun.COM			~(Eli)Transfer-Encoding:.*)
1128462SApril.Chin@Sun.COM				response.transfer_encoding="${i/~(El).*:[[:blank:]]*/}"
1138462SApril.Chin@Sun.COM				;;
1148462SApril.Chin@Sun.COM		esac
1158462SApril.Chin@Sun.COM	done
1168462SApril.Chin@Sun.COM
1178462SApril.Chin@Sun.COM	return 0
1188462SApril.Chin@Sun.COM}
1198462SApril.Chin@Sun.COM
1208462SApril.Chin@Sun.COMfunction cat_http_body
1218462SApril.Chin@Sun.COM{
1228462SApril.Chin@Sun.COM	typeset emode="$1"
1238462SApril.Chin@Sun.COM	typeset hexchunksize="0"
1248462SApril.Chin@Sun.COM	integer chunksize=0
1258462SApril.Chin@Sun.COM
1268462SApril.Chin@Sun.COM	if [[ "${emode}" == "chunked" ]] ; then
1278462SApril.Chin@Sun.COM		while IFS=$'\r' read hexchunksize &&
1288462SApril.Chin@Sun.COM			[[ "${hexchunksize}" == ~(Elri)[0-9abcdef]* ]] &&
1298462SApril.Chin@Sun.COM			(( chunksize=16#${hexchunksize} )) && (( chunksize > 0 )) ; do
1308462SApril.Chin@Sun.COM			dd bs=1 count="${chunksize}" 2>/dev/null
1318462SApril.Chin@Sun.COM		done
1328462SApril.Chin@Sun.COM	else
1338462SApril.Chin@Sun.COM		cat
1348462SApril.Chin@Sun.COM	fi
1358462SApril.Chin@Sun.COM
1368462SApril.Chin@Sun.COM	return 0
1378462SApril.Chin@Sun.COM}
1388462SApril.Chin@Sun.COM
1398462SApril.Chin@Sun.COMfunction history_write_record
1408462SApril.Chin@Sun.COM{
1418462SApril.Chin@Sun.COM	# rec: history record:
1428462SApril.Chin@Sun.COM	#     rec.title
1438462SApril.Chin@Sun.COM	#     rec.description
1448462SApril.Chin@Sun.COM	#     rec.provider
1458462SApril.Chin@Sun.COM	#     rec.providertoken
1468462SApril.Chin@Sun.COM	#     rec.url
1478462SApril.Chin@Sun.COM	nameref rec="$1"
1488462SApril.Chin@Sun.COM	integer histfd
1498462SApril.Chin@Sun.COM
1508462SApril.Chin@Sun.COM	mkdir -p "${HOME}/.shnote"
1518462SApril.Chin@Sun.COM
1528462SApril.Chin@Sun.COM	{
1538462SApril.Chin@Sun.COM		# write a single-line record which can be read
1548462SApril.Chin@Sun.COM		# as a compound variable back into the shell
1558462SApril.Chin@Sun.COM		printf "title=%q description=%q date=%q provider=%q providertoken=%q url=%q\n" \
1568462SApril.Chin@Sun.COM			"${rec.title}" \
1578462SApril.Chin@Sun.COM			"${rec.description}" \
1588462SApril.Chin@Sun.COM			"$(date)" \
1598462SApril.Chin@Sun.COM			"${rec.provider}" \
1608462SApril.Chin@Sun.COM			"${rec.providertoken}" \
1618462SApril.Chin@Sun.COM			"${rec.url}"
1628462SApril.Chin@Sun.COM	} >>"${history_file}"
1638462SApril.Chin@Sun.COM
1648462SApril.Chin@Sun.COM	return $?
1658462SApril.Chin@Sun.COM}
1668462SApril.Chin@Sun.COM
1678462SApril.Chin@Sun.COMfunction print_history
1688462SApril.Chin@Sun.COM{
1698462SApril.Chin@Sun.COM	integer histfd # http stream number
1708462SApril.Chin@Sun.COM	typeset line
1718462SApril.Chin@Sun.COM
1728462SApril.Chin@Sun.COM	(( $# != 0 && $# != 1 )) && { print -u2 -f $"%s: Wrong number of arguments.\n" "$0" ; return 1 ; }
1738462SApril.Chin@Sun.COM
1748462SApril.Chin@Sun.COM	# default output format is:
1758462SApril.Chin@Sun.COM	# <access url>/<title> <date> <access url>
1768462SApril.Chin@Sun.COM	[[ "$1" == "-l" ]] || printf "# %s\t\t\t\t\t%s\t%s\n" "<url>" "<title>" "<date>"
1778462SApril.Chin@Sun.COM
1788462SApril.Chin@Sun.COM	# no history file ?
1798462SApril.Chin@Sun.COM	if [[ ! -f "${history_file}" ]] ; then
1808462SApril.Chin@Sun.COM		return 0
1818462SApril.Chin@Sun.COM	fi
1828462SApril.Chin@Sun.COM
1838462SApril.Chin@Sun.COM	# open history file
184*10898Sroland.mainz@nrubsig.org	redirect {histfd}<> "${history_file}"
185*10898Sroland.mainz@nrubsig.org	(( $? != 0 )) && { print -u2 "Could not open history file." ;  return 1 ; }
1868462SApril.Chin@Sun.COM
1878462SApril.Chin@Sun.COM	while read -u${histfd} line ; do
188*10898Sroland.mainz@nrubsig.org		compound rec
1898462SApril.Chin@Sun.COM
1908462SApril.Chin@Sun.COM		printf "( %s )\n" "${line}"  | read -C rec
1918462SApril.Chin@Sun.COM
1928462SApril.Chin@Sun.COM		if [[ "$1" == "-l" ]] ; then
1938462SApril.Chin@Sun.COM			print -- "${rec}"
1948462SApril.Chin@Sun.COM		else
1958462SApril.Chin@Sun.COM			printf "%q\t%q\t%q\n" "${rec.url}" "${rec.title}" "${rec.date}"
1968462SApril.Chin@Sun.COM		fi
1978462SApril.Chin@Sun.COM
1988462SApril.Chin@Sun.COM		unset rec
1998462SApril.Chin@Sun.COM	done
2008462SApril.Chin@Sun.COM
2018462SApril.Chin@Sun.COM	# close history file
2028462SApril.Chin@Sun.COM	redirect {histfd}<&-
2038462SApril.Chin@Sun.COM
2048462SApril.Chin@Sun.COM	return 0
2058462SApril.Chin@Sun.COM}
2068462SApril.Chin@Sun.COM
2078462SApril.Chin@Sun.COMfunction put_note_pastebin_ca
2088462SApril.Chin@Sun.COM{
2098462SApril.Chin@Sun.COM	# key to autheticate this script against pastebin.ca
2108462SApril.Chin@Sun.COM	typeset -r pastebin_ca_key="9CFXFyeNC3iga/vthok75kTBu5kSSLPD"
2118462SApril.Chin@Sun.COM	# site setup
2128462SApril.Chin@Sun.COM	typeset url_host="opensolaris.pastebin.ca"
2138462SApril.Chin@Sun.COM	typeset url_path="/quiet-paste.php?api=${pastebin_ca_key}"
2148462SApril.Chin@Sun.COM	typeset url="http://${url_host}${url_path}"
2158462SApril.Chin@Sun.COM	integer netfd # http stream number
216*10898Sroland.mainz@nrubsig.org	compound httpresponse
2178462SApril.Chin@Sun.COM
2188462SApril.Chin@Sun.COM	(( $# != 1 )) && { print -u2 -f $"%s: Wrong number of arguments.\n" "$0" ; return 1 ; }
2198462SApril.Chin@Sun.COM	(( ${#1} == 0 )) && { print -u2 -f $"%s: No data.\n" "$0" ; return 1 ; }
2208462SApril.Chin@Sun.COM
2218462SApril.Chin@Sun.COM	# argument for "encode_multipart_form_data"
222*10898Sroland.mainz@nrubsig.org	compound mimeform=(
2238462SApril.Chin@Sun.COM		# input
2248462SApril.Chin@Sun.COM		typeset boundary
2258462SApril.Chin@Sun.COM		typeset -a form
2268462SApril.Chin@Sun.COM		# output
2278462SApril.Chin@Sun.COM		typeset content
2288462SApril.Chin@Sun.COM		integer content_length
2298462SApril.Chin@Sun.COM	)
2308462SApril.Chin@Sun.COM
2318462SApril.Chin@Sun.COM	typeset request=""
2328462SApril.Chin@Sun.COM	typeset content=""
2338462SApril.Chin@Sun.COM
2348462SApril.Chin@Sun.COM	typeset -r boundary="--------shnote_${RANDOM}_Xfish_${RANDOM}_Yeats_${RANDOM}_Zchicken_${RANDOM}monster_--------"
2358462SApril.Chin@Sun.COM
2368462SApril.Chin@Sun.COM	mimeform.boundary="${boundary}"
2378462SApril.Chin@Sun.COM	mimeform.form=( # we use explicit index numbers since we rely on them below when filling the history
2388462SApril.Chin@Sun.COM		[0]=( name="name"		data="${LOGNAME}" )
2398462SApril.Chin@Sun.COM		[1]=( name="expiry"		data="Never" )
2408462SApril.Chin@Sun.COM		[2]=( name="type"		data="1" )
2418462SApril.Chin@Sun.COM		[3]=( name="description"	data="logname=${LOGNAME};hostname=$(hostname);date=$(date)" )
2428462SApril.Chin@Sun.COM		[4]=( name="content"		data="$1" )
2438462SApril.Chin@Sun.COM	)
2448462SApril.Chin@Sun.COM	encode_multipart_form_data mimeform
2458462SApril.Chin@Sun.COM
2468462SApril.Chin@Sun.COM	content="${mimeform.content}"
2478462SApril.Chin@Sun.COM
2488462SApril.Chin@Sun.COM	request="POST ${url_path} HTTP/1.1\r\n"
2498462SApril.Chin@Sun.COM	request+="Host: ${url_host}\r\n"
2508462SApril.Chin@Sun.COM	request+="User-Agent: ${http_user_agent}\r\n"
2518462SApril.Chin@Sun.COM	request+="Connection: close\r\n"
2528462SApril.Chin@Sun.COM	request+="Content-Type: multipart/form-data; boundary=${boundary}\r\n"
2538462SApril.Chin@Sun.COM	request+="Content-Length: $(( mimeform.content_length ))\r\n"
2548462SApril.Chin@Sun.COM
255*10898Sroland.mainz@nrubsig.org	redirect {netfd}<> "/dev/tcp/${url_host}/80"
256*10898Sroland.mainz@nrubsig.org	(( $? != 0 )) && { print -u2 -f $"%s: Could not open connection to %s.\n" "$0" "${url_host}" ;  return 1 ; }
2578462SApril.Chin@Sun.COM
2588462SApril.Chin@Sun.COM	# send http post
2598462SApril.Chin@Sun.COM	{
2608462SApril.Chin@Sun.COM		print -n -- "${request}\r\n"
2618462SApril.Chin@Sun.COM		print -n -- "${content}\r\n"
2628462SApril.Chin@Sun.COM	}  >&${netfd}
2638462SApril.Chin@Sun.COM
2648462SApril.Chin@Sun.COM	# process reply
2658462SApril.Chin@Sun.COM	parse_http_response httpresponse <&${netfd}
2668462SApril.Chin@Sun.COM	response="$(cat_http_body "${httpresponse.transfer_encoding}" <&${netfd})"
2678462SApril.Chin@Sun.COM
2688462SApril.Chin@Sun.COM	# close connection
2698462SApril.Chin@Sun.COM	redirect {netfd}<&-
2708462SApril.Chin@Sun.COM
2718462SApril.Chin@Sun.COM	if [[ "${response}" == ~(E).*SUCCESS.* ]] ; then
2728462SApril.Chin@Sun.COM		typeset response_token="${response/~(E).*SUCCESS:/}"
2738462SApril.Chin@Sun.COM
2748462SApril.Chin@Sun.COM		printf "SUCCESS: http://opensolaris.pastebin.ca/%s\n" "${response_token}"
2758462SApril.Chin@Sun.COM
2768462SApril.Chin@Sun.COM		# write history entry
277*10898Sroland.mainz@nrubsig.org		compound histrec=(
2788462SApril.Chin@Sun.COM			title="${mimeform.form[0].data}"
2798462SApril.Chin@Sun.COM			description="${mimeform.form[3].data}"
2808462SApril.Chin@Sun.COM			providertoken="${response_token}"
2818462SApril.Chin@Sun.COM			provider="opensolaris.pastebin.ca"
2828462SApril.Chin@Sun.COM			url="http://opensolaris.pastebin.ca/${response_token}"
2838462SApril.Chin@Sun.COM		)
2848462SApril.Chin@Sun.COM
2858462SApril.Chin@Sun.COM		history_write_record histrec
2868462SApril.Chin@Sun.COM		return 0
2878462SApril.Chin@Sun.COM	else
2888462SApril.Chin@Sun.COM		printf "ERROR: %s\n" "${response}"
2898462SApril.Chin@Sun.COM		return 1
2908462SApril.Chin@Sun.COM	fi
2918462SApril.Chin@Sun.COM
2928462SApril.Chin@Sun.COM	# not reached
2938462SApril.Chin@Sun.COM}
2948462SApril.Chin@Sun.COM
2958462SApril.Chin@Sun.COMfunction get_note_pastebin_ca
2968462SApril.Chin@Sun.COM{
2978462SApril.Chin@Sun.COM	typeset recordname="$1"
2988462SApril.Chin@Sun.COM	integer netfd # http stream number
2998462SApril.Chin@Sun.COM
3008462SApril.Chin@Sun.COM	(( $# != 1 )) && { print -u2 -f $"%s: No key or key URL.\n" "$0" ; return 1 ; }
3018462SApril.Chin@Sun.COM
3028462SApril.Chin@Sun.COM	case "${recordname}" in
3038462SApril.Chin@Sun.COM		~(Elr)[0-9][0-9]*)
3048462SApril.Chin@Sun.COM			# pass-through
3058462SApril.Chin@Sun.COM			;;
3068462SApril.Chin@Sun.COM		~(Elr)http://opensolaris.pastebin.ca/raw/[0-9]*)
3078462SApril.Chin@Sun.COM			recordname="${recordname/~(El)http:\/\/opensolaris.pastebin.ca\/raw\//}"
3088462SApril.Chin@Sun.COM			;;
3098462SApril.Chin@Sun.COM		~(Elr)http://opensolaris.pastebin.ca/[0-9]*)
3108462SApril.Chin@Sun.COM			recordname="${recordname/~(El)http:\/\/opensolaris.pastebin.ca\//}"
3118462SApril.Chin@Sun.COM			;;
3128462SApril.Chin@Sun.COM		*)
3138462SApril.Chin@Sun.COM			fatal_error $"Unsupported record name ${recordname}."
3148462SApril.Chin@Sun.COM	esac
3158462SApril.Chin@Sun.COM
3168462SApril.Chin@Sun.COM	print -u2 -f "# Record name is '%s'\n" "${recordname}"
3178462SApril.Chin@Sun.COM
3188462SApril.Chin@Sun.COM	typeset url_host="opensolaris.pastebin.ca"
3198462SApril.Chin@Sun.COM	typeset url_path="/raw/${recordname}"
3208462SApril.Chin@Sun.COM	typeset url="http://${url_host}${url_path}"
3218462SApril.Chin@Sun.COM	# I hereby curse Solaris for not having an entry for "http" in /etc/services
3228462SApril.Chin@Sun.COM
3238462SApril.Chin@Sun.COM	# open TCP channel
324*10898Sroland.mainz@nrubsig.org	redirect {netfd}<> "/dev/tcp/${url_host}/80"
325*10898Sroland.mainz@nrubsig.org	(( $? != 0 )) && { print -u2 -f $"%s: Could not open connection to %s.\n" "$0" "${url_host}" ; return 1 ; }
3268462SApril.Chin@Sun.COM
3278462SApril.Chin@Sun.COM	# send HTTP request
3288462SApril.Chin@Sun.COM	request="GET ${url_path} HTTP/1.1\r\n"
3298462SApril.Chin@Sun.COM	request+="Host: ${url_host}\r\n"
3308462SApril.Chin@Sun.COM	request+="User-Agent: ${http_user_agent}\r\n"
3318462SApril.Chin@Sun.COM	request+="Connection: close\r\n"
3328462SApril.Chin@Sun.COM	print -u${netfd} -- "${request}\r\n"
3338462SApril.Chin@Sun.COM
3348462SApril.Chin@Sun.COM	# collect response and send it to stdout
3358462SApril.Chin@Sun.COM	parse_http_response httpresponse <&${netfd}
3368462SApril.Chin@Sun.COM	cat_http_body "${httpresponse.transfer_encoding}" <&${netfd}
3378462SApril.Chin@Sun.COM
3388462SApril.Chin@Sun.COM	# close connection
3398462SApril.Chin@Sun.COM	redirect {netfd}<&-
3408462SApril.Chin@Sun.COM
3418462SApril.Chin@Sun.COM	print # add newline
3428462SApril.Chin@Sun.COM
3438462SApril.Chin@Sun.COM	return 0
3448462SApril.Chin@Sun.COM}
3458462SApril.Chin@Sun.COM
3468462SApril.Chin@Sun.COMfunction usage
3478462SApril.Chin@Sun.COM{
3488462SApril.Chin@Sun.COM	OPTIND=0
3498462SApril.Chin@Sun.COM	getopts -a "${progname}" "${USAGE}" OPT '-?'
3508462SApril.Chin@Sun.COM	exit 2
3518462SApril.Chin@Sun.COM}
3528462SApril.Chin@Sun.COM
3538462SApril.Chin@Sun.COM# program start
3548462SApril.Chin@Sun.COMbuiltin basename
3558462SApril.Chin@Sun.COMbuiltin cat
3568462SApril.Chin@Sun.COMbuiltin date
3578462SApril.Chin@Sun.COMbuiltin uname
3588462SApril.Chin@Sun.COM
3598462SApril.Chin@Sun.COMtypeset progname="${ basename "${0}" ; }"
3608462SApril.Chin@Sun.COM
3618462SApril.Chin@Sun.COM# HTTP protocol client identifer
362*10898Sroland.mainz@nrubsig.orgtypeset -r http_user_agent="shnote/ksh93 (2009-05-09; $(uname -s -r -p))"
3638462SApril.Chin@Sun.COM
3648462SApril.Chin@Sun.COM# name of history log (the number after "history" is some kind of version
3658462SApril.Chin@Sun.COM# counter to handle incompatible changes to the history file format)
3668462SApril.Chin@Sun.COMtypeset -r history_file="${HOME}/.shnote/history0.txt"
3678462SApril.Chin@Sun.COM
3688462SApril.Chin@Sun.COMtypeset -r shnote_usage=$'+
369*10898Sroland.mainz@nrubsig.org[-?\n@(#)\$Id: shnote (Roland Mainz) 2009-05-09 \$\n]
3708462SApril.Chin@Sun.COM[-author?Roland Mainz <roland.mainz@nrubsig.org>]
3718462SApril.Chin@Sun.COM[+NAME?shnote - read/write text data to internet clipboards]
3728462SApril.Chin@Sun.COM[+DESCRIPTION?\bshnote\b is a small utilty which can read and write text
3738462SApril.Chin@Sun.COM	data to internet "clipboards" such as opensolaris.pastebin.ca.]
3748462SApril.Chin@Sun.COM[+?The first arg \bmethod\b describes one of the methods, "put" saves a string
3758462SApril.Chin@Sun.COM	to the internet clipboard, returning an identifer and the full URL
3768462SApril.Chin@Sun.COM	where the data are stored. The method "get" retrives the raw
3778462SApril.Chin@Sun.COM	information using the identifer from the previous "put" action.
3788462SApril.Chin@Sun.COM	The method "hist" prints a history of transactions created with the
3798462SApril.Chin@Sun.COM	"put" method and the keys to retrive them again using the "get" method.]
3808462SApril.Chin@Sun.COM[+?The second arg \bstring\b contains either the string data which should be
3818462SApril.Chin@Sun.COM	stored on the clipboard using the "put" method, the "get" method uses
3828462SApril.Chin@Sun.COM	this information as identifer to retrive the raw data from the
3838462SApril.Chin@Sun.COM	clipboard.]
3848462SApril.Chin@Sun.COM
3858462SApril.Chin@Sun.COMmethod [ string ]
3868462SApril.Chin@Sun.COM
3878462SApril.Chin@Sun.COM[+SEE ALSO?\bksh93\b(1), \brssread\b(1), \bshtwitter\b(1), \bshtinyurl\b(1), http://opensolaris.pastebin.ca]
3888462SApril.Chin@Sun.COM'
3898462SApril.Chin@Sun.COM
3908462SApril.Chin@Sun.COMwhile getopts -a "${progname}" "${shnote_usage}" OPT ; do
3918462SApril.Chin@Sun.COM#	printmsg "## OPT=|${OPT}|, OPTARG=|${OPTARG}|"
3928462SApril.Chin@Sun.COM	case ${OPT} in
3938462SApril.Chin@Sun.COM		*)	usage ;;
3948462SApril.Chin@Sun.COM	esac
3958462SApril.Chin@Sun.COMdone
3968462SApril.Chin@Sun.COMshift $((OPTIND-1))
3978462SApril.Chin@Sun.COM
3988462SApril.Chin@Sun.COM# expecting at least one more argument, the single method below will do
3998462SApril.Chin@Sun.COM# the checks for more arguments if needed ("put" and "get" methods need
4008462SApril.Chin@Sun.COM# at least one extra argument, "hist" none).
4018462SApril.Chin@Sun.COM(($# >= 1)) || usage
4028462SApril.Chin@Sun.COM
4038462SApril.Chin@Sun.COMtypeset method="$1"
4048462SApril.Chin@Sun.COMshift
4058462SApril.Chin@Sun.COM
4068462SApril.Chin@Sun.COMcase "${method}" in
4078462SApril.Chin@Sun.COM	put)	put_note_pastebin_ca "$@" ; exit $? ;;
4088462SApril.Chin@Sun.COM	get)	get_note_pastebin_ca "$@" ; exit $? ;;
4098462SApril.Chin@Sun.COM	hist)	print_history "$@"        ; exit $? ;;
4108462SApril.Chin@Sun.COM	*)	usage ;;
4118462SApril.Chin@Sun.COMesac
4128462SApril.Chin@Sun.COM
4138462SApril.Chin@Sun.COMfatal_error $"not reached."
4148462SApril.Chin@Sun.COM# EOF.
415