#!/bin/sh

KERNELDIR=/usr/src/linux
DOCP=docpd
PREPARSER="./preparser"
UNIQUE=false
VERBOSE=false
NOTEST=true

docpd() {
  BASENAME=`basename $2`; 
  if [ "$BASENAME" = "Makefile" ] ; then
    if [ $VERSION -gt 3 -o $PATCHLEVEL -gt 3 ]; then
      rm -f $2
      cat $1 | sed -e "s/drivers\/isdn\/Rules\.make/Rules\.make/" > $2
    fi
  else
    if ! cmp -s $1 $2 ; then
      if $NOTEST ; then
          echo Copying $1 ...
	  mkdir -p `dirname $2`
  	  rm -f $2 # unlink first
          cp $1 $2
      else
          echo $1 was changed
      fi
    else
      if $VERBOSE ; then
	      echo $2 is up to date, NOT converted
      fi
    fi
  fi
}

docp() {
  if [ $1 -nt $2 -o ! -f $2 ] ; then
    if $NOTEST ; then
        echo Copying $1 ...
	mkdir -p `dirname $2`
	rm -f $2 # unlink first
        cp $1 $2
    else
        echo $1 was changed
    fi
  else
    if $VERBOSE ; then
	    echo $2 is up to date, NOT converted
    fi
  fi
}

docpuni() {
    if $VERBOSE ; then
    	echo -n "Processing $1 ... "
    fi
    TMPNAME=/tmp/`basename $1`.$$
    $PREPARSER -c $CTRLNAME $1 $TMPNAME
    RES=$?
    if [ "$RES" -eq "0" ] ; then 
	if ! cmp -s $1 $2 ; then
		if $NOTEST ; then
			if $VERBOSE ; then
				echo copying original
			else
				echo "Processing $1 ... copying original"
			fi
			mkdir -p `dirname $2`
			rm -rf $2 # unlink first
			cp $1 $2
		else
			if $VERBOSE ; then
				echo original was changed
			else
				echo "Processing $1 ... original was changed"
			fi
		fi
	else
		if $VERBOSE ; then
			echo original file is up to date
		fi
	fi
	rm $TMPNAME
	return 0
    fi
    if [ "$RES" -eq "2" ] ; then
	if ! cmp -s $TMPNAME $2 ; then
		if $NOTEST ; then
			if $VERBOSE ; then
				echo copying modified
			else
				echo "Processing $1 ... copying modified"
			fi
			mkdir -p `dirname $2`
			rm -rf $2 # unlink first
			cp $TMPNAME $2
		else
			if $VERBOSE ; then
				echo modified was changed
			else
				echo "Processing $1 ... modified was changed"
			fi
		fi
	else
		if $VERBOSE ; then
			echo modified file is up to date
		fi
	fi 
	rm $TMPNAME
	return 0
    fi
    echo
    echo "problem with $PREPARSER retcode $RES"
    exit 1
}


#
# Print usage and exit
#
usage() {
	cat<<EOM

	std2kern is used for updating your kernel-tree from within
	this directory.

	std2kern [-d] [-h] [-k DIR] [-v] [-u] [-c FILE] [files ...]

	Options:

	-h	This Text.
	-d	Copy depends on modification date instead of file-compare.
	-k DIR	Kerneltree is in DIR instead of /usr/src/linux
        -v      More mesages about processing
	-u      Make a diff for a unique kernel-tree 
                (preprocessing with $PREPARSER)
	-c FILE	Use FILE as control file for $PREPARSER (only with -u)
	-t      Test, don't really copy files

	Without any files given, within the whole tree, the "right"
	files are copied. When any files are given in the commandline,
	only those are copied.

EOM
	exit
}

#
# Check, if argument is a linux kernel dir
#
checkkernel() {
	if [ -f $1/Makefile ] ; then
		if [ "`grep ^vmlinux: $1/Makefile | grep vmlinux`" != "" ] ; then
			return 0
		fi
	fi
	echo "The given argument does not look like a kernel dir"
	exit 1
}

#
# Determine a control file name
#
calc_ctrl_file() {
	eval `sed -n 's/^\([A-Z]*\) = \([0-9]*\)$/\1=\2/p' $KERNELDIR/Makefile`
	echo "Current kernel version is $VERSION.$PATCHLEVEL.$SUBLEVEL"
	if [ -z "$CTRLNAME" ] ; then
		CTRLNAME=v$VERSION.$PATCHLEVEL.$SUBLEVEL.ctrl
		if [ -f $CTRLNAME ] ; then
			return 0
		fi
		CTRLNAME=v$VERSION.$PATCHLEVEL.ctrl
		if [ -f $CTRLNAME ] ; then
			return 0
		fi
		CTRLNAME=default.ctrl
	fi
	if [ -f $CTRLNAME ] ; then
		return 0
	fi
	echo "No control file found"
	exit 1
}

while getopts :dhk:uc:vt a ; do
	case $a in
		\?)	case $OPTARG in
				k)	echo "-k requires Kernel directory parameter"
					;;
				*)  echo "Unknown option: -$OPTARG"
					echo "Try std2kern -h"
					;;
			esac
			exit 1
			;;
		k)	checkkernel $OPTARG
			KERNELDIR=$OPTARG
			;;
		c)	CTRLNAME=$OPTARG
			;;
		u)	UNIQUE=true
			;;
		v)	VERBOSE=true
			;;
		t)	NOTEST=false
			;;
		d)	DOCP=docp
			;;
		h)	usage
			;;
	esac
done
shift `expr $OPTIND - 1`

calc_ctrl_file

if $UNIQUE ; then
	DOCP=docpuni
fi

