#!/bin/bash

## Copyright (C) 2012 - 2025 ENCRYPTED SUPPORT LLC <adrelanos@whonix.org>
## See the file COPYING for copying conditions.

## Small script for Travis CI.

set -x

true "INFO: Currently running script: $BASH_SOURCE"

set -o pipefail

error_handler_travis() {
   exit 1
}

trap "error_handler_travis" ERR

true "INFO $BASH_SOURCE: Script running as user: $(logname)"

mydir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
true "INFO $BASH_SOURCE: mydir: $mydir"

## Example mydir:
## /home/travis/build/Whonix/developer-meta-files/usr/bin

dist_developer_meta_files_folder="$(dirname "$mydir")"
true "INFO $BASH_SOURCE: dist_developer_meta_files_folder: $dist_developer_meta_files_folder"

## Example dist_developer_meta_files_folder:
## /home/travis/build/Whonix/developer-meta-files

whonix_source_folder="$(dirname "$(dirname "$dist_developer_meta_files_folder")")"
true "INFO $BASH_SOURCE: whonix_source_folder: $whonix_source_folder"

## Example whonix_source_folder:
## /home/travis/build/Whonix/Whonix

## Get into Whonix folder.
cd "$whonix_source_folder"

## Debugging.
ls -la

## {{ Build physically isolated machine.

if [ "$TRAVIS_BRANCH" = "travis-gateway" ]; then
   sudo --non-interactive --preserve-env ./derivative-maker --flavor whonix-gateway -- --target root --allow-untagged true
elif [ "$TRAVIS_BRANCH" = "travis-gateway-minimal" ]; then
   sudo --non-interactive --preserve-env ./derivative-maker --flavor whonix-gateway -- --target root --allow-untagged true
elif [ "$TRAVIS_BRANCH" = "travis-gateway-pkg-only" ]; then
   sudo --non-interactive --preserve-env ./build-steps.d/*_prepare-build-machine --allow-untagged true
   ./build-steps.d/1200_create-debian-packages --allow-untagged true
   sudo --non-interactive --preserve-env dm-upgrade-from-local-repository --target root --flavor whonix-gateway --allow-untagged true
elif [ "$TRAVIS_BRANCH" = "travis-workstation-minimal" ]; then
   sudo --non-interactive --preserve-env ./derivative-maker --flavor whonix-workstation -- --target root --allow-untagged true
elif [ "$TRAVIS_BRANCH" = "travis-workstation" ]; then
   sudo --non-interactive --preserve-env ./derivative-maker --flavor whonix-workstation -- --target root --allow-untagged true
elif [ "$TRAVIS_BRANCH" = "travis-workstation-pkg-only" ]; then
   sudo --non-interactive --preserve-env ./build-steps.d/*_prepare-build-machine --allow-untagged true
   ./build-steps.d/*_create-debian-packages --allow-untagged true
   sudo --non-interactive --preserve-env dm-upgrade-from-local-repository --target root --flavor whonix-workstation --allow-untagged true
else
   error "Unknown $TRAVIS_BRANCH branch, check $BASH_SOURCE."
fi

## }}

## Cleanup, so we can create a dummy update package to test the upgrade
## process. (See if re-running all scripts is supported.)
./help-steps/cleanup-files

## Commit a dummy change.
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
temp="dummy_file"
touch "$temp"
git add "$temp"
git commit -m "dummy commit"

## Create a new dummy tag
git tag 101 -m "."

## {{ Test the upgrading from local repository process.

export dist_build_version="101"

if [ "$TRAVIS_BRANCH" = "travis-gateway" ]; then
   ./build-steps.d/*_create-debian-packages  --allow-untagged true
   sudo --non-interactive --preserve-env dm-upgrade-from-local-repository --target root --flavor whonix-gateway
elif [ "$TRAVIS_BRANCH" = "travis-gateway-minimal" ]; then
   #./build-steps.d/1200_create-debian-packages --allow-untagged true
   #sudo --non-interactive --preserve-env dm-upgrade-from-local-repository --target root --flavor whonix-gateway
   true
elif [ "$TRAVIS_BRANCH" = "travis-gateway-pkg-only" ]; then
   ./build-steps.d/*_create-debian-packages --allow-untagged true
   sudo --non-interactive --preserve-env dm-upgrade-from-local-repository --target root --flavor whonix-gateway
elif [ "$TRAVIS_BRANCH" = "travis-workstation-minimal" ]; then
   #./build-steps.d/*_create-debian-packages --allow-untagged true
   #sudo --non-interactive --preserve-env dm-upgrade-from-local-repository --target root --flavor whonix-workstation
   true
elif [ "$TRAVIS_BRANCH" = "travis-workstation" ]; then
   ./build-steps.d/*_create-debian-packages --allow-untagged true
   sudo --non-interactive --preserve-env dm-upgrade-from-local-repository --target root --flavor whonix-workstation
elif [ "$TRAVIS_BRANCH" = "travis-workstation-pkg-only" ]; then
   ./build-steps.d/*_create-debian-packages --allow-untagged true
   sudo --non-interactive --preserve-env dm-upgrade-from-local-repository --target root --flavor whonix-workstation
else
   error "Unknown $TRAVIS_BRANCH branch, check $BASH_SOURCE."
fi

## }}

## Cleanup.
./help-steps/cleanup-files

## Debugging.
ls -la ../
ls -la /home/root/derivative-binary/
ls -la /home/root/derivative-binary/*.report

true "INFO $BASH_SOURCE: Showing report..."
cat /home/root/derivative-binary/*.report
true "INFO $BASH_SOURCE: Done, showed report."

true "INFO $BASH_SOURCE: Executing Run Initializer Gui..."
## Not installed when using "-minimal".
/usr/lib/first_run_initializer_gui || true
true "INFO $BASH_SOURCE: Executed First Run Initializer Gui."

systemctl --no-pager timesanitycheck start || true
systemctl --no-pager bootclockrandomization start || true
systemctl --no-pager msgcollector start || true
systemctl --no-pager sdwdate start || true

true "INFO $BASH_SOURCE: Executing systemcheck"
sudo --non-interactive --preserve-env -u travis bash -x systemcheck || true
true "INFO $BASH_SOURCE: Executed systemcheck."

## {{ Do not try to reboot Travis CI!

## Do not do this!!!
## Otherwise you'll run into...
## "I'm sorry but there was an error with the connection to the VM.
## Your job will be requeued shortly."
## reboot

## }}

## End.
true "INFO: End of: $BASH_SOURCE | $dist_build_error_counter error(s) detected."
