#!/bin/bash
# Adds a file to the initrd (if it's an initramfs)
# first argument is the name of archive
# second argurment is the name of the file to add

# 20060126: fix "Premature end of file" with some old cpio (Roland Robic)


# check the arguments
if [ $# -ne 2 ]; then
	program_name=$(basename $0)
	echo "\
$program_name: too few arguments
Usage: $program_name initrd-name.img file-to-add
Adds a file to an initrd (in initramfs format)

  initrd-name.img: filename of the initrd in initramfs format
  file-to-add: filename of the file to add
  " 1>&2
    exit 1
fi

tempcpio=$(mktemp)
# cleanup on exit, hangup, interrupt, quit, termination
trap 'rm -f $tempcpio' 0 1 2 3 15

# extract the archive
gunzip -c "$1" > $tempcpio || exit 1

# add the file
(echo "$2" | cpio --quiet -H newc -c -o -A -O $tempcpio) || exit 1

# re-compress the archive
gzip -c $tempcpio > "$1"