echo -n "Using $DOCP"

if $UNIQUE ; then
	echo " with controlfile $CTRLNAME"
else
	echo
fi

if [ $# != 0 ]; then
  for i in $* ; do
    $DOCP $i $KERNELDIR/$i
  done
else
  for i in drivers/isdn/isdn_*.[ch] ; do
    $DOCP $i $KERNELDIR/$i
  done
  for i in drivers/isdn/icn/icn.[ch] ; do
    $DOCP $i $KERNELDIR/$i
  done
  for i in drivers/isdn/pcbit/*.[ch] ; do
    $DOCP $i $KERNELDIR/$i
  done
  for i in drivers/isdn/hisax/*.[ch] drivers/isdn/hisax/md5sums.asc ; do
	if [ "$i" = "drivers/isdn/hisax/md5sums.asc" -a \
	     "$UNIQUE" = "true" ] ; then
		if $VERBOSE ; then
			echo "$i skipped"
		fi
	else
		$DOCP $i $KERNELDIR/$i
	fi
  done
  for i in drivers/isdn/sc/*.[ch] ; do
    $DOCP $i $KERNELDIR/$i
  done
  for i in drivers/isdn/avmb1/*.[ch] ; do
    $DOCP $i $KERNELDIR/$i
  done
  for i in drivers/isdn/act2000/*.[ch] ; do
    $DOCP $i $KERNELDIR/$i
  done
  for i in drivers/isdn/isdnloop/*.[ch] ; do
    $DOCP $i $KERNELDIR/$i
  done
  for i in drivers/isdn/hysdn/*.[ch] ; do
    $DOCP $i $KERNELDIR/$i
  done
  if [ ! -d $KERNELDIR/drivers/isdn/divert ] ; then
    mkdir $KERNELDIR/drivers/isdn/divert
  fi
  for i in drivers/isdn/divert/*.[ch] ; do
    $DOCP $i $KERNELDIR/$i
  done
  if [ -f $KERNELDIR/Documentation/Configure.help ] ; then
    grep -q CONFIG_ISDN_DIVERSION $KERNELDIR/Documentation/Configure.help
    if [ $? != 0 ] ; then
      patch -d $KERNELDIR/Documentation < Documentation/Configure.help.divert.diff
    fi
  fi
  if [ -f $KERNELDIR/Documentation/Configure.help ] ; then
    grep -q CONFIG_ISDN_DRV_EICON $KERNELDIR/Documentation/Configure.help
    if [ $? != 0 ] ; then
      patch -d $KERNELDIR/Documentation < Documentation/Configure.help.eicon.diff
    fi
  fi
  if $NOTEST ; then
    if [ -f $KERNELDIR/Documentation/Configure.help ] ; then
      grep -q CONFIG_ISDN_WITH_ABC $KERNELDIR/Documentation/Configure.help
      if [ $? != 0 ] ; then
	  if [ -f  Documentation/Configure.help.dwabc.diff ] ; then
            patch -d $KERNELDIR/Documentation < Documentation/Configure.help.dwabc.diff
	  fi
      fi
    fi
  fi
  for i in drivers/isdn/eicon/*.[ch] ; do
    $DOCP $i $KERNELDIR/$i
  done
  COPY_COMPAT=false
  if [ -n "$CTRLNAME" ] ; then
        grep "linux/isdn_compat.h" $CTRLNAME >/dev/null || COPY_COMPAT=true;
  fi
  for i in include/linux/*.h ; do
	if [ "$i" = "include/linux/isdn_compat.h" -a \
	     "$UNIQUE" = "true" -a "$COPY_COMPAT" = "false" ] ; then
		if $VERBOSE ; then
			echo "$i skipped"
		fi
	else
		$DOCP $i $KERNELDIR/$i
	fi
  done
  for i in Documentation/isdn/CREDITS Documentation/isdn/README* \
           Documentation/isdn/*.FAQ Documentation/isdn/INTERFACE* \
           Documentation/isdn/HiSax* Documentation/isdn/00-INDEX ; do
	if $UNIQUE ; then
		docpd $i $KERNELDIR/$i
	else
		$DOCP $i $KERNELDIR/$i
	fi
  done
  for i in drivers/isdn/Config.in ; do
	if $UNIQUE ; then
		docpd $i $KERNELDIR/$i
	else
		$DOCP $i $KERNELDIR/$i
	fi
  done
  for i in drivers/isdn/Makefile \
	   drivers/isdn/icn/Makefile \
           drivers/isdn/hisax/Makefile \
           drivers/isdn/pcbit/Makefile \
	   drivers/isdn/sc/Makefile \
           drivers/isdn/act2000/Makefile \
           drivers/isdn/isdnloop/Makefile \
           drivers/isdn/eicon/Makefile \
           drivers/isdn/divert/Makefile \
           drivers/isdn/avmb1/Makefile \
           drivers/isdn/hysdn/Makefile; do
	if [ -f $i.kernel ] ; then
		if $UNIQUE ; then
			docpd $i.kernel $KERNELDIR/$i
		else
			$DOCP $i.kernel $KERNELDIR/$i
		fi
	else
		if $UNIQUE ; then
			docpd $i $KERNELDIR/$i
		else
			$DOCP $i $KERNELDIR/$i
		fi
	fi
  done
	if [ $VERSION -le 2 -o $PATCHLEVEL -le 3 ]; then
		if $UNIQUE ; then
			docpd drivers/isdn/Rules.make $KERNELDIR/drivers/isdn/Rules.make
		else
			$DOCP drivers/isdn/Rules.make $KERNELDIR/drivers/isdn/Rules.make
		fi

	fi
fi
