#!/bin/bash
#
# Copyright 2010,2016,2022  Stuart Winter, Surrey, England, UK.
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
#
###################################################################
# Script : package_miniroots ('arm/build')
# Purpose: Prepare a range of mini Slackware ARM root filesystems
#          of specific Slackware ARM trees.
# Author : Stuart Winter <mozes@slackware.com>
# Version: 1.01
# Date...: 28th Mar 2016
###################################################################
# History
###################################################################
# 28th Mar 2016, version 1.01
#  * Corrected package list (missing line continuation markers)
#    Removed asterisk from "iputils" package name, as it was probably
#    some legacy kludge.
#    Thanks to Johann Wilhelm
#
# 4th Feb 2010, version 1.00
#  * First version
#  * Includes later minor package name updates that were not logged.
#
###################################################################
# Usage:
#
# This script is intended only to be run on the Slackware ARM
# build machines.  For any other use, you'd need to modify the
# locations in the variables below.
# This script really doesn't need to be used by the public - you'd
# just run the miniroot.build and maually pack up the output
# in /tmp/miniroot.
###################################################################

# Load the Slackware ARM development library:
source /usr/share/slackdev/buildkit.sh

CWD=$PWD
ROOTLOC=/tmp/miniroot
ROOTFSSTORE=$CWD/../ # = ~/armedslack/slackwarearm-devtools/minirootfs/
# On the Slackware ARM build machines, the Slackware ARM trees
# (eg "slackwarearm-14.0", "slackwarearm-current" live inside root's
#  home directory, "/root")
SLACKARMTREESBASE=/root/

case $( uname -m ) in
   arm*)    ARCH="arm" ;;
   aarch64) ARCH="aarch64" ;;
esac

# When using xz with tar, set the options:
#export XZ_OPT="-9e --threads $(( $(nproc) -1 ))"
# Let xz decide the number of threads to use:
export XZ_OPT="-9e --threads=0 -c"

##############################################################

# Build the root:
# Syntax: build_root <name of tree> <dir name in root's home dir>
#
function build_root() {

cd $SLACKARMTREESBASE/$2 || { echo "Cannot change into $SLACKARMTREESBASE/$2" ; exit 1 ;}

# Run the mini rootfs build script:
# $3 is regarding dropping into a chroot to allow manual testing:
$ROOTFSSTORE/scripts/miniroot.build $3 # 2>&1 | tee /tmp/buildroot.log

# Archive the new mini root:
cd $ROOTLOC
# Wipe the old version:
rm -rfv  $ROOTFSSTORE/roots/slack${ARCH}-$1*

# Retrieve the root password:
ROOTPW="$( cat $ROOTLOC/tmp/rootpw )"
rm -f $ROOTLOC/tmp/rootpw

echo "Archiving rootfs..."
#tar pcf - . | bzip2 -f9c > $ROOTFSSTORE/roots/slack${ARCH}-$1-miniroot_$(date "+%d%h%y").tar.bz2
# We need to store the file name in case we're running this over midnight when the date changes:
OUTFILE=slack${ARCH}-$1-miniroot_$(date "+%d%h%y").tar.xz
# Much better compression using LZMA:
tar -Ixz -pcf $ROOTFSSTORE/roots/$OUTFILE .
#tar -cf- . | xz -ze9 - > $ROOTFSSTORE/roots/$OUTFILE
chmod 644 $ROOTFSSTORE/roots/slack${ARCH}-$1-miniroot*.tar.xz

echo "Generating details file, including SHA1SUM..."
cat << EOF > $ROOTFSSTORE/roots/slack${ARCH}-$1-miniroot_details.txt

This is a mini rootfs for Slackware ${ARCH} branch "$1".
Generated on $( date -u )

The login details are:
User....: root
Password: $ROOTPW

SSH key finger prints:
$( for key in $ROOTLOC/etc/ssh/ssh_host_*key*.pub ; do ssh-keygen -lf $key ; done | sed 's/^/\t/g' )

The SHA1SUM of the mini root filesystem archive:
$( pushd $ROOTFSSTORE/roots > /dev/null && sha1sum $OUTFILE | sed 's/^/\t/g' ; popd > /dev/null )

Uncompressed size: $( du -sh . | awk '{print $1}' )

EOF

}

##############################################################

# If we're running as a child of the 'r2b' build orchestration tool,
# we don't want to chroot since it'll stall the build process.
if [ ! -z "${R2BRUNNING}" ]; then
   echo "Running under r2b - chroot deselected and unavailable"
   CHROOTORNOT=nochroottest
 else
   # Change this appropriately, manually:
#   CHROOTORNOT=nochroottest
   CHROOTORNOT=testchroot # default - if calling manually, we'll be testing a change
fi

echo "******************* Building mini roots *********************"
# Syntax: build_root <name of tree> <dir name in the root user's ('/') home dir> <nochroottest|testchroot>
#
#build_root current ac $CHROOTORNOT
#[ "$SLKPORTARCH" = "arm" ]     && build_root 15.0 slackwarearm-15.0 $CHROOTORNOT
#[ "$SLKPORTARCH" = "arm" ]     && build_root current slackwarearm-current $CHROOTORNOT
[ "$SLKPORTARCH" = "aarch64" ] && build_root current slackwareaarch64-current $CHROOTORNOT
#
#build_root 14.2 slackwarearm-14.2 nochroottest
#build_root current ac testchroot
#build_root 14.1 slackwarearm-14.1

# EOF
