#!/bin/sh
cmd=$(basename "$0")
if [ -z $1 ]; then
	echo "Usage: $cmd <path-to-drm-video>"
    exit 3;
fi
path=$1
if [ ! -d "$path/linux-core" ]; then
	echo "Error: Directory $path/linux-core/ not found."
    exit 1;
fi
if [ ! -d "$path/shared-core" ]; then
	echo "Error: Directory $path/shared-core/ not found."
    exit 1;
fi
curdirname=$(basename "$PWD")
if [ "$curdirname" != "psb-kmd" ]; then
	echo "Error: Current directory must be psb-kmd."
	exit 1;
fi
files=$(git-ls-files -x 'debian/*' .)
if [ -n "$files" ]; then
    echo Removing existing GIT files
	git-rm -f -- $files || exit 2
fi
files=$(find . -maxdepth 1 -type f)
if [ -n "$files" ]; then
    echo Removing other files from directory
	rm -v -- $files || exit 2
fi
files=$(find $path/linux-core -type f -name '*.o' -o -name '*.ko' -o -name '*.mod')
if [ -n "$files" ]; then
	echo "Error: $path/linux-core is not clean, do 'make clean' first"
	exit 2;
fi
files=$(find $path/linux-core -type f -not -name 'Module.symvers' -not -name '.*' -not -name '*~')
if [ -z "$files" ]; then
	echo "Error: No files found in $path/linux-core"
	exit 2;
fi
echo Copying linux-core files
cp $files . || exit 2
files=$(find $path/shared-core -type f -not -name 'Makefile*' -not -name '*~')
if [ -z "$files" ]; then
	eho "Error: No files found in $path/shared-core"
	exit 2;
fi
echo Copying shared-core files
cp $files . || exit 2
files=$(find $path/scripts -type f -name 'create_linux_pci_lists.sh')
if [ -z "$files" ]; then
	eho "Error: $path/scripts/create_linux_pci_lists.sh not found"
	exit 2;
fi
echo Copying create_linux_pci_lists.sh
cp $files . || exit 2
echo Removing non-Intel files
find . -maxdepth 1 '(' -name '*_*.c' -o -name '*_*.h' ')' -a -not '(' -name 'drm*' -o -name 'psb*' -o -name 'i915*' -o -name 'intel_*' -o -name 'ati_pcigart.c' ')' -exec  rm '{}' ';'
echo Removing PSB_MSVDX_DEBUG
sed -e 's^/. remove this from release.*^^;s^#define PSB_MSVDX_DEBUG^^' psb_msvdx.h > psb_msvdx.h.new || exit 2
mv psb_msvdx.h.new psb_msvdx.h
debug_files=$(grep -l PSB_MSVDX_DEBUG *.c *.h)
for file in $debug_files; do
  echo "  Removing #ifdef PSB_MSVDX_DEBUG / #endif blocks from $file"
  awk -- '/#ifdef/ { while (getline && $0 !~ /#endif/) ; getline  } // { print }' $file > $file.new || exit 2
  mv $file.new $file
done
sed -e 's^psb_msvdx_debug.o^^' Makefile.kernel > Makefile.kernel.new || exit 2
mv Makefile.kernel.new Makefile.kernel
rm psb_msvdx_debug.* psb_gpu_struct.h psb_kreg.h psb_firmware.* drm_pciids.h
echo Patching Makefile
sed -e 's^SHAREDDIR := ../shared-core^SHAREDDIR := .^;s^../scripts/^./^' Makefile > Makefile.new || exit 2
mv Makefile.new Makefile
echo Patching Release data
DATE=$(date '+%Y%m%d')
sed psb_drv.h -e 's^#define PSB_DRM_DRIVER_DATE.*^#define PSB_DRM_DRIVER_DATE "'$DATE'"^' > psb_drv.h.new && mv psb_drv.h.new psb_drv.h 
echo Adding new files
files=$(find . -maxdepth 1 -type f)
if [ -z "$files" ]; then
	echo "Error: No files found in current directory"
	exit 2;
fi
git-add -- $files Makefile || exit 2
echo "Success"
exit 0
