#!/bin/bash
# Cleans all build artifacts and custom config from the repo.
# Intended for use on developer workstations. Should NOT be run
# on production Admin Workstations.
set -e
set -u

USE_PODMAN="${USE_PODMAN:-}"

# Allow opting into using podman with USE_PODMAN=1
if  [[ -n "${USE_PODMAN}" ]]; then
    DOCKER_BIN="podman"
else
    DOCKER_BIN="docker"
fi

function remove_unwanted_files() {

    # Any Vagrant VMs should be destroyed. We check for the .vagrant/
    # directory, because it'll be created by vagrant otherwise, breaking
    # idempotence for the cleanup action.
    if hash vagrant &> /dev/null && [[ -d .vagrant/ ]]
    then
        printf "Removing vagrant VMs..."
        # Using `|| true` as different versions of Vagrant exit different
        # status codes depending on VM state during destroy.
        vagrant destroy -f > /dev/null || true
        printf " done\n"
    fi

    if hash molecule &> /dev/null
    then
        printf "Removing Molecule staging environment..."
        staging_scenario_name="$(./devops/scripts/select-staging-env)"
        molecule destroy -s "$staging_scenario_name" > /dev/null
        printf " done\n"
    fi

    # Remove any Onion URL info from previous instances.
    rm -vf install_files/ansible-base/app-journalist.auth_private \
        install_files/ansible-base/app-sourcev3-ths \
        install_files/ansible-base/app-ssh.auth_private \
        install_files/ansible-base/mon-ssh.auth_private \
        install_files/ansible-base/tor_v3_keys.json \
        build/*.deb

    # Remove any Onion URL from make dev-tor
    if $DOCKER_BIN volume inspect sd-onion-services > /dev/null; then
        $DOCKER_BIN volume remove sd-onion-services
    fi

    # Remove extraneous copies of the git repos, pulled in
    # via the Molecule upgrade testing scenario.
    rm -rf molecule/upgrade/.molecule/sd-orig \
        molecule/vagrant-packager/.molecule/sd-orig

    # Python bytecode, left over from tests or local app runs.
    find "$PWD" -type f -iname '*.pyc' -delete

    # Ansible playbook retry files, which are never used.
    find "$PWD" -type f -iname '*.retry' -delete

    # Static assets generated by local dev, as well as
    # virtualenvs.
    rm -rvf securedrop/static/.webassets-cache .venv*

    # Any and all git-ignored files and directories, if present. Includes e.g.
    # deb packages in build/, and VM information stored locally.
    git clean -x -d -f
}


function confirmation_prompt() {
    # Display scary warning as last chance to avoid destructive action.
    cat <<-WARNING_MESSAGE
WARNING: Proceeding will destroy all local customizations, including
application configuration, user credentials, Onion URLs, static assets,
VMs, and all git-ignored files. It is NOT possible to undo these changes!
WARNING_MESSAGE

    # We want to avoid mistakes here, so force developers
    # to type 'yes' in order to confirm and proceed with cleanup.
    read -r -p "To proceed, type 'yes': " user_confirmation
    case "${user_confirmation}" in
        yes ) remove_unwanted_files;;
        * ) printf "Action declined, exiting...\\n"
    esac
}


confirmation_prompt
