<commit_before>package main

import (
	"bytes"
	"fmt"
	"html/template"
	"io/ioutil"
	"net/http"
	"os"
	"os/exec"
	"os/user"
	"path/filepath"
	"strconv"
	"strings"

	"koding/klientctl/config"
	"koding/klientctl/metrics"

	"github.com/codegangsta/cli"
	"github.com/koding/logging"
	"github.com/koding/service"
)

type ServiceOptions struct {
	Username   string
	KontrolURL string
}

var defaultServiceOpts = &ServiceOptions{
	Username:   username(),
	KontrolURL: config.KontrolURL,
}

var klientShTmpl = template.Must(template.New("").Parse(`#!/bin/bash

# Koding Service Connector
# Copyright (C) 2012-2016 Koding Inc., all rights reserved.

# source environment
for src in /etc/{environment,profile,bashrc}; do
	[[ -f ${src} ]] && source ${src}
done

isOSX=$(uname -v | grep -Ec '^Darwin Kernel.*')

if [[ ${isOSX} -ne 0 ]]; then
	# wait under OS-X till network is ready
	. /etc/rc.common

	CheckForNetwork

	while [ "${NETWORKUP}" != "-YES-" ]; do
		sleep 5
		NETWORKUP=
		CheckForNetwork
	done
fi

# configure environment

ulimit -n 5000

# start klient

{{if .KontrolURL}}
export KITE_KONTROL_URL=${KITE_KONTROL_URL:-{{.KontrolURL}}}
{{else}}
export KITE_KONTROL_URL=${KITE_KONTROL_URL:-https://koding.com/kontrol/kite}
{{end}}
{{if .User}}
export USERNAME=${USERNAME:-{{.User}}}
{{end}}
{{if .KlientBinPath}}
export KLIENT_BIN=${KLIENT_BIN:-{{.KlientBinPath}}}
{{else}}
export KLIENT_BIN=${KLIENT_BIN:-/opt/kite/klient/klient}
{{end}}
export HOME=$(eval cd ~${USERNAME}; pwd)
export PATH=$PATH:/usr/local/bin

sudo -E -u "${USERNAME}" ${KLIENT_BIN} -kontrol-url "${KITE_KONTROL_URL}"
`))

// newService provides a preconfigured (based on klientctl's config)
// service object to install, uninstall, start and stop Klient.
func newService(opts *ServiceOptions) (service.Service, error) {
	if opts == nil {
		opts = defaultServiceOpts
	}

	// TODO: Add hosts's username
	svcConfig := &service.Config{
		Name:        "klient",
		DisplayName: "klient",
		Description: "Koding Service Connector",<commit_msg>klienctl: ensure KITE_HOME is set in klient.sh<commit_after>package main

import (
	"bytes"
	"fmt"
	"html/template"
	"io/ioutil"
	"net/http"
	"os"
	"os/exec"
	"os/user"
	"path/filepath"
	"strconv"
	"strings"

	"koding/klientctl/config"
	"koding/klientctl/metrics"

	"github.com/codegangsta/cli"
	"github.com/koding/logging"
	"github.com/koding/service"
)

type ServiceOptions struct {
	Username   string
	KontrolURL string
}

var defaultServiceOpts = &ServiceOptions{
	Username:   username(),
	KontrolURL: config.KontrolURL,
}

var klientShTmpl = template.Must(template.New("").Parse(`#!/bin/bash

# Koding Service Connector
# Copyright (C) 2012-2016 Koding Inc., all rights reserved.

# source environment
for src in /etc/{environment,profile,bashrc}; do
	[[ -f ${src} ]] && source ${src}
done

isOSX=$(uname -v | grep -Ec '^Darwin Kernel.*')

if [[ ${isOSX} -ne 0 ]]; then
	# wait under OS-X till network is ready
	. /etc/rc.common

	CheckForNetwork

	while [ "${NETWORKUP}" != "-YES-" ]; do
		sleep 5
		NETWORKUP=
		CheckForNetwork
	done
fi

# configure environment

ulimit -n 5000

# start klient

export USERNAME=${USERNAME:-{{.User}}}
export KITE_HOME=${KITE_HOME:-{{.KiteHome}}}
{{if .KontrolURL}}
export KITE_KONTROL_URL=${KITE_KONTROL_URL:-{{.KontrolURL}}}
{{else}}
export KITE_KONTROL_URL=${KITE_KONTROL_URL:-https://koding.com/kontrol/kite}
{{end}}
{{if .KlientBinPath}}
export KLIENT_BIN=${KLIENT_BIN:-{{.KlientBinPath}}}
{{else}}
export KLIENT_BIN=${KLIENT_BIN:-/opt/kite/klient/klient}
{{end}}
export HOME=$(eval cd ~${USERNAME}; pwd)
export PATH=$PATH:/usr/local/bin

sudo -E -u "${USERNAME}" ${KLIENT_BIN} -kontrol-url "${KITE_KONTROL_URL}"
`))

// newService provides a preconfigured (based on klientctl's config)
// service object to install, uninstall, start and stop Klient.
func newService(opts *ServiceOptions) (service.Service, error) {
	if opts == nil {
		opts = defaultServiceOpts
	}

	// TODO: Add hosts's username
	svcConfig := &service.Config{
		Name:        "klient",
		DisplayName: "klient",
		Description: "Koding Service Connector",
{'fraction_non_alphanumeric': 0.16163104888233848, 'fraction_numerical': 0.006877916973716531, 'mean_word_length': 4.639889196675901, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5544551', 'n_tokens_mistral': 1615, 'n_tokens_neox': 1530, 'n_words': 348}