#!/bin/bash

this=$(readlink -f $0)
yast_orig=/sbin/yast2

if [[ -f "$yast_orig" ]]; then
    true
else  # Search for original yast script which we are wrapping
    for alt_path in /sbin/yast /sbin/yast2 /usr/sbin/yast2 /usr/sbin/yast /usr/bin/yast /bin/yast; do
        if [[ -f $alt_path ]] && [[ "$this" != $alt_path ]] && [[ ! -h "$alt_path" ]]; then
            yast_orig=$alt_path
        fi
    done
fi

if [[ ! -f "$yast_orig" ]]; then
    echo Error: $yast_orig: No such file or directory.
    exit 127
fi

if [[ -t 0 || -p /dev/stdin ]]; then  # Check if stdin is open on a terminal (or ssh socket).
    true
else
    exec -a $0 $yast_orig "$@"
fi

# Disable GUI mode in YaST2 - prevent GUI error message on standard output
unset DISPLAY

# Only prompt for the default yast menu or a few well-known submodules.
case "$*" in
    ""|lan|users|ntp-client|timezone|disk|firewall|host|sw_single|runlevel|security) 
        if dialog --yesno "!!! DO NOT CHANGE ANY CONFIGURATION VIA YAST !!!\n\nConfiguration changes must be made via firstinst-netw or recovery script.\n\nJump out?" 0 0; then
            exit 1;
        else
            exec -a $0 $yast_orig "$@";

        fi
        ;;
esac

exec -a $0 $yast_orig "$@"
