#!/bin/sh
#  tcbi forked from buildinxi 2010
# Version: 1.4
# Date: 2021-04-17
# 
# aus9: http://forum.tinycorelinux.net/index.php?topic=14082.0
# http://distro.ibiblio.org/tinycorelinux/2.x/tce/src/inxi/

[ `id -u` != 0 ] && { echo "Must be root"; exit 1; }

PACKAGE="inxi"
ME="h2"
CONTACT="smxi.org/site/contact.php"

TCUSER=`cat /etc/sysconfig/tcuser`
THISDIR=`pwd`
PKGDIR=${THISDIR}/${PACKAGE}

#  mailutils mail doesn't have -A attachment in tc
BUILDDEPS="squashfs-tools submitqc"
for dep in ${BUILDDEPS}; do
   sudo -u ${TCUSER} tce-load -w -i $dep
done

[ -d ${PKGDIR} ] && rm -rf ${PKGDIR}/*

mkdir -p ${PKGDIR}/tmp/usr/local/bin
cd ${PKGDIR}/tmp/usr/local/bin/
# redirects to: https://github.com/smxi/inxi/raw/master/ but smxi.org url never changes
wget --no-check-certificate https://smxi.org/${PACKAGE} && chmod +x ${PACKAGE} || exit 1
# my $self_name='inxi';
VERSION=$(awk -F "'" '/^my \$self_version/ {print $2;exit}' ${PKGDIR}/tmp/usr/local/bin/${PACKAGE})

# grab mutt
#echo "grabbing 32 bit mutt for emailer..."
#wget --no-check-certificate http://distro.ibiblio.org/tinycorelinux/4.x/x86/tcz/mutt.tcz
#tce-load -i mutt.tcz

cd ${PKGDIR}/tmp/
mkdir -pm 775 usr/local/tce.installed
mkdir -pm 775 usr/local/share/doc/${PACKAGE}

cat <<EOF> usr/local/tce.installed/${PACKAGE}
#!/bin/sh
busybox cp -p /tmp/tcloop/inxi/usr/local/bin/${PACKAGE} /usr/local/bin
EOF

chmod 775 usr/local/bin/${PACKAGE}
chmod -R 775 usr/local/tce.installed
chown root:staff usr/local/bin/${PACKAGE}
chown -R root:staff usr/local/tce.installed

cat <<EOF> usr/local/share/doc/${PACKAGE}/LICENSE.txt
${PACKAGE}: Copyright (C) 2008-2021 Harald Hope
Additional features (C) Scott Rogers - kde, cpu info
Further fixes (listed as known): Horst Tritremmel <hjt at sidux.com>
Steven Barrett (aka: damentz) - usb audio patch; swap percent used patch
Jarett.Stevens - dmidecode -M patch for older systems with the /sys

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  

See GNU General Public License version 3 for full license text:
https://www.gnu.org/licenses/gpl-3.0.en.html
EOF

find usr/ -not -type d > ../${PACKAGE}.tcz.list
mksquashfs ./ ../${PACKAGE}.tcz -b 4096

chmod 755 ${PACKAGE}.tcz

cd ../
md5sum ${PACKAGE}.tcz > ${PACKAGE}.tcz.md5.txt

# http://mirror.cpsc.ucalgary.ca/mirror/tinycorelinux/tcz/
# maybe udev-extra? that was in old inxi
# procps.tcz not in 12
cat <<EOF> ${PACKAGE}.tcz.dep
coreutils.tcz
dmidecode.tcz
pci-utils.tcz
perl5.tcz
EOF

size=`du -h ${PACKAGE}.tcz | cut -f1`
today=`date +%Y/%m/%d`

cat <<EOF> ${PACKAGE}.tcz.info
Title:          ${PACKAGE}.tcz
Description:    Universal, portable system information tool
Version:        ${VERSION}
Author:         Harald Hope
Original-site:  https://smxi.org/docs/inxi.htm
Copying-policy: GPLv3
Size:           ${size}
Extension_by:   ${ME} (${CONTACT})
Tags:           CLI ${PACKAGE} IRC perl sysinfo system information hardware report
Comments:       Commonly used in IRC, CLI (sys admin), and for forum support.
                inxi -h will show switches to use for different outputs.
                ---------
                Depends on perl5, coreutils, dmidecode, pci-utils, and procps
                Suggests: bind (for dig/WAN IP), hddtemp, lm_sensors, 
                usbutils (optional, provides slightly different USB data).
                ---------
                This extension uses recursive deps
                This extension is PPI compatible
Change-log:     2009/07/21 First version 1.0.17 (kingdomcome)
                2009/08/22 Updated to 1.1.13 (kingdomcome)
                2009/10/12 Updated to 1.1.14 (kingdomcome)
                2009/11/29 Updated to 1.2.7 (kingdomcome)
                2010/01/04 Updated to 1.2.8 (kingdomcome)
                2010/02/15 Updated to 1.4.4 (kingdomcome)
                2010/02/22 Updated to 1.4.6 (kingdomcome)
                2010/04/04 Updated to 1.4.8 (kingdomcome)
                2010/06/22 Updated to 1.4.10 (kingdomcome)
                2012/09/20 Updated to 1.8.17 (aus9)
                2018/10/05 Updated to 3.0.26 (${ME})
                2018/10/14 Updated to 3.0.27 (${ME})
                2019/02/08 Updated to 3.0.32 (${ME})
                2019/05/02 Updated to 3.0.34 (${ME})
                2020/04/24 Updated to 3.1.00 (${ME})
                2021/04/17 Updated to 3.3.04 (${ME})
Current:        ${today} Updated to ${VERSION} (${ME})
EOF

cd ${PKGDIR}
submitqc

cd ../

rm -rf ${PKGDIR}/tmp

tar cvzf ${PACKAGE}.tar.gz ${PACKAGE}

echo 'bcrypt password: tinycore'

# password 'tinycore'
bcrypt ${PACKAGE}.tar.gz

echo 'done! upload then email package to: tcesubmit@gmail.com'

# To decrypt a command, run it using the same command. 
# Decrypting with the -o option outputs the file to the command line, 
# allowing it to be read, but not leaving the unencrypted file on the hard drive.

## NOTE: because emailers aren't working, just use gftp to upload the file to techpatterns
## then email it from dev system to: tcesubmit@gmail.com

#  -a ${THISDIR}/${PACKAGE}.tar.gz

# echo "New version of ${PACKAGE}: ${VERSION}" | mutt -s "TinyCore ${PACKAGE} ${VERSION} update" -a ${THISDIR}/${PACKAGE}.tar.gz ${TO}
# echo "New version of ${PACKAGE}: ${VERSION}" | /usr/local/bin/mail -s "TinyCore ${PACKAGE} ${VERSION} update" -a ${THISDIR}/${PACKAGE}.tar.gz ${TO}



