package main

import (
	"path/filepath"
	"strings"
)

// ~~~~~~~~~~~~~~~~~~~~ define each license ~~~~~~~~~~~~~~~~~~~~
var common = `
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
`

var sh = `
#  Licensed to the Apache Software Foundation (ASF) under one or more
#  contributor license agreements.  See the NOTICE file distributed with
#  this work for additional information regarding copyright ownership.
#  The ASF licenses this file to You under the Apache License, Version 2.0
#  (the "License"); you may not use this file except in compliance with
#  the License.  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.
`

var xml = `
<!--
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
-->
`

// ~~~~~~~~~~~~~~~~ end ~~~~~~~~~~~~~~~~

var (
	lic                License
	supportedFileNames = []string{"Makefile", ".npmignore"}
)

type License map[string]string

// init license
func NewLicense() License {
	// cache license
	if lic != nil {
		return lic
	}

	// make license
	license := make(map[string]string)
	license[".go"] = strings.TrimSpace(common)
	license[".py"] = strings.TrimSpace(sh)
	license[".sh"] = strings.TrimSpace(sh)
	license[".yml"] = strings.TrimSpace(sh)
	license[".yaml"] = strings.TrimSpace(sh)
	license[".ts"] = strings.TrimSpace(common)
	license[".java"] = strings.TrimSpace(common)
	license[".js"] = strings.TrimSpace(common)
	license["Makefile"] = strings.TrimSpace(sh)
	license[".npmignore"] = strings.TrimSpace(sh)
	license[".xml"] = strings.TrimSpace(xml)

	return license
}

// support check current file is supported with license
func (l License) support(file string) bool {
	// check file name
	for _, name := range supportedFileNames {
		if file == name {
			return true
		}
	}

	// check file suffix
	for suffix := range l {
		if strings.HasSuffix(file, suffix) {
			return true
		}
	}

	return false
}

// get license by file and ext name
func (l License) get(file string) string {
	// check file name
	for _, name := range supportedFileNames {
		if file == name {
			return l[name]
		}
	}

	// check file
	for suffix, license := range l {
		ext := filepath.Ext(file)
		if ext == suffix {
			return license
		}
	}

	return ""
}
