#!/bin/bash
TARGET=$1
OPT_RPM=$2
if [[ -z $TARGET ]]; then
    echo "ERROR: No target defined."
    echo "Usage: $0 TARGET"
fi

if [[ ! -z $( echo "$TARGET" | grep '\.' ) ]]; then
    LTARGET=$TARGET
else
    LTARGET=$( /usr/local/bin/longname $TARGET )
    if [[ -z $LTARGET ]]; then
        echo "ERROR: Could not get long name of $TARGET."
        echo "INFO : Please retry with FQDN of $TARGET."
    fi
fi

SSH=$( which ssh )
if [[ $? -ne 0 ]]; then
    echo "ERROR: No ssh found"
    exit 1
fi

SCP=$( which scp )
if [[ $? -ne 0 ]]; then
    echo "ERROR: No scp found"
    exit 1
fi

$SSH $LTARGET /usr/local/sbin/marsconfigure_minion disable

$SCP /usr/local/sbin/minstall_helper.py ${LTARGET}:/tmp

python3=$( $SSH $LTARGET which python3 )
if [[ $? -eq 0 ]]; then
    $SSH $LTARGET $python3 /tmp/minstall_helper.py uninstall=true $OPT_RPM
else
    $SSH $LTARGET python /tmp/minstall_helper.py uninstall=true $OPT_RPM
fi
$SSH $LTARGET rm -f /tmp/minstall_helper.py

marsng -o -rh $TARGET
salt-key -yd $LTARGET