# -*- coding: utf-8 -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
import ulib.base.i_need_py23

__all__ = ('ShTempl',)

from ulib.base.lines import Lines
from ulib.base.uio import UTF8, LATIN1, LATIN9
from base import FileTempl, register_templ

class ShTempl(FileTempl):
    NAME = ('shell', 'sh')
    EXT = '.sh'

    _executable = True
    shell = u'/bin/bash'
    ulib = True

    PROPERTIES, ALIASES, OPTIONS = \
        FileTempl.DECL_P(FileTempl, 'executable', 'noexec', 'shell', 'ulib', 'noulib',
                         executable=dict(initial=_executable, value=True, aliases=('exec', 'x')),
                         noexec=dict(varname='executable', value=False, aliases='n'),
                         shell=dict(initial=shell),
                         ulib=dict(initial=ulib, value=True),
                         noulib=dict(varname='ulib', value=False, aliases='c'),
                         )

    MODE = u"sh"

    SHEBANG = u"""#!%(shell)s"""
    ULIB = u"""\
function display_help() {
    uecho "$scriptname:

USAGE
    $scriptname [options]

OPTIONS"
}

source /etc/ulib &&
urequire DEFAULTS ||
exit 1

parse_opts "${PRETTYOPTS[@]}" \\
    --help '$exit_with display_help' \\
    @ args -- "$@" && set -- "${args[@]}" || die "$args"\
"""

    def get_lines(self):
        lines = Lines()
        if self.executable:
            self.appendtext(lines, self.SHEBANG)
        self.appendtext(lines, self.modeline)
        if self.executable and self.ulib:
            self.appendtext(lines, self.ULIB, bnl=True)
        return lines

register_templ(ShTempl)
