#!/bin/sh
# Copyright 2014 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.

PKG=mingetty
VERSION=1.08

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

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

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

rm -rf $PKG-$VERSION
tar xzf $PKG-$VERSION.tar.gz || exit 4
cd $PKG-$VERSION || exit 5

ARGS="-O2 -march=i686 -mtune=generic"
make || exit 6
mkdir -p /tmp/core-$PKG/sbin/
mkdir -p /tmp/core-$PKG/usr/man/man8/
make install DESTDIR=/tmp/core-$PKG MANDIR=/usr/man/man8
gzip /tmp/core-$PKG/usr/man/man8/*.8

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

mkdir -p /tmp/core-$PKG/usr/share/$PKG
cp -p COPYING /tmp/core-$PKG/usr/share/$PKG
gzip /tmp/core-$PKG/usr/share/$PKG/COPYING

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