So this is version 1 of the new script to automate catalyst builds. There is a catalyst-auto from gentoo-releng, but 1. my needs for this purpose are much simpler than what catalyst-auto provides and 2. writing this script is proving to be fun.

As always, suggestions welcome.

#!/bin/sh

### VARS

url="https://distfiles.gentoo.org/releases/amd64/autobuilds/"
txtfile="latest-stage3-amd64-desktop-systemd.txt"
email="webmaster@gentoostudio.org"

### ERROR MSGS

remote_text_file_missing="Remote text file does not exist. Details sent to $email."
couldnt_get_stage3="Could not get stage3 seed. Details sent to $email."

### EMAIL MSGS
# Done this way instead of editing text files directly for conveniece.
# File output needed for ssmtp.
# TODO: Convert these to a single mailout function.

msg_wget_fail_txt=$(cat << EOF
Subject: wget failed (txt file)

The wget command to fetch the text file containing the path to the latest stage3 seed failed.

The currently set url is: $url$txtfile
EOF
)

msg_emaint_fail=$(cat << EOF
Subject: emaint sync failed

emaint failed to sync.
EOF
)

msg_mv_seed=$(cat << EOF
Subject: Failed to mv seed

Seed could not be moved to build dir.
EOF
)

msg_catalyst_snapshot_fail=$(cat << EOF
Subject: Catalyst snapshot failed

Catalyst failed to create the latest snapshot.
EOF
)

msg_catalyst_stage1_fail=$(cat << EOF
Subject: Catalyst stage1 failed

Catalyst failed to build stage1.
EOF
)

msg_catalyst_stage2_fail=$(cat << EOF
Subject: Catalyst stage2 failed

Catalyst failed to build stage2.
EOF
)

msg_catalyst_stage3_fail=$(cat << EOF
Subject: Catalyst stage3 failed

Catalyst failed to build stage3.
EOF
)

msg_catalyst_stage4_fail=$(cat << EOF
Subject: Catalyst stage4 failed

Catalyst failed to build stage4.
EOF
)


### PART 1: UPDATE SEED

# Get text file describing latest stage3 tarball.
# -O option circumvents wget creating a new file on every run and gives us a fixed filename to use.
# The wget -S option is --server-response, which can be grepped.
# ssmtp.conf has been configured.
if [[ `wget -S --spider $url$txtfile 2>&1 | grep 'HTTP/1.1 200 OK'` ]];
	then wget -O latest.txt $url$txtfile;
	else
		echo -e "$msg_wget_fail_txt" > wget_textfile_failmsg
		ssmtp -v $email < wget_textfile_failmsg
		echo $remote_text_file_missing
		exit 1;
fi

# Parse text file for URL
# Use tail cmd to read last line of file, which is all we need,
# then use sed to chop off everything after the space in that line
# When wget is done, move the file to where its needed
# (The mv destination is a symlink)
# Not sure we need an ifelse here. If the above check passes, this wget should work.
latest=$(tail -n 1 latest.txt | sed 's#[[:space:]].*##')
wget -O stage3seed.tar.xz $url$latest
if [ $? != 0 ]; then

msg_wget_fail_seed=$(cat << EOF
Subject: wget failed (stage3 seed)

The wget command to fetch the latest stage3 seed failed.

The currently set url is $url$latest
EOF
)
	echo -e "$msg_wget_fail_seed" > wget_seed_failmsg
	ssmtp -v $email < wget_seed_failmsg
	echo $couldnt_get_stage3
	exit 1;
fi
# Trailing slash prevents mv'ing seed to a new file.
mv stage3seed.tar.xz builddir/
if [ $? != 0 ]; then
	echo -e "$msg_mv_seed" > mv_seed_failmsg
	ssmtp -v $email < mv_seed_failmsg
	echo "Failed to mv seed to build dir."
	exit 1;
fi

### PART 2: SYNC

emaint -a sync
if [  $? != 0 ]; then
	echo -e "$msg_emaint_fail" > emaint_failmsg
	ssmtp -v $email < emaint_failmsg
	echo "Emaint sync failed. Notification sent to $email."
	exit 1;
fi

emerge -uDN --keep-going --backtrack=250 @world
#TODO: Error capture here.

catalyst -s latest
if [ $? != 0 ]; then
	echo -e "$msg_catalyst_snapshot_fail" > catalyst_snapshot_failmsg
	ssmtp -v $email < catalyst_snapshot_failmsg
	echo "Catalyst could not create a snapshot. Notification sent to $email."
	exit 1;
fi

### PART 3: STAGES

catalyst -af stage1.spec
if [ $? != 0 ]; then
	echo -e "$msg_catalyst_stage1_fail" > catalyst_stage1_failmsg
	ssmtp -v $email < catalyst_stage1_failmsg
	echo "Catalyst failed to build stage1. Notification sent to $email."
	exit 1;
fi

catalyst -af stage2.spec
if [ $? != 0 ]; then
	echo -e "$msg_catalyst_stage2_fail" > catalyst_stage2_failmsg
	ssmtp -v $email < catalyst_stage2_failmsg
	echo "Catalyst failed to build stage2. Notification sent to $email."
	exit 1;
fi

catalyst -af stage3.spec
if [ $? != 0 ]; then
	echo -e "$msg_catalyst_stage3_fail" > catalyst_stage3_failmsg
	ssmtp -v $email < catalyst_stage3_failmsg
	echo "Catalyst failed to build stage3. Notification sent to $email."
	exit 1;
fi

catalyst -af stage4.spec
if [ $? != 0 ]; then
	echo -e "$msg_catalyst_stage4_fail" > catalyst_stage4_failmsg
	ssmtp -v $email < catalyst_stage4_failmsg
	echo "Catalyst failed to build stage4. Notification sent to $email."
	exit 1;
fi

completion_time=$(date)
msg_catalyst_complete=$(cat << EOF
Subject: Catalyst build complete

The latest catalyst build completed at $completion_time.
EOF
)
echo -e "$msg_catalyst_complete" > catalyst_complete_msg
ssmtp -v $email < catalyst_complete_msg

### fscript is automatically executed by stage4.

### PART 7: MV TARBALL TO WEB SERVER
# Catalyst is actually running in a chroot, so this step requires a cron job on the host, as the chroot has no access to the web server.