#!/bin/bash

BUILDCONF="/usr/share/mix/builder.conf"

# Auto generate builder.conf if it's not there
if [ ! -f "$BUILDCONF" ]; then
echo "[Mixer]
LOCAL_BUNDLE_DIR = /usr/share/mix/local-bundles

[Builder]
SERVER_STATE_DIR = /usr/share/mix/update
BUNDLE_DIR = /usr/share/mix/local-bundles
YUM_CONF = /usr/share/mix/.yum-mix.conf
CERT = /usr/share/mix/Swupd_Root.pem
VERSIONS_PATH =/usr/share/mix
LOCAL_RPM_DIR = /usr/share/mix/rpms
LOCAL_REPO_DIR = /usr/share/mix/local

[swupd]
BUNDLE=os-core
CONTENTURL=file:///usr/share/mix/update/www
VERSIONURL=file:///usr/share/mix/update/www
FORMAT=1" | sudo tee "$BUILDCONF" > /dev/null
fi


MIX_DIR="/usr/share/mix/"
OUTPUTDIR="$MIX_DIR/update/www"
BUNDLE_DIR="$(awk -F '=' '/LOCAL_BUNDLE_DIR/ {print $2}' $BUILDCONF  | tr -d ' ')"
RPM_DIR="$(awk -F '=' '/LOCAL_RPM_DIR/ {print $2}' $BUILDCONF  | tr -d ' ')"
REPO_DIR="$(awk -F '=' '/LOCAL_REPO_DIR/ {print $2}' $BUILDCONF  | tr -d ' ')"
BUILD_STATE_DIR="/var/lib/mix"
MIX_CERT="$MIX_DIR/Swupd_Root.pem"
PRIVKEY="$MIX_DIR/private.pem"
FORMAT="$(awk -F '=' '/FORMAT/ {print $2}' $BUILDCONF  | tr -d ' ')"

pkg=$1
bundle=$2

type "mixer"
if [ $? -ne 0 ]; then
	echo -e "Please install the mixer bundle before proceeding:\n  swupd bundle-add mixer"
	exit 1
fi

# Make sure all the directories exist
sudo mkdir -p $MIX_DIR $REPO_DIR $BUILD_STATE_DIR
if [ ! -f "$MIX_DIR/upstreamversion" ]; then
	awk -F  "=" '/^VERSION_ID=/ { print $2 }' /usr/lib/os-release | tr -d '\n' | sudo tee "$MIX_DIR/upstreamversion" > /dev/null
elif [[ "$pkg" == "regenerate" ]]; then
	echo -n "$(($(cat $MIX_DIR/upstreamversion)*1000))" | sudo tee "$MIX_DIR/mixversion" > /dev/null
fi
if [ ! -f "$MIX_DIR/mixversion" ]; then
	echo -n "$(($(cat $MIX_DIR/upstreamversion)*1000))" | sudo tee "$MIX_DIR/mixversion" > /dev/null
fi

clearver=$(cat $MIX_DIR/upstreamversion| tr -d ' ')
echo -n "$clearver" | sudo tee "$MIX_DIR/version" > /dev/null

cd "$MIX_DIR"
sudo touch mixbundles
sudo mixer init --upstream-version $(cat "$MIX_DIR/upstreamversion") --mix-version $(cat "$MIX_DIR/mixversion")

if [[ -n "$pkg" && -n "$bundle" ]]; then
	echo "Adding $pkg to $bundle..."
	if [ ! -f "$BUNDLE_DIR/$bundle" ]; then
		echo "# [TITLE]: $bundle
# [DESCRIPTION]: Custom bundle
# [STATUS]: Active
# [CAPABILITIES]:
# [MAINTAINER]: Mixer User" | sudo tee "$BUNDLE_DIR/$bundle" > /dev/null
	fi

	grep -x "$pkg" "$BUNDLE_DIR/$bundle"
	if [ $? -ne 0 ]; then
		# Write package to bundle definition
		echo "$pkg" | sudo tee -a "$BUNDLE_DIR/$bundle" > /dev/null
	fi
fi

# Save LAST_VER so we can add unchanged bundles to new Manifest.MoM
PREVIOUS_VER=$(cat "$MIX_DIR/update/image/LAST_VER")
if [ $? -ne 0 ]; then
	echo "LAST_VER not found, setting to 0"
	PREVIOUS_VER=0
fi

mixver=$(cat $MIX_DIR/mixversion)

# Copy the rpms into the local database location
sudo mixer add-rpms --config "$BUILDCONF"
sudo mixer bundle add "$bundle" os-core --config "$BUILDCONF"
# Use the original (non-merged) MoM for creating updates between custom content
if [ $PREVIOUS_VER -eq 0 ]; then
	sudo mixer build bundles --config "$BUILDCONF" --new-chroots
	sudo mixer build update --config "$BUILDCONF" --new-swupd --increment
