#!/usr/pkg/bin/bash

help() {
	{
		echo ""
		echo "Usage: $0 [--exec] [--prefix pf] args.."
		echo "       $0 --build modname.so cppsources.."
		echo ""
		echo "Replacement args:"
		echo "    --cxx         g++"
		echo "    --cxxflags    $( echo '-O2 -I/usr/include -I/usr/pkg/include/python3.12 -I/usr/pkg/include -Wall -Wextra -ggdb -I"/usr/pkg/share/yosys/include" -MD -MP -D_YOSYS_ -fPIC -I/usr/pkg/include -std=c++17 -O3 -DYOSYS_ENABLE_READLINE -I/usr/local/include -I/usr/pkg/include -DYOSYS_ENABLE_PLUGINS -DYOSYS_ENABLE_GLOB -DYOSYS_ENABLE_ZLIB -I/usr/pkg/include -DYOSYS_ENABLE_TCL -DYOSYS_ENABLE_ABC -DYOSYS_ENABLE_COVER' | fmt -w60 | sed ':a;N;$!ba;s/\n/ \\\n                      /g' )"
		echo "    --linkflags   -rdynamic"
		echo "    --ldflags     (alias of --linkflags)"
		echo "    --libs        -lstdc++ -lm -lrt -lreadline -Wl,-R/usr/pkg/lib -L/usr/pkg/lib -lffi -lz -Wl,-R/usr/pkg/lib -L/usr/pkg/lib -ltcl86 -ltclstub86"
		echo "    --ldlibs      (alias of --libs)"
		echo "    --bindir      /usr/pkg/bin"
		echo "    --datdir      /usr/pkg/share/yosys"
		echo ""
		echo "All other args are passed through as they are."
		echo ""
		echo "Use --exec to call a command instead of generating output. Example usage:"
		echo ""
		echo "  $0 --exec --cxx --cxxflags --ldflags -o plugin.so -shared plugin.cc --libs"
		echo ""
		echo "The above command can be abbreviated as:"
		echo ""
		echo "  $0 --build plugin.so plugin.cc"
		echo ""
		echo "Use --prefix to change the prefix for the special args from '--' to"
		echo "something else. Example:"
		echo ""
		echo "  $0 --prefix @ bindir: @bindir"
		echo ""
		echo "The args --bindir and --datdir can be directly followed by a slash and"
		echo "additional text. Example:"
		echo ""
		echo "  $0 --datdir/simlib.v"
		echo ""
	} >&2
	exit 1
}

if [ $# -eq 0 ]; then
	help
fi

if [ "$1" = "--build" ]; then
	modname="$2"; shift 2
	set -- --exec --cxx --cxxflags --ldflags -o "$modname" -shared "$@" --libs
fi

prefix="--"
get_prefix=false
exec_mode=false
declare -a tokens=()

for opt; do
	if $get_prefix; then
		prefix="$opt"
		get_prefix=false
		continue
	fi
	case "$opt" in
		"$prefix"cxx)
			tokens=( "${tokens[@]}"  g++       ) ;;
		"$prefix"cxxflags)
			tokens=( "${tokens[@]}"  -O2 -I/usr/include -I/usr/pkg/include/python3.12 -I/usr/pkg/include -Wall -Wextra -ggdb -I"/usr/pkg/share/yosys/include" -MD -MP -D_YOSYS_ -fPIC -I/usr/pkg/include -std=c++17 -O3 -DYOSYS_ENABLE_READLINE -I/usr/local/include -I/usr/pkg/include -DYOSYS_ENABLE_PLUGINS -DYOSYS_ENABLE_GLOB -DYOSYS_ENABLE_ZLIB -I/usr/pkg/include -DYOSYS_ENABLE_TCL -DYOSYS_ENABLE_ABC -DYOSYS_ENABLE_COVER  ) ;;
		"$prefix"linkflags)
			tokens=( "${tokens[@]}"  -rdynamic   ) ;;
		"$prefix"libs)
			tokens=( "${tokens[@]}"  -lstdc++ -lm -lrt -lreadline -Wl,-R/usr/pkg/lib -L/usr/pkg/lib -lffi -lz -Wl,-R/usr/pkg/lib -L/usr/pkg/lib -ltcl86 -ltclstub86    ) ;;
		"$prefix"ldflags)
			tokens=( "${tokens[@]}"  -rdynamic   ) ;;
		"$prefix"ldlibs)
			tokens=( "${tokens[@]}"  -lstdc++ -lm -lrt -lreadline -Wl,-R/usr/pkg/lib -L/usr/pkg/lib -lffi -lz -Wl,-R/usr/pkg/lib -L/usr/pkg/lib -ltcl86 -ltclstub86    ) ;;
		"$prefix"bindir)
			tokens=( "${tokens[@]}" '/usr/pkg/bin'   ) ;;
		"$prefix"datdir)
			tokens=( "${tokens[@]}" '/usr/pkg/share/yosys'   ) ;;
		"$prefix"bindir/*)
			tokens=( "${tokens[@]}" '/usr/pkg/bin'"${opt#${prefix}bindir}" ) ;;
		"$prefix"datdir/*)
			tokens=( "${tokens[@]}" '/usr/pkg/share/yosys'"${opt#${prefix}datdir}" ) ;;
		--help|-\?|-h)
			if [ ${#tokens[@]} -eq 0 ]; then
				help
			else
				tokens=( "${tokens[@]}" "$opt" )
			fi ;;
		--exec)
			if [ ${#tokens[@]} -eq 0 ]; then
				exec_mode=true
			else
				tokens=( "${tokens[@]}" "$opt" )
			fi ;;
		--prefix)
			if [ ${#tokens[@]} -eq 0 ]; then
				get_prefix=true
			else
				tokens=( "${tokens[@]}" "$opt" )
			fi ;;
		*)
			tokens=( "${tokens[@]}" "$opt" )
	esac
done

if $exec_mode; then
	exec "${tokens[@]}"
fi

echo "${tokens[@]}"
exit 0
