#!/bin/bash

function bail () { echo "Error: ${1:-Unknown Error}\n"; exit ${2:-1}; }

instance=${1:-Unset}
[[ $instance == "Unset" ]] && bail "Specify instance as parameter"

function obliterate_changes() {
    P4PORT=${1}
    last_change=$(p4 -p $P4PORT -u bruno changes | grep import_ws | head -1 | cut -d' ' -f 2) 
    start=$((last_change+1))
    cmd="p4 -p $P4PORT obliterate -y //...@$start,now"
    echo $cmd
    $cmd | tail -5
    p4 -p $P4PORT admin journal -z 
}

p4port=$(ps ax | grep "p4d_${instance}" | grep -v grep | head -1 |perl -ne 'print "$1" if /\-p\s*([0-9.:]+)/')
#del_clients irp210-h04:${instance}666
#del_clients irp210-h03:${instance}666
#del_clients irp210-h02:${instance}666
obliterate_changes $p4port

