#!/bin/sh


# Parse commandline

TARGET=$1
TARGET_ARCH=`echo $TARGET | sed -e 's/-.*//'`

if [ "$TARGET" = "" ]; then
		echo "Usage: $0 <target>"
		exit
fi

TST=`echo $TARGET | grep -- -`
if [ "$TST" = "" ]; then
    echo "WARNING: Full GNU system type requred, such as arm-linux,"
    echo "         and $TARGET does not seem to be appropreate."
    echo "         Press Ctrl-C to abort, otherwise I'll continue in"
    echo "         5 s"
    sleep 5
fi

TARGET_PKG=`echo $TARGET | sed -e 's/_/-/g'`

# Get some data on the package we are about to craete

GDB_INSTALLED=`dpkg -s toolchain-source-gdb | grep ^Status:`

TOOLCHAIN_VERSION=`dpkg -s toolchain-source | grep Version: | sed -e 's/Version: //'`
GCC_VERSION=`ls -l /usr/src/toolchain/gcc.tar.bz2 | sed -e 's/^.* -> gcc-\(.*\).tar.bz2$/\1/'`
BINUTILS_VERSION=`ls -l /usr/src/toolchain/binutils.tar.bz2 | sed -e 's/^.* -> binutils-\(.*\).tar.bz2$/\1/'`
if [ ! "$GDB_INSTALLED" = "Status: install ok installed" ]; then
  GDB_VERSION="toolchain-source-gdb not installed"
else
  GDB_VERSION=`ls -l /usr/src/toolchain/gdb.tar.bz2 | sed -e 's/^.* -> gdb-\(.*\).tar.bz2$/\1/'`
fi
DATE=`822-date`

USERNAME=`awk -F: -vUSER=$USER '$1 == USER { print $5; }' /etc/passwd`

if [ "$USERNAME" = "" -a -x /usr/bin/ypmatch ]; then
        # Give NIS a try
        USERNAME=`ypmatch $USER passwd.byname|awk -F: '{ print $5; }'`
fi

if echo $USERNAME | grep -q "\,"; then
        X=`expr index "$USERNAME" ","`
        X=`expr $X - 1`
        USERNAME=`expr substr "$USERNAME" 1 $X`
fi

if [ "$EMAIL" = "" ]; then
        EMAIL="$USER@`cat /etc/mailname`"
fi


echo
echo "Target                    : $TARGET"
echo "Maintainer                : $USERNAME"
echo "Email-Address             : $EMAIL"
echo "Toolchain                 : $TOOLCHAIN_VERSION"
echo "  binutils                : $BINUTILS_VERSION"
echo "  gcc                     : $GCC_VERSION"
echo "  gdb                     : $GDB_VERSION"

DEFS="-D '__TARGET__=$TARGET' -D '__TARGET_PKG__=$TARGET_PKG' -D '__TARGET_ARCH__=$TARGET_ARCH' -D '__MAINTAINER__=$USERNAME <$EMAIL>' -D '__TOOLCHAIN_VERSION__=$TOOLCHAIN_VERSION' -D '__BINUTILS_VERSION__=$BINUTILS_VERSION-5' -D '__GCC_VERSION__=$GCC_VERSION-1' -D '__GDB_VERSION__=$GDB_VERSION-1' -D '__DATE__=$DATE'"


# Unpack templates
tar xzf /usr/src/toolchain/template.tgz
tar xzf /usr/src/toolchain/template-gcc.tgz

BINUTILS_DIR=binutils-$TARGET_PKG-$BINUTILS_VERSION
mv binutils-TARGET $BINUTILS_DIR

GCC_DIR=gcc-$TARGET_PKG-$GCC_VERSION
mv gcc-TARGET $GCC_DIR

if [ "$GDB_INSTALLED" = "Status: install ok installed" ]; then
  GDB_DIR=gdb-$TARGET_PKG-$GDB_VERSION
  mv gdb-TARGET/debian/gdb-TARGET.files gdb-TARGET/debian/gdb-$TARGET_PKG.files
  mv gdb-TARGET $GDB_DIR
else
  rm -rf  gdb-TARGET
fi

# Run m4 macros

for i in $GCC_DIR/debian/changelog.m4 `find $BINUTILS_DIR $GDB_DIR -name \*.m4`; do
		o=`echo $i | sed -e 's/\.m4//'`
		sh -c "m4 $DEFS < $i > $o"
		rm $i
done

chmod +x $BINUTILS_DIR/debian/rules
chmod +x $GCC_DIR/debian/rules

debarch=`tpkg-debarch $TARGET`
mv $GCC_DIR/debian/rules.defs $GCC_DIR/debian/rules.defs.t
echo GCC_TARGET=$debarch > $GCC_DIR/debian/rules.defs
echo DEB_TARGET_ALIAS=$TARGET >> $GCC_DIR/debian/rules.defs
cat $GCC_DIR/debian/rules.defs.t >> $GCC_DIR/debian/rules.defs
rm $GCC_DIR/debian/rules.defs.t

cd $GCC_DIR 
mv debian/control.m4 debian/control.m4.t
sed -e 's/^Source:.*/Source: gcc-'$TARGET_PKG'/' \
    -e 's/^Maintainer:.*/Maintainer: '"$USERNAME"' <'"$EMAIL"'>/' \
    -e 's/BINUTILS_BUILD_DEP/binutils-'$TARGET_PKG'/' \
    <debian/control.m4.t >debian/control.m4
rm debian/control.m4.t
./debian/rules control
cd ..


if [ "$GDB_INSTALLED" = "Status: install ok installed" ]; then
  chmod +x $GDB_DIR/debian/rules
  echo
  echo "Created in $BINUTILS_DIR, $GCC_DIR, and $GDB_DIR"
  echo
else
  echo
  echo "Created in $BINUTILS_DIR, $GCC_DIR"
  echo
fi

# All done

