src/azimuth/system/generate_blob_index.sh
#!/bin/sh
#=============================================================================#
# Copyright 2012 Matthew D. Steele <mdsteele@alum.mit.edu>                    #
#                                                                             #
# This file is part of Azimuth.                                               #
#                                                                             #
# Azimuth 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.                                                          #
#                                                                             #
# Azimuth 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 Azimuth.  If not, see <http://www.gnu.org/licenses/>.                  #
#=============================================================================#

set -e
set -u

OUTFILE=$1
shift

# Keep this declaration in sync with resource_blob.c
cat > $OUTFILE <<EOF
// File generated by src/azimuth/system/generate_blob_index.sh
#include <stddef.h>
const struct resource_entry {
  const char *name;
  size_t offset, length;
} resource_index[] = {
EOF

OFFSET=0
NUM_ENTRIES=0
for RESOURCE_FILE in "$@"; do
    NAME="$(basename $(dirname $RESOURCE_FILE))/$(basename $RESOURCE_FILE)"
    SIZE=$(wc -c < $RESOURCE_FILE)
    echo "  {.name=\"$NAME\", .offset=$OFFSET, .length=$SIZE}," >> $OUTFILE
    OFFSET=$((OFFSET + SIZE))
    NUM_ENTRIES=$((NUM_ENTRIES + 1))
done
echo "};" >> $OUTFILE
echo "const size_t resource_index_size = $NUM_ENTRIES;" >> $OUTFILE
