Last active
July 1, 2016 14:01
-
-
Save criztovyl/08715cb4c9cf44f3d33f to your computer and use it in GitHub Desktop.
Initializes files with GPL or adds it to existing files.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# A small tool to init files with a GPL header. | |
# Copyright (C) 2015 Christoph "criztovyl" Schulz | |
# | |
# This program is free software: you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation, either version 3 of the License, or | |
# (at your option) any later version. | |
# | |
# This program is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# | |
# You should have received a copy of the GNU General Public License | |
# along with this program. If not, see <http://www.gnu.org/licenses/>. | |
if [[ "$1" =~ ^-{0,2}h(elp)?$ ]]; then | |
echo -e "Usage: $0 filename.type" | |
echo "Will open file afterwards, supress this by setting \$GI_NOPOSTPROG to any value." | |
echo "By default is interactive (asks you for description and author)." | |
echo " To disable this completly set \$GI_NOINTERACTIVE to any value." | |
echo " You also can define \$GI_SUMMARY and/or \$GI_AUTHOR to use these values instead of asking." | |
exit 1 | |
fi | |
# postprog - program to run after initialisation, {} will be replaced with file name | |
postprog="vim {}" | |
prefix=".gpl-init" | |
filename=$1 | |
year=`date +%Y` | |
[ "$filename" ] || exit 5 | |
ext=${filename##*.} | |
for name in summary author; do | |
var=GI_${name^^} | |
[ "${!var}" ] && | |
eval $name=\"${!var}\" | |
if [ -z "${!name}" ]; then | |
if [ -f ".gi_${name,,}" ]; then | |
echo "Using summary file .gi_${name,,}" | |
eval $name=\"`cat ".gi_${name,,}"`\" | |
elif [ -z "$GI_NOINTERACTIVE" ]; then | |
read -p "${name^}: " $name | |
fi | |
fi | |
done | |
# Disable globbing, "/*" would cause problems otherwise | |
# Remember if was already disabled | |
[[ "$-" == *f* ]] && noglob=true | |
set -f | |
# Determine comment type | |
case $ext in | |
sh|bash) | |
firstline="#!/bin/bash" | |
fc="#" | |
lastline="" | |
;; | |
php) | |
firstline='<?php\n/*' | |
fc="" | |
lastline=' */' | |
;; | |
py) | |
firstline='"""' | |
fc="" | |
lastline='"""' | |
;; | |
rb) | |
firstline="#!/usr/bin/env ruby" | |
fc="#" | |
;; | |
esac | |
# Create Header | |
gpl="" | |
# mapfile callback | |
callback(){ gpl=$gpl$2"\n"; } | |
mapfile -c 1 -C callback <<gpl | |
$firstline | |
$fc $summary | |
$fc Copyright (C) $year $author | |
$fc | |
$fc This program is free software: you can redistribute it and/or modify | |
$fc it under the terms of the GNU General Public License as published by | |
$fc the Free Software Foundation, either version 3 of the License, or | |
$fc (at your option) any later version. | |
$fc | |
$fc This program is distributed in the hope that it will be useful, | |
$fc but WITHOUT ANY WARRANTY; without even the implied warranty of | |
$fc MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
$fc GNU General Public License for more details. | |
$fc | |
$fc You should have received a copy of the GNU General Public License | |
$fc along with this program. If not, see <http://www.gnu.org/licenses/>. | |
$lastline | |
gpl | |
# Copy the file to preserve file permissions | |
[ -f $filename ] && cp $filename $filename$prefix | |
# Put header into file | |
echo -e $gpl > $filename$prefix | |
# If file exists, append contents | |
[ -f $filename ] && cat $filename >> $filename$prefix | |
#Re-enable globbing if wasn't disabled before | |
[[ "$noglob" = true ]] || set +f | |
# Backup original file | |
[ -f $filename ] && cp $filename $filename$prefix-bak | |
# Overwrite real file with header + file contents | |
mv -f $filename$prefix $filename | |
if [ -z "$NOPOSTPROG" ]; then | |
# Replace {} with filename in post-program | |
postprog=${postprog//\{\}/$filename} | |
# Does not work; not an interactive shell. | |
# Add post-program to history | |
#history -s $postprog | |
# Run post-program | |
$postprog | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment