#!/usr/bin/bash
#
# Generate a SIL-SA directory structure
# for a SIL-SA bundle
# P1 == the bundle name to use
# P2 - PN == a module name to use in the SIL bundle

function show_usage {
    echo ""
    echo "Usage: make_sil_sa_bundle <bundle-name> 1*<module-name> 0*(deviation=<name>)*"
    echo ""
    echo "Call from the parent directory where SIL-SA bundle dir should be created."
    echo "At least 1 module name must be specified."
    echo "No modules can have the same name as the bundle name."
    echo "Paraemters such as --deviation and --sil-nmda can be set at the end of the command line."
    echo ""
    echo "Example:  make_sil_sa_bundle bundle1 test1 test2 test3 test4"
    echo "Outputs:  Ddirectory <bundle-name> will be created (e.g. bundle1)"
}

function bad_exit {
    echo "yangdump-sdk failed; Error exit"
    exit 1
}


function check_doxygen {
    pathstr=$(which doxygen)
    if [ ! -x "$pathstr" ]
    then
        echo "Install doxygen to use the YumaPro Doxygen browser"
        echo ""
        echo "Ubuntu: sudo apt-get install doxygen graphviz"
        echo "Fedora: sudo dnf install doxygen graphviz"
        echo "Centos: sudo dnf --enablerepo=powertools install doxygen graphviz"
        echo ""
        echo "After installation, follow the steps below"
        echo ""
    fi

    echo "Run the following commands to get started:"
    echo ""
    echo "  cd $1"
    echo "  make doc"
    echo "  make opendoc"
    echo ""
}


#yangdump=yangdump-sdk
# OK starting 21.10-4
yangdump=yangdump-pro


# this new dir suffix causes jenkins tests to fail so still TBD
#suffix=silsa

if [ $# -le  1 ]; then
    show_usage
    exit 1
fi

bundle=$1
#bundledir=$1.$suffix
bundledir=$1


if [ -f $bundledir ]; then
    echo "Error: File '$bundledir' already exists"
    show_usage
    exit 1
fi

if [ -d $bundledir ]; then
    echo "Error: Directory '$bundledir' already exists"
    show_usage
    exit 1
fi

echo "Creating SIL-SA bundle directory '$bundledir'"

mkdir $bundledir

if [ ! -d $bundledir ]; then
    echo "Error: Make directory '$bundledir' failed"
    show_usage
    exit 1
fi

mkdir $bundledir/src
mkdir $bundledir/bin
mkdir $bundledir/lib

if [ -z $YUMAPRO_HOME ]; then
    if [ ! -f /usr/share/yumapro/util/makefile.sil-sa ]; then
        echo "Error: /usr/share/yumapro/util/makefile.sil-sa not found"
        show_usage
        rm -rf $bundledir
        exit 1
    else
        echo "MODULE_NAME=$bundle" > $bundledir/src/Makefile
        cat /usr/share/yumapro/util/makefile.sil-sa >> $bundledir/src/Makefile
        cp /usr/share/yumapro/util/makefile-top.sil $bundledir/Makefile
    fi
else
    if [ ! -f $YUMAPRO_HOME/util/makefile.sil-sa ]; then
        echo "Error: $YUMAPRO_HOME/util/makefile.sil-sa not found"
        show_usage
        rm -rf $bundledir
        exit 1
    else
        echo "MODULE_NAME=$bundle" > $bundledir/src/Makefile
        cat $YUMAPRO_HOME/util/makefile.sil-sa >> $bundledir/src/Makefile
        cp $YUMAPRO_HOME/util/makefile-top.sil $bundledir/Makefile
    fi
fi

cd $bundledir/src

modparms=""

shift 1
while [ $# -ne 0 ]
do
    modparms+=" $1"
    shift 1
done

echo "modparms = $modparms"

cmnparms="sil-sa sil-bundle=$bundle indent=4 unified=true defnames=true"
echo "cmnparms = $cmnparms"

$yangdump $cmnparms format=uh $modparms || bad_exit
$yangdump $cmnparms format=uc $modparms || bad_exit
$yangdump $cmnparms format=yh $modparms || bad_exit
$yangdump $cmnparms format=yc $modparms || bad_exit
$yangdump $cmnparms format=bh $modparms || bad_exit
$yangdump $cmnparms format=bc $modparms || bad_exit

cd ../..

check_doxygen $bundledir
