licence check added by elflng on Mon Aug 2 16:57:57 2021

#!/bin/bash
#
# licence-check.sh
# checks licences of all dependencies in a tree
# 
# author:      elf
# date:        16 10 2017
# platform:    linux


# directory to check
CHECKDIR=$1
OUTTYPE=$2
CHECKFILE=/tmp/licencecheck_`echo ${CHECKDIR} | tr '/' '_'`

if [ -z "${OUTTYPE}" ] ; then
    OUTTYPE="normal"
elif [ "${OUTTYPE: -3}" = "csv" ] ; then
    OUTTYPE="csv"
else
    OUTTYPE="normal"
fi

if [ \( -z "${CHECKDIR}" \) -o \( \! -d "${CHECKDIR}" \) ] ; then
    echo "usage: licence-check.sh <directory-name>"
    echo "where <directory-name> is the post-build source code directory"
    exit 1
fi

# get the file list
# grep -l -r -i 'licen[sc]e' ${CHECKDIR} > ${CHECKFILE}
#find ${CHECKDIR} -iname '*.app' -o -iname '*package.json' -o -iname '*mix.exs' > ${CHECKFILE}
#grep -l -r -i 'licen[sc]e' `cat ${CHECKFILE}` > ${CHECKFILE}.2
find ${CHECKDIR} \( -iname '*.app' -o -iname '*package.json' -o -iname '*mix.exs' \) -a -exec grep -l -r -i 'licen[sc]e' '{}' ';' > ${CHECKFILE}
find ${CHECKDIR} -iname '*licen[sc]e*' -a -type f | grep -v '/.git/' >> ${CHECKFILE}
grep -v '/_build/' ${CHECKFILE} | sort | uniq > ${CHECKFILE}.2
mv ${CHECKFILE}.2 ${CHECKFILE}

# make the packages (directories) list
cat ${CHECKFILE} | xargs dirname | grep -v '/_build/' | sed -e 's%/ebin%%' | sort | uniq > ${CHECKFILE}.2




package_name () {
    #echo "${1}" | sed -e "s%${CHECKDIR}/deps/%%" -e 's%/ebin%%' -e 's%/deps/%/%'
    echo "$1" | sed -e "s%${CHECKDIR}/\(deps/\)\?%%" -e 's%/deps/%/%'
}

package_grep () {
    local p q
    p="$1"
    q="$2"
    echo "${p}" | sed -e "s%${q}%%" 
}

for i in `cat ${CHECKFILE}.2` ; do
    PKG_NAME=`package_name $i`
    PKG_LIC=""
    for j in `grep -E "^${i}/(ebin/)?[^/]*$" ${CHECKFILE}` ; do
#        echo ${j}
        if [ "${j: -4}" = "json" ] ; then
            LIC=`grep 'licen[sc]e' ${j} | sed -e 's/^.*license.: "\([^"]*\)".*$/\1/'`
        elif [ "${j: -3}" = "app" ] ; then
            LIC=`grep 'licen[sc]e' ${j} | sed -e 's/.*[[]\(.*\)[]].*/\1/' -e 's/"//g'`
        # elif [ \( "${j: -3}" = "app" \) -o \( "${j: -7}" = "mix.exs" \) ] ; then
        elif [ "${j: -7}" = "mix.exs" ] ; then
            LIC=`grep -A 3 'licen[sc]e' ${j} | tr '\n' ' ' | sed -e 's/^.*licen[sc]es[^"]*"\([^"]*\)".*$/\1/'` 
        else 
            # LICENSE
            if `head -1 ${j} | grep -i apache > /dev/null` ; then
                LIC="Apache `head -2 ${j} | grep -i version | sed -e 's/^.*ersion \([0-9.]*\)[^0-9.]\?.*$/\1/'`"
            elif `head -1 ${j} | grep -i mozilla > /dev/null` ; then
                LIC="MPL `head -2 ${j} | grep -i version | sed -e 's/^.*ersion \([0-9.]*\)[^0-9.]\?.*$/\1/'`"
            elif `head -4 ${j} | grep -i 'licensed under the apache lic' > /dev/null` ; then
                LIC="Apache `head -4 ${j} | grep -i version | sed -e 's/^.*ersion \([0-9.]*\)[^0-9.].*$/\1/'`"
            elif `head -10 ${j} | grep -i '^Permission is hereby granted, free of charge, to any person' > /dev/null` ; then
                LIC="MIT"
            elif `head -10 ${j} | grep -i '^Redistribution and use in source and binary forms' > /dev/null` ; then
                LIC="BSD"
            elif `head -5 ${j} | grep -i '^Permission to use, copy, modify, and/or distribute this software' > /dev/null` ; then
                LIC="ISC"
            elif `head -5 ${j} | grep -i '^CC0 1.0 Universal' > /dev/null` ; then
                LIC="CC0-1.0"
            else
                LIC="UNKNOWN"
            fi
        fi
#        echo "pkglic: ${PKG_LIC} lic: ${LIC} pg: `package_grep "${PKG_LIC}" "${LIC}"`"
        if [ -z "${PKG_LIC}" ] ; then
            PKG_LIC="${LIC}"
        elif [ "${PKG_LIC}" = "UNKNOWN" ] ; then
            PKG_LIC="${LIC}"
        elif [ "${LIC}" = "UNKNOWN" ] ; then
            continue
        elif [ "`package_grep "${PKG_LIC}" "${LIC}"`" = "${PKG_LIC}" ] ; then
            if [ "${OUTTYPE}" = "csv" ] ; then
                PKG_LIC="${PKG_LIC}:${LIC}"
            else
               PKG_LIC="${PKG_LIC}, ${LIC}"
            fi
        else
            continue
        fi
    done
    if [ "${OUTTYPE}" = "csv" ] ; then
        echo "\"${PKG_NAME}\",\"${PKG_LIC}\""
    else
        echo "${PKG_NAME}: ${PKG_LIC}"
    fi
done

rm ${CHECKFILE} ${CHECKFILE}.2