File indexing completed on 2024-04-14 05:33:34

0001 #!/bin/sh
0002 #
0003 # Copyright (c) 2019, Michal Policht. This file is dually licensed under terms of
0004 # either WTFPL or BEER-WARE LICENSE. You may obtain the copy of WTFPL or BEER-WARE
0005 # LICENSE by googling it. NO WARRANTY. YOU WILL PAY ALL COSTS FOR ANY REPAIRS.
0006 #
0007 # A chain of commands to strip and put license into file. If license file contains
0008 # magic phrase %YEAR%, it will be replaced by file modification years from Git
0009 # log. Another magic phrase is %AUTHORS%, which will be replaced by authors, who
0010 # have changed a file according to Git log.
0011 #
0012 # usage: putlic.sh target_file license_file [comment] [qualifier] [iors] [find_program] [git_program]
0013 # example: putlic.sh script.cmake LICENSE "hash" "MYID" "\n" find git
0014 #
0015 # parameters:
0016 # target_file - where to put license.
0017 # license_file - file containing license.
0018 # comment - comment type. Passed as "comment" variable to striplic.awk.
0019 #           Check striplic.awk for possible values.
0020 # qualifier - copyright notice qualifier. See stiplic.awk for details.
0021 # iors - input/output record separator (new lines style; typically: "\n" - unix mac "\r\n" - windows).
0022 # find_program - command to execute findutils find.
0023 # git_program - command to execute git.
0024 #
0025 # required tools: sh, stat, cut, paste, grep, touch, awk, sed, echo, find (findutils), cat, rm, uniq, git, striplic.awk.
0026 
0027 
0028 usage()
0029 {
0030     echo "usage: $0 target_file license_file [comment] [qualifier] [IORS] [find_program]"
0031     echo "note: working directory from which this script is called"
0032     echo "      must be a directory containing awkgward directory."
0033 }
0034 
0035 target_file=$1
0036 license_file=$2
0037 
0038 if [ ! -e $target_file ]; then
0039         echo $target_file "does not exist"
0040         exit 1
0041 fi
0042 
0043 if [ ! -e $license_file ]; then
0044         echo $license_file "does not exist"
0045         exit 1
0046 fi
0047 
0048 if [ $# -lt 2 ]; then
0049     echo "error: too few arguments"
0050     usage
0051     exit 0
0052 fi
0053 
0054 if [ $# -gt 2 ]; then
0055     comment=$3;
0056 else
0057     comment="dslash"
0058 fi
0059 
0060 if [ $# -gt 3 ]; then
0061     qualifier=$4;
0062 else
0063     qualifier=""
0064 fi
0065 
0066 if [ $# -gt 4 ]; then
0067     iors=$5;
0068 else
0069     iors="\n"
0070 fi
0071 
0072 if [ $# -gt 5 ]; then
0073     find_program=$6;
0074 else
0075     find_program=find
0076 fi
0077 
0078 if [ $# -gt 6 ]; then
0079     git_program=$7;
0080 else
0081     git_program=git
0082 fi
0083 
0084 echo $target_file
0085 timestamp=`stat $target_file | grep 'Modify: ' | cut -d ' ' -f 2,3,4`
0086 # Using "\n|\r\n" as RS is more safe than native record separator, because gawk behaves strangely on Windows.
0087 # (If run without shell it seems to translate input file "\r\n" into unix style "\n".)
0088 awk -v ORS="$iors" -v RS="\n|\r\n" -v comment="$comment" -v qualifier="$qualifier" -f awkgward/striplic.awk $target_file > $target_file.strip
0089 year=`$git_program log --reverse --pretty=format:"%ad" $target_file | cut -d' ' -f5 | awk 'NR==1; END{print}' | uniq | paste -sd "-"`
0090 # HEAD is required when using shortlog from a script (see: git help shortlog and https://stackoverflow.com/questions/43041659/git-shortlog-does-not-show-output-in-jenkins-shell)
0091 authors=`$git_program shortlog HEAD --summary --numbered --email $target_file | cut -c8- | paste -sd "," | sed s/,/,\ /g`
0092 # Invoking sed with -b option to prevent new lines conversion
0093 sed -b -e "s/%YEAR%/$year/" -e "s/%AUTHORS%/$authors/" $license_file > $license_file.putlic
0094 cat $target_file.strip $license_file.putlic > $target_file
0095 rm $target_file.strip
0096 rm $license_file.putlic
0097 touch -md "$timestamp" $target_file
0098