#!/bin/bash

if test $# -gt 0
then
  echo 1>&2 "Syntax: $0"
  echo 1>&2 "Function: Apply RBF fs patches to 2.6.x kernel source tree"
  echo 1>&2 "Options:"
  echo 1>&2 "    (none)"
  exit 1
fi

suffix=pre-rbfpatch

function patchit
{
  file="$1"
  oldtag="$2"
  newtag1="$3"
  newtag2="$4"

  if ! test -f $file
  then
    echo 1>&2 File $file not found, cannot patch
    return 1
  fi

  if grep -q "$newtag1" $file
  then
    echo File $file already patched
  else
    oldtagline="`grep "$oldtag" $file`"
    if test -z "$oldtagline"
    then
      echo "$oldtag" not found in file $file
      return 2
    fi
    mv $file $file.$suffix
    if test -z "$newtag2"
    then
      sed "s,$oldtagline,$oldtagline\n$newtag1," <$file.$suffix >$file
    else
      sed "s,$oldtagline,$oldtagline\n$newtag1\n$newtag2," <$file.$suffix >$file
    fi
    echo File $file successfully patched, old version copied to $file.$suffix
  fi
  return 0
}

file=.config
oldtag='CONFIG_QNX4FS_FS'
newtag1='CONFIG_RBF_FS=m'
newtag2='CONFIG_RBF_FS_WRITE=y'
patchit "$file" "$oldtag" "$newtag1" "$newtag2"
if test $? = 2
then
  oldtag='CONFIG_QNX4FS_FS=m'
  patchit "$file" "$oldtag" "$newtag1" "$newtag2"
  if test $? != 0
  then
    echo Could not patch file $file
  fi
fi

file=fs/Makefile
oldtag='obj-$(CONFIG_QNX4FS_FS)		+= qnx4/'
newtag='obj-$(CONFIG_RBF_FS)		+= rbf/'
patchit "$file" "$oldtag" "$newtag"
if test $? != 0
then
  echo Could not patch file $file
fi

file=fs/Kconfig
if grep -q 'config RBF_FS' $file
then
  echo File $file already patched
  exit 0
fi

cat <<EOF >$file.rbf
config RBF_FS
	tristate "OS-9/RBF file system support (read only)"
	help
	  This is the native file system used by the real-time operating system
	  OS-9/OS-9000 from Microware Systems Corp. (now Radisys). Say Y if you want
	  to be able to mount RBF hard disks and (possibly) floppies. Unless you say
	  Y to "RBF file system write support" below, you will only be able to read
	  these disks.

	  This is a fairly complete implementation but there are certain limitations
	  for very small (<512) and very large (>4096) RBF sector sizes. Floppy
	  support has not been tested.

	  If you want to compile this as a module ( = code which can be
	  inserted in and removed from the running kernel whenever you want),
	  say M here and read Documentation/modules.txt. The module will be
	  called rbf.o.

	  If you don't know whether you need it, then you don't need it:
	  answer N.				

config RBF_FS_WRITE
	bool "RBF write support (DANGEROUS)"
	depends on RBF_FS && EXPERIMENTAL
	help
	  Say Y here if you want to test write support for RBF disks. Make
	  sure you have backed up any valuable data before using this file
	  system in read-write mode.

	  If in doubt, say N.



EOF

headlines=`grep -n 'config QNX4FS_FS' $file | cut -d: -f1`
head -`expr $headlines + 26` $file >$file.head
tail -n +`expr $headlines + 27` $file >$file.tail
mv $file $file.$suffix
cat $file.head $file.rbf $file.tail >$file
rm -f $file.head $file.rbf $file.tail
echo File $file successfully patched, old version copied to $file.$suffix
