#!/bin/sh

if [ $# -ne 2 ]
then
	echo "usage: $(basename $0) <flash image> <directory>"
	exit 1
fi

flashFile="$1"
extractDir="$2"

GZIP_START=$(grep -aboP "\x1f\x8b\x08" $flashFile|cut -d":" -f1)

#Get the initrd from the image
dd if="$flashFile" of=kernelfs.initrd-orig.gz bs=$GZIP_START skip=1

#Decompress it
gunzip kernelfs.initrd-orig.gz

#Extract the image
mkdir "$extractDir" && cd "$extractDir" && cpio -id --no-absolute-filenames < ../kernelfs.initrd-orig

cd ..

#Remove the temporary file
rm kernelfs.initrd-orig