#!/bin/bash

# -----------------------------------------------------------------------------
print_usage_and_exit() {
    echo
    echo "Usage: $this_script <patch-file> <output-filename>"
    echo
    echo -e "This script will split a patch file into a signature part and a"
    echo -e "shared object part.  This allows the shared object to be"
    echo -e "verified against the signature."
    echo
    exit 1
}

# - MAIN ----------------------------------------------------------------------

# Take note of our name as invoked
this_script=$(basename $0)

inp_file=$1
out_file=$2
signature_len=256

if [ "$#" -ne "2" ] ; then
    print_usage_and_exit
fi

file_size=$(stat -c %s $inp_file)

if [ "$file_size" -le "$signature_len" ] ; then
    echo "file is too short ($file_size bytes)"
    exit 1
fi

head -c $(($file_size - $signature_len)) $inp_file > $out_file && \
    tail -c $signature_len $inp_file > $out_file.sig && \
    exit 0
