#!/bin/bash
#
# Generate a SIL-SA directory structure
#
# P1 == --split or --no-split
# P2 == the module name to use
# P3 .. extra parameter=value pairs like deviation=dev1b
#
# OR
#
# P1 == the module name to use
# P2 == --split or --no-split
# P3 .. extra parameter=value pairs like deviation=dev1b


function show_usage {
    echo "Usage: make_sil_sa_dir [--no-split | --split] <module-name> [more-parms]*"
    echo "Call from the parent directory where SIL-SA dir should be created"
    echo "   Add --no-split parameter to generate deprecated unified SIL files"
    echo "   The --sil-get2 and --sil-edit3 parameters are now the default"
    echo ""
    echo "Example:"
    echo "  make_sil_sa_dir test1 --sil-nmda=true --deviation=test1-dev"
    echo "  Directory <module-name> (e.g., test1) will be created"
}


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

mod=""

# default for split is now 1 starting in 21.10-1
needshift=0
split=1

if [ $# -ge 1 ]; then
    if [ "$1" = "--split" ]; then
        needshift=1
        mod=$2
        split=1
    elif [ "$2" = "--split" ]; then
        needshift=1
        mod=$1
        split=1
    elif [ "$1" = "--no-split" ]; then
        needshift=1
        mod=$2
        split=0
    elif [ "$2" = "--no-split" ]; then
        needshift=1
        mod=$1
        split=0
    else
        mod=$1
    fi
fi


if [ "$mod" = "" ]; then
    show_usage
    exit 1
fi

# new: add .sil suffix so same module or bundle can have both
# TBD does not work
#moddir=$mod.silsa
moddir=$mod

if [ -f $moddir ]; then
    echo "Error: File '$moddir' already exists. Delete and re-run make_sil_sa_dir"
    exit 1
fi

if [ -d $moddir ]; then
    echo "Error: Directory '$moddir' already exists. Delete and re-run make_sil_sa_dir"
    exit 1
fi

mkdir $moddir

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


mkdir $moddir/src
mkdir $moddir/bin
mkdir $moddir/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"
        rm -rf $moddir
        exit 1
    else
        echo "MODULE_NAME=$mod" > $moddir/src/Makefile
        cat /usr/share/yumapro/util/makefile.sil-sa >> $moddir/src/Makefile
        cp /usr/share/yumapro/util/makefile-top.sil $moddir/Makefile
    fi
else
    echo "YUMAPRO_HOME variable set: Using $YUMAPRO_HOME/util directory"
    if [ ! -f $YUMAPRO_HOME/util/makefile.sil-sa ]; then
        echo "Error: $YUMAPRO_HOME/util/makefile.sil-sa not found"
        rm -rf $moddir
        exit 1
    else
        echo "MODULE_NAME=$mod" > $moddir/src/Makefile
        cat $YUMAPRO_HOME/util/makefile.sil-sa >> $moddir/src/Makefile
        cp $YUMAPRO_HOME/util/makefile-top.sil $moddir/Makefile
    fi
fi

modparms=""

shift 1
if [ $needshift -eq 1 ]; then
    shift 1
fi

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

echo "modparms = $modparms"

cmnparms="--sil-sa --indent=4 --module=$mod --unified=true"

echo "cmnparms = $cmnparms"

cd $moddir/src

if [ $split -eq 1 ]; then
    $yangdump format=uh output=u_$mod.h $modparms $cmnparms || bad_exit
    $yangdump format=uc output=u_$mod.c $modparms $cmnparms || bad_exit
    $yangdump format=yh output=y_$mod.h $modparms $cmnparms || bad_exit
    $yangdump format=yc output=y_$mod.c $modparms $cmnparms || bad_exit
else
    $yangdump format=h output=$mod.h $modparms $cmnparms || bad_exit
    $yangdump format=c output=$mod.c $modparms $cmnparms || bad_exit
fi

cd ../..

check_doxygen $moddir
