#!/bin/bash
# vim:set ts=2:sw=2:tw=80:et:ai:si:cindent:cino=L0,b1,(1s,U1,m1,j1,J1,)50,*90:cinkeys=0{,0},0),0],\:,!^F,o,O,e,0=break:
# settings from: https://datapax.com.au/code_conventions/
#
#/**********************************************************************
#    Rep(eat) Char(acter)
#    Copyright (C) 2013-2017 Todd Harbour
#
#    This program is free software; you can redistribute it and/or
#    modify it under the terms of the GNU General Public License
#    version 2 ONLY, as published by the Free Software Foundation.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program, in the file COPYING or COPYING.txt; if
#    not, see http://www.gnu.org/licenses/ , or write to:
#      The Free Software Foundation, Inc.,
#      51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
# **********************************************************************/

# repchar
#--------
# repeats a character or series of characters. can be sourced or run.

# we are being run as a script, so set it's name
[ "${0}" = "$(basename "$0")" ] && _repcharbinname="${0##*/}"

# repchar -h|--help
# repchar <char> <count>
repchar() {
  [ "${1}" = "--help" ] || [ "${1}" = "-h" ] && {

    cat <<EOF
Repeats a given character (or string) a specified number of times.

Can be run directly or sourced ( . "${_repcharbinname:-repchar}" ).

Usage: ${_repcharbinname:-repchar} -h|--help
       ${_repcharbinname:-repchar} <char> <count>

-h|--help           - Displays this help
<char>              - Character (or string) to repeat.
<count>             - Number of times to repeat <char>.

Example: ${_repcharbinname:-repchar} = 10
=========
EOF

    return 0
  }
  # expecting 2 params
  [ ${#} -ne 2 ] && {
    echo >&2 "ERROR: Invalid number of parameters"
    return 1
  }

  # if first param is NULL, obviously the output will be nothing
  [ -z "${1}" ] && echo && return 0

  # second param must be a number
  [ "${2}" -eq "${2}" ] >/dev/null 2>&1 || {
    echo >&2 "ERROR: Invalid <num>: ${2}"
    return 1
  }

  # raw that number (strip of extras)
  num=$((${2} + 0))

  # if second param is a zero, obviously the output will be nothing
  [ "${num}" -eq 0 ] && echo && return 0

  # man printf says: the format is reused as necessary to consume all of
  #                  the arguments.
  # given that "%.0s" prints NOTHING, all you're left with is ${num} ${1}'s
  # shellcheck disable=SC2046,SC2312
  printf "%.0s${1}" $(eval echo "{1..${num}}")
  echo
}

# we are being sourced
[ "${0}" != "$(basename "$0")" ] && return 0

# we are being run as a script
repchar "${@}"
exit "$?"
