#!/bin/sh

arch=i386
build=P1
pkgbase=linuxinc

root_chk=`id | grep 'uid=0' | cut -d" " -f1`
echo $root_chk

if [ $root_chk".x" == ".x" ] ; then
  SUDO='/usr/bin/sudo'
else
  SUDO=''
fi

if [ -f Makefile ]; then
  major=`grep "^VERSION" Makefile | cut -d" " -f3`
  minor=`grep "^PATCH" Makefile | cut -d" " -f3`
  rel=`grep "^SUBLEVEL" Makefile | cut -d" " -f3`
  patch=`grep "^EXTRAVERSION" Makefile | cut -d" " -f3`
else
  echo "cannot find Makefile"
  exit
fi

if [ -f .config ]; then
  local=`grep "^CONFIG_LOCALVERSION=" .config | cut -d"=" -f2 | sed 's/\"//g' | sed 's/-/_/'`
else
  echo "cannot find .config "
  exit
fi

vers=${major}"."${minor}"."${rel}${patch}
fullvers=${major}"."${minor}"."${rel}${patch}${local}

echo $fullvers

pkg=${pkgbase}"-"${fullvers}"-"${arch}"-"${build}".tgz"

H=`pwd`
T=`pwd`/../pkg

if [ -d $T ]; then
  $SUDO rm -rf $T
fi

mkdir -p $T/usr/src
( cd .. ; tar cvf - linux-$vers ) | ( cd $T/usr/src ; tar xf -)

( cd $T/usr/src ; ln -sf linux-$vers linux )
pushd $T/usr/src/linux
  for i in COPYING CREDITS Documentation MAINTAINERS README REPORTING-BUGS arch block crypto drivers fs init ipc kernel lib mm net security sound usr ; do
    $SUDO rm -rf $i
  done
  for i in asm-alpha asm-arm asm-arm26 asm-cris asm-frv asm-h8300 asm-ia64 asm-m32r asm-m68k asm-m68knommu asm-mips asm-parisc asm-powerpc asm-ppc asm-s390 asm-sh asm-sh64 asm-sparc asm-sparc64 asm-um asm-v850 asm-x86_64 asm-xtensa ; do
    $SUDO rm -rf include/$i
  done
popd

pushd $T
  $SUDO chown -R root.root *
  $SUDO /sbin/makepkg ../$pkg
popd