else
	((mixver-=10))
	sudo mv $OUTPUTDIR/$mixver/Manifest.MoM $OUTPUTDIR/$mixver/FullManifest.MoM
	sudo mv $OUTPUTDIR/$mixver/Manifest.MoM.$mixver $OUTPUTDIR/$mixver/Manifest.MoM
	sudo mixer build bundles --config "$BUILDCONF" --new-chroots
	sudo mixer build update --config "$BUILDCONF" --new-swupd --increment
	sudo mv $OUTPUTDIR/$mixver/Manifest.MoM $OUTPUTDIR/$mixver/Manifest.MoM.$mixver
	sudo mv $OUTPUTDIR/$mixver/FullManifest.MoM $OUTPUTDIR/$mixver/Manifest.MoM
	((mixver+=10))

fi
cd -

# Download and verify the upstream Clear MoM
echo "* Downloading $clearver Manifest.MoM..."
sudo -E curl -f -O "https://download.clearlinux.org/update/$clearver/Manifest.MoM"
if [ $? -eq 22 ]; then
	echo "ERROR: Could not download Manifest.MoM ver $clearver"
	sudo rm -rf "Manifest.MoM"
	exit 1
fi
sudo -E curl -f -O "https://download.clearlinux.org/update/$clearver/Manifest.MoM.sig"
if [ $? -eq 22 ]; then
	echo "ERROR: Could not download Manifest.MoM.sig ver $clearver"
	sudo rm -rf "Manifest.MoM.sig"
	exit 1
fi
CERT="/usr/share/ca-certs/Swupd_Root.pem"

sudo openssl smime -verify -in Manifest.MoM.sig -inform der -content Manifest.MoM -CAfile "$CERT"  > /dev/null 2>&1
if [ $? -ne 0 ]; then
	echo "ERROR: Official Manifest.MoM version $clearver could not be verified!"
	exit
fi

echo "* Verified upstream Manifest.MoM"

### Combine upstream MoM with Mixer MoM ###
###########################################
filecount=$(grep filecount Manifest.MoM  | cut -d ':' -f 2 | tr -d '\t')
mixcount=$(grep filecount $OUTPUTDIR/$mixver/Manifest.MoM | cut -d ':' -f 2 | tr -d '\t')

# Filecount must include new manifests
((mixcount--))
((filecount+=mixcount))

echo "* Adjusting stats for Manifest.MoM...."

# Adjust the stats for Manifest.MoM when it's combined
sudo sed -i "s/version:\\t[0-9].*/version:\\t$mixver/" Manifest.MoM
# Need to set mixver to previous ver, without this mixver = the currently built ver
((mixver-=10))
if [ $PREVIOUS_VER -eq 0 ]; then
	sudo sed -i "s/previous:\\t[0-9].*/previous:\\t$clearver/" Manifest.MoM
else
	sudo sed -i "s/previous:\\t[0-9].*/previous:\\t$mixver/" Manifest.MoM
fi
((mixver+=10))
sudo sed -i "s/filecount:\\t[0-9].*/filecount:\\t$filecount/" Manifest.MoM
sudo sed -i "s/contentsize:\\t[0-9].*/contentsize:\\t$contentsize/" Manifest.MoM
sudo sed -i "s/MANIFEST\\t[0-9].*/MANIFEST\\t$FORMAT/" Manifest.MoM

# Remove os-core because ours will take place
sudo sed -i '/os-core$/d' Manifest.MoM

# Change the Mixer manifest to have the new mixer flag
sudo sed -i "s/M\.\.\./M\.\.m/" "$OUTPUTDIR/$mixver/Manifest.MoM"

# Add mixer bundles to official MoM
grep -w 'M..m' "$OUTPUTDIR/$mixver/Manifest.MoM" | sudo tee -a Manifest.MoM

# Copy the combined MoM to our output now (backup original mix one)
sudo mv "$OUTPUTDIR/$mixver/Manifest.MoM" "$OUTPUTDIR/$mixver/Manifest.MoM.$mixver"
sudo cp Manifest.MoM "$OUTPUTDIR/$mixver/Manifest.MoM"

# Resign the Manifest.MoM
sudo openssl smime -sign -binary -in "$OUTPUTDIR/$mixver/Manifest.MoM" -signer "$MIX_CERT" -inkey "$PRIVKEY" -outform DER -out "$OUTPUTDIR/$mixver/Manifest.MoM.sig"
cd "$OUTPUTDIR/$mixver"
sudo tar -cvf Manifest.MoM.tar Manifest.MoM
cd -

# Clean up files
sudo rm -rf Manifest.MoM Manifest.MoM.sig
