#!/bin/sh -e

set -u

usage() {
  cat <<__USAGE__
usage: ${0##*/} [<option(s)>] [<file(s)>]
convert TSV to Markdown table.

options:
  -d, --deconvert  convert Markdown table to TSV
      --help       print usage
__USAGE__
}

warn() {
  printf "%s\n" "${0##*/}: $*" >&2
}

deconvert=false
is_help=false
while [ $# -ge 1 ]; do
  opt=$1
  shift
  case ${opt} in
    -d | --deconvert)
      deconvert=true
      ;;
    -d*)
      deconvert=true
      set -- "-${opt#-d}" "$@"
      continue
      ;;
    -h | --help)
      is_help=true
      ;;
    --)
      break
      ;;
    -*)
      warn "unrecognized option '${opt}'"
      exit 1
      ;;
    *)
      set -- "${opt}" "$@"
      break
      ;;
  esac
done
if "${is_help}"; then
  usage
  exit 0
fi

tab='	'
if ${deconvert}; then
  cat -- "$@" \
    | sed '2d' \
    | sed 's/^| //; s/ |$//' \
    | sed 's/  *| /'"${tab}"'/g; s/ *$//'
else
  cat -- "$@" \
    | sed '1{p; s/[^'"${tab}"']/-/g}' \
    | sed 's/^/-'"${tab}"'/; s/$/'"${tab}"'-/' \
    | column -t -s "${tab}" -o ' | ' \
    | sed 's/^- //; s/ -$//' \
    | sed '2s/[^|]/-/g'
fi
