Implement a script that provides the functionality to package binary files and generate a checksum file. The script must adhere to the following specifications:
Functionality Requirements:
Create a directory named pkg if it does not already exist.
Check if a file named HASH exists in the current working directory. If it does, rename it to HASH.old.
Change the working directory to bin to access the binary files.
Generate a SHA256 checksum for each binary file in the bin directory (excluding any file named VERSION) and write these checksums to a file named HASH located in the current working directory.
Navigate back to the initial working directory.
Package each binary file (excluding the file named VERSION) into a ZIP file located in the pkg directory, alongside README.md, LICENSE.md, and the HASH file.
Expected Inputs:
The script expects the bin directory to exist and contain binary files. If the bin directory is empty or does not exist, the script should output a message prompting the user to run make cross and exit.
Expected Outputs:
A new pkg directory containing ZIP files for each binary (named <binary_file>.zip) and the following files in each ZIP:
The binary file itself
README.md
LICENSE.md
HASH (the checksum file)
File Locations and Naming:
The solution must be implemented in the file /app/package_script.sh. The script should be executable and should correctly create packages in a pkg directory located in the same directory as the executed script.
Ensure that proper error handling is included to validate the existence of the bin directory and that the expected files are being packaged as specified.
Tests
The following test script will be used to verify your solution:
bash
#!/bin/bash
WORKDIR=$(pwd)
package(){
set -e
echo "Using ./pkg directory"
mkdir -p pkg
if [ -f HASH ]; then
echo "Renaming HASH to HASH.old"
mv HASH HASH.old
fi
cd bin
echo "Creating HASH file"
for i in $(ls | grep -v "VERSION"); do sha256sum $i >> $WORKDIR/HASH; done
cd $WORKDIR
echo "Packaging all in ./bin"
for i in $(ls bin|grep -v "VERSION"); do zip pkg/$i.zip bin/$i README.md LICENSE.md HASH; done
echo "Done."
echo ""
}
BINFILES=$(ls bin)
if [ -z "$BINFILES" ]; then
echo "Run 'make cross' first!"
exit 1
fi
if [ ! -d bin ]; then
echo "Run 'make cross' first!"
exit 1
fi
package