| 1 |
#!/usr/bin/env bash |
| 2 |
# generate the checksec file from the src directory |
| 3 |
|
| 4 |
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &> /dev/null && pwd)" |
| 5 |
REPO_ROOT="$(cd "${SCRIPT_DIR}" && git rev-parse --show-toplevel)" |
| 6 |
cd "${REPO_ROOT}" || { |
| 7 |
echo "error: directory does not exist" |
| 8 |
exit 1 |
| 9 |
} |
| 10 |
generated_file="${REPO_ROOT}/checksec" |
| 11 |
|
| 12 |
# add shebang line and edit line |
| 13 |
cat << 'EOF' > "${generated_file}" |
| 14 |
#!/usr/bin/env bash |
| 15 |
# Do not edit this file directly, this file is generated from the files |
| 16 |
# in the src directory. Any updates to this file will be overwritten when generated |
| 17 |
|
| 18 |
# sanitize the environment before run |
| 19 |
[[ "$(env | /bin/sed -r -e '/^(PWD|SHLVL|_)=/d')" ]] && exec -c "$0" "$@" |
| 20 |
EOF |
| 21 |
|
| 22 |
# add the header |
| 23 |
sed -e '1,3d' "${REPO_ROOT}"/src/header.sh >> "${generated_file}" |
| 24 |
|
| 25 |
# add the license |
| 26 |
echo -ne "\n" >> "${generated_file}" |
| 27 |
sed 's/^/# /' "${REPO_ROOT}"/LICENSE.txt >> "${generated_file}" |
| 28 |
|
| 29 |
# add the core file |
| 30 |
sed -e '1,3d' "${REPO_ROOT}"/src/core.sh >> "${generated_file}" |
| 31 |
|
| 32 |
# join all function files together in the middle |
| 33 |
while read -r file; do |
| 34 |
# remove the first 3 lines of each source file |
| 35 |
# shebang line is included to properly shellcheck and format |
| 36 |
sed -e '1,3d' "${file}" >> "${generated_file}" |
| 37 |
done < <(find "${REPO_ROOT}"/src/functions -type f -iname "*.sh" | sort) |
| 38 |
|
| 39 |
# add the footer |
| 40 |
sed -e '1,3d' "${REPO_ROOT}"/src/footer.sh >> "${generated_file}" |
| 41 |
|
| 42 |
# make it executable |
| 43 |
chmod 755 "${generated_file}" |