#!/usr/bin/env runawk

# Copyright (c) 2010 Aleksey Cheusov <vle@gmx.net>
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

#use "alt_assert.awk"
#use "alt_getopt.awk"
#use "shquote.awk"
#use "embed_str.awk"

#env "LC_ALL=C"

#.begin-str help
# alt_getopt is a command options parser, it gets options
# specification and application's options as arguments and outputs
# shell commands to be executed.
# Usage:
#   alt_getopt -h
#   alt_getopt [OPTIONS] opt1 sh_cmd1 [opt2 sh_cmd2...] -- [args]
# OPTIONS:
#    -h|--help        display this screen
#    -c               do not output 'shift' command, variable
#                     __shifts_cnt is assigned instead
#.end-str

function __takes_arg (opt){
	if (!opt in __getopt_opts)
		return 0
	if (__getopt_opts [opt] == takes_arg)
		return 1
	if (__getopt_opts [opt] == "")
		return 0
	return __getopt_opts [__getopt_opts [opt]] == takes_arg
}

BEGIN {
	start = 1
	if (ARGV [1] == "-h" || ARGV [1] == "--help"){
		print EMBED_STR ["help"] > "/dev/stderr"
		exitnow(0)
	}
	if (ARGV [1] == "-c"){
		delete ARGV [1]
		++start
		output_shift_cnt = 1
	}

	for (i=start; i < ARGC; i += 2){
		if (ARGV [i] == "--"){
			delete ARGV [i]
			++i
			break
		}

		if (ARGV [i] == "" || ARGV [i+1] == ""){
			print "bad arguments, see alt_getopt -h for more information" \
				> "/dev/stderr"
		}

		if (ARGV [i] ~ /^[A-Za-z0-9!@%^=+\/,.]$/){
			shopts = shopts ARGV [i]
			action [ARGV [i]] = ARGV [i+1]
		}else if (ARGV [i] ~ /^=[A-Za-z0-9!@%^=+\/,.]$/){
			opt = substr(ARGV [i], 2)
			shopts = shopts opt ":"
			action [opt] = ARGV [i+1]
		}else if (ARGV [i] ~ /^[A-Za-z0-9!@%^=+\/,.][ |][^ |]+$/){
			split(ARGV [i], arr, /[ |]/)
			shopts = shopts arr [1]
			long_opts [arr [2]] = arr [1]
			action [arr [1]] = ARGV [i+1]
		}else if (ARGV [i] ~ /^=[A-Za-z0-9!@%^=+\/,.][ |][^ |]+$/){
			split(substr(ARGV [i], 2), arr, /[ |]/)
			shopts = shopts arr [1] ":"
			long_opts [arr [2]] = arr [1]
			action [arr [1]] = ARGV [i+1]
		}else if (ARGV [i] ~ /^[^=][^ |]*$/){
			long_opts [ARGV [i]] = ""
			action [ARGV [i]] = ARGV [i+1]
		}else if (ARGV [i] ~ /^=[^ |]+$/){
			opt = substr(ARGV [i], 2)
			long_opts [opt] = takes_arg
			action [opt] = ARGV [i+1]
		}else{
			abort("unknown format of'" ARGV [i] "'")
		}

		delete ARGV [i]
		delete ARGV [i+1]
	}

	first = i

	while (getopt(shopts, 1)){
		if (__takes_arg(optopt))
			print action [optopt] shquote(optarg)
		else
			print action [optopt]
	}

	shifts_cnt = 0
	for (i=first; i < ARGC; ++i){
		if (ARGV [i] == "")
			++shifts_cnt
	}
	if (output_shift_cnt)
		print "shifts=" shifts_cnt
	else
		print "shift", shifts_cnt

	exitnow(0)
}
