8b973a1
#!/usr/bin/env bash
8b973a1
8b973a1
#
8b973a1
# Fedora Drupal 7 prep licenses and docs
8b973a1
#
8b973a1
# - Moves common licenses and docs into .rpm/{licenses,docs}/ respectively
8b973a1
# - Provides licenses and docs file list ".rpm/licenses-and-docs.txt"
8b973a1
#   which can be used in spec files as "%files -f .rpm/licenses-and-docs.txt"
8b973a1
#
49c3bbb
# Copyright (c) 2016-2020 Shawn Iwinski <shawn@iwin.ski>
8b973a1
#
8b973a1
# Permission is hereby granted, free of charge, to any person obtaining a copy
8b973a1
# of this software and associated documentation files (the "Software"), to deal
8b973a1
# in the Software without restriction, including without limitation the rights
8b973a1
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8b973a1
# copies of the Software, and to permit persons to whom the Software is
8b973a1
# furnished to do so, subject to the following conditions:
8b973a1
#
8b973a1
# The above copyright notice and this permission notice shall be included in
8b973a1
# all copies or substantial portions of the Software.
8b973a1
#
8b973a1
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
8b973a1
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
8b973a1
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
8b973a1
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
8b973a1
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
8b973a1
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
8b973a1
# SOFTWARE.
8b973a1
#
8b973a1
8b973a1
[ -d .rpm ] || mkdir .rpm
8b973a1
8b973a1
# Licenses
8b973a1
rm -rf .rpm/licenses
8b973a1
mkdir .rpm/licenses
8b973a1
for LICENSE_FILENAME in LICENSE COPYING COPYRIGHT
8b973a1
do
8b973a1
    for LICENSE in $(find . -type f -iregex ".*/${LICENSE_FILENAME}\(\.\(md\|rst\|txt\)\)?\$")
8b973a1
    do
8b973a1
        DIR=$(dirname "$LICENSE")
8b973a1
        mkdir -p .rpm/licenses/${DIR}
8b973a1
        mv "$LICENSE" .rpm/licenses/${DIR}/
8b973a1
    done
8b973a1
done
8b973a1
8b973a1
# Docs
8b973a1
rm -rf .rpm/docs
8b973a1
mkdir .rpm/docs
8b973a1
mv INSTALL.*.txt .rpm/docs/
8b973a1
for DOC_FILENAME in AUTHORS CHANGELOG CHANGES INSTALL MAINTAINERS README TESTING UPGRADE
8b973a1
do
8b973a1
    for DOC in $(find . -type f -iregex ".*/${DOC_FILENAME}\(\.\(md\|rst\|txt\)\)?\$")
8b973a1
    do
8b973a1
        DIR=$(dirname "$DOC")
8b973a1
        mkdir -p .rpm/docs/${DIR}
8b973a1
        mv $DOC .rpm/docs/${DIR}/
8b973a1
    done
8b973a1
done
8b973a1
for COMPOSER in $(find . -name "composer.*")
8b973a1
do
8b973a1
    DIR=$(dirname "$COMPOSER")
8b973a1
    mkdir -p .rpm/docs/${DIR}
8b973a1
    mv $COMPOSER .rpm/docs/${DIR}/
8b973a1
done
8b973a1
8b973a1
# Licenses and docs files list
8b973a1
rm -f .rpm/licenses-and-docs.txt
8b973a1
touch .rpm/licenses-and-docs.txt
8b973a1
[ $(find .rpm/licenses/ -type f | wc -l) -ge 1 ] && \
8b973a1
    echo "%license .rpm/licenses/*" >> .rpm/licenses-and-docs.txt
8b973a1
[ $(find .rpm/docs/ -type f | wc -l) -ge 1 ] && \
8b973a1
    echo "%doc .rpm/docs/*" >> .rpm/licenses-and-docs.txt
8b973a1
8b973a1
# Ensure no executable files
8b973a1
for EXECUTABLE_FILE in $(find .rpm/{licenses,docs}/ -type f -executable)
8b973a1
do
8b973a1
    chmod a-x "${EXECUTABLE_FILE}"
8b973a1
    echo "NOTICE: License or doc file \"${EXECUTABLE_FILE}\" was executable and should be fixed upstream" \
8b973a1
        | sed 's#\.rpm/\(licenses\|docs\)/##' 1>&2
8b973a1
done
8b973a1
8b973a1
# Verbose output for logging...
8b973a1
echo "+ find .rpm/{licenses,docs}/ | sort"
8b973a1
find .rpm/{licenses,docs}/ | sort
8b973a1
echo "+ cat .rpm/licenses-and-docs.txt"
8b973a1
cat .rpm/licenses-and-docs.txt