Switch on/off service start at boot.

    .. versionchanged:: 2016.3.4

    Support for jail (representing jid or jail name) and chroot keyword argument
    in kwargs. chroot should be used when jail's /etc is mounted read-only and
    should point to a root directory where jail's /etc is mounted read-write.

def _switch(name,                   # pylint: disable=C0103
            on,                     # pylint: disable=C0103
            **kwargs):
    '''
    Switch on/off service start at boot.

    .. versionchanged:: 2016.3.4

    Support for jail (representing jid or jail name) and chroot keyword argument
    in kwargs. chroot should be used when jail's /etc is mounted read-only and
    should point to a root directory where jail's /etc is mounted read-write.
    '''
    jail = kwargs.get('jail', '')
    chroot = kwargs.get('chroot', '').rstrip('/')
    if not available(name, jail):
        return False

    rcvar = _get_rcvar(name, jail)
    if not rcvar:
        log.error('rcvar for service %s not found', name)
        return False

    if jail and not chroot:
        # prepend the jail's path in config paths when referring to a jail, when
        # chroot is not provided. chroot should be provided when the jail's /etc
        # is mounted read-only
        chroot = _get_jail_path(jail)

    config = kwargs.get('config',
                        __salt__['config.option']('service.config',
                                                  default='{0}/etc/rc.conf'.format(chroot)
                                                  )
                        )

    if not config:
        rcdir = '{0}/etc/rc.conf.d'.format(chroot)
        if not os.path.exists(rcdir) or not os.path.isdir(rcdir):
            log.error('%s not exists', rcdir)
            return False
        config = os.path.join(rcdir, rcvar.replace('_enable', ''))

    nlines = []
    edited = False

    if on:
        val = 'YES'
    else:
        val = 'NO'

    if os.path.exists(config):
        with salt.utils.files.fopen(config, 'r') as ifile:
            for line in ifile:
                line = salt.utils.stringutils.to_unicode(line)
                if not line.startswith('{0}='.format(rcvar)):
                    nlines.append(line)
                    continue
                rest = line[len(line.split()[0]):]  # keep comments etc
                nlines.append('{0}="{1}"{2}'.format(rcvar, val, rest))
                edited = True
    if not edited:
        # Ensure that the file ends in a \n
        if len(nlines) > 1 and nlines[-1][-1] != '\n':
            nlines[-1] = '{0}\n'.format(nlines[-1])
        nlines.append('{0}="{1}"\n'.format(rcvar, val))

    with salt.utils.files.fopen(config, 'w') as ofile:
        nlines = [salt.utils.stringutils.to_str(_l) for _l in nlines]
        ofile.writelines(nlines)

    return True