# CorePKG build script for demidecode.
# Copyright 2016-2017 David Egan Evans
#
# This software is provided 'as-is', without any express or implied
# warranty. In no event will the authors be held liable for any damages
# arising from the use of this software.
#
# Permission is granted to anyone to use this software for any purpose,
# including commercial applications, and to alter it and redistribute it
# freely, subject to the following restrictions:
#
# 1. The origin of this software must not be misrepresented; you must
# not claim that you wrote the original software. If you use this
# software in a product, an acknowledgment in the product documentation
# would be appreciated but is not required.
#
# 2. Altered source versions must be plainly marked as such, and must 
# not be misrepresented as being the original software.
#
# 3. This notice may not be removed or altered from any source distribution.

DIST=core
PKG=dmidecode
VERSION=3.1

if test ! -w .
then echo 'Cannot write to this directory!' 1>&2; exit 2
fi

if test ! -f core.info
then echo "The core.info file is not present." 1>&2; exit 3
fi

if test -d /tmp/$DIST-$PKG/
then rm -rf /tmp/$DIST-$PKG/
fi

if test -d /tmp/$DIST-$PKG-doc/
then rm -rf /tmp/$DIST-$PKG-doc/
fi

rm -rf $PKG-$VERSION
tar xJf $PKG-$VERSION.tar.xz || exit 4
cd $PKG-$VERSION || exit 5

CFLAGS+="-O2 -march=i686 -mtune=generic" make || exit 6
make install DESTDIR=/tmp/$DIST-$PKG \
  sbindir="/bin" \
  mandir="/usr/man" \
  man8dir="/usr/man/man8" \
  docdir="/usr/share/dmidecode" \
  || exit 7
find /tmp/$DIST-$PKG/usr/man/ -type f -exec gzip {} \;
for i in $(find /tmp/$DIST-$PKG/usr/man/ -type l)
do ln -s $(readlink "$i").gz "$i".gz
	rm "$i"
done

find /tmp/$DIST-$PKG/ | xargs file | \
	grep -e "executable" -e "shared object" | \
	grep ELF | cut -f1 -d: | xargs strip 2> /dev/null

rm /tmp/$DIST-$PKG/usr/share/$PKG/*
cp -p LICENSE* /tmp/$DIST-$PKG/usr/share/$PKG/
gzip /tmp/$DIST-$PKG/usr/share/$PKG/LICENSE*

cd ..
cp core.info /tmp/$DIST-$PKG/
chmod -R u+w,go+r-w,a-s /tmp/$DIST-$PKG*/
find /tmp/$DIST-$PKG*/ -type d -exec chmod ugo+x "{}" \;
if test $(id -u) -ne 0
then echo "$PKG has been installed to /tmp/$DIST-$PKG/"
	echo "Before packaging: chown -R root: /tmp/$DIST-$PKG/"
else chown -R root: /tmp/$DIST-$PKG/
	corepkg -c /tmp/$DIST-$PKG/ && rm -rf /tmp/$DIST-$PKG/
fi
rm -rf $PKG-$VERSION
