#!/bin/bash


print()
{

if(($1==2));then
    printf "\033[1;33m
        %-20s>> 1. 添加    MT  源\n
        %-20s>> 2. 添加    NI  源\n
        %-20s>> 3. 检查  MT/NI 源\n
        %-20s>> 4. 删除  MT/NI 源\n
        %-20s Q|q  返回上一级\n       
    \033[0m"
elif(($1==1));then
    printf "\033[1;34m
        %-20s> 1.源\n
        %-20s> 2.安装基础环境(visa-labview)\n
        %-20s> 3.安装 MT-RIO驱动包\n
        %-20s> 4.删除基础环境(visa-labviwe)\n
        %-20sq|Q 退出\n
\033[0m
"
fi
}

CheckSource()
{
    dpkg -l  | grep mt-software-stream-focal  1>/dev/null
    if (($?))
    then
        echo "
        未检测到 MT-源"
    else
        echo "
        MT-源 已安装"
    fi
    
    dpkg -l  | grep ni-software-stream-focal  1>/dev/null
    if (($?))
    then
        echo "
        未检测到 NI-源"
    else
        echo "
        NI-源 已安装"
    fi
}

DelSource()
{
    dpkg -l  | grep mt-software-stream-focal  1>/dev/null
    if (($?))
    then
    echo 
    else
        echo "
        正在移除 MT-源"
        sudo apt-get   autoremove   mt-software-stream-focal 
        sudo  dpkg -l | grep ^rc | cut -d' ' -f3 | sudo xargs dpkg --purge  &>/dev/null
    fi

    dpkg -l  | grep ni-software-stream-focal  &>/dev/null
    if (($?))
    then
        echo
    else
        echo "
        正在移除 NI-源"
        sudo apt-get   autoremove  --purge ni-software-stream-focal
        sudo  dpkg -l | grep ^rc | cut -d' ' -f3 | sudo xargs dpkg --purge &>/dev/null
    fi

    
}
AddNISource()
{
    dpkg -l  | grep ni-software-stream-focal  
    if (($?))
    then
#        wget -c https://mangotree.diskstation.me:9100/Software/Feed/ni-linux-desktop/stream/mt-ubuntu2004-driver-stream.deb
         wget -c https://download.ni.com/ni-linux-desktop/stream/ni-ubuntu2004-drivers-stream.deb
         sudo  dpkg  -i   ni-ubuntu2004-drivers-stream.deb
         rm -rf  ni-ubuntu2004-driver-stream.deb
         sudo  apt-get  update 
    else
         echo "
          NI-源 已添加"
    fi
    
}
AddMTSource()
{
    dpkg -l  | grep mt-software-stream-focal 
    if (($?))
    then
         wget -c https://mangotree.diskstation.me:9100/Software/Feed/ni-linux-desktop/stream/mt-ubuntu2004-driver-stream.deb
         sudo  dpkg  -i   mt-ubuntu2004-driver-stream.deb
         rm -rf  mt-ubuntu2004-driver-stream.deb
         sudo  apt-get  update 
    else
         echo "
          MT-源 已安装"
    fi
    
}

Source()
{
    while :
    do
    clear
    print 2
    read -p  "Enter selection  >" pigs
    case $pigs in
    1)
       AddMTSource
       CheckSource
       read -p  "Input any key continue..." 
       ;;
    2)
        AddNISource
        CheckSource
        read -p  "Input any key continue..." 
        ;;
    3)
       CheckSource
       read -p  "Input any key continue..." 
       ;;
    4)
       DelSource
       read -p  "Input any key continue..." 
       ;;
    
    q|Q) break
        ;;
    *)
        echo "error"
        read -p  "Input any key continue..." 
        ;;
    esac
    echo " "
done
}


checkfile()
{
    myPath="data/visa"
    myPath2="data.tar.gz"
    if [ ! -d "$myPath" ];then
        if [ !  -d "$myPath2" ];then
        wget -c https://mangotree.diskstation.me:9100/Software/Feed/ni-linux-desktop/stream/data.tar.gz
        tar -xvf $myPath2
        else
         tar -xvf $myPath2
        fi
    fi

}
AddVISA-runtime()
{
    sudo dpkg -i   data/visa-runtime/*.deb
}
DelSoftware()
{
    sudo  apt-get  autoremove ni-[a-t]*  ni-[v-z]*
    sudo  apt-get  autoremove mtrio-driver
    sudo  apt-get  update &>/dev/null
    sudo  dpkg  -l |  grep  "^ii  ni"|awk '{print $2}'|sudo  xargs  dpkg  -P  &>/dev/null
    dpkg -l | grep ^rc | cut -d' ' -f3 | sudo xargs dpkg --purge  &>/dev/null

}

AddVISA()
{
    sudo dpkg -i   data/visa/*.deb
    echo "

    installed VISA need to reboot
    
    "
}
AddMTRIO()
{
    sudo dpkg -i   data/MTRIO/*.deb
}


checkfile
while :
do
clear
print 1
read -p  "Enter selection [0-5] >" number
#echo "chose is >$number"
echo " "
case $number in
    1)
        Source
        read -p  "Input any key continue..." 
        ;;
    2)
        AddVISA
        read -p  "Input any key continue..." 
        
        ;;
    3)
       AddMTRIO
       read -p  "Input any key continue..." 
       
        
        ;;
    4)
       DelSoftware
       read -p  "Input any key continue..." 
        ;;
    5)
        checkfile
        read -p  "Input any key continue..." 
        ;;
    6)
        
        ;;
    7)
         
         ;;

    q|Q) exit
        ;;

    *)
        echo "error"
        read -p  "Input any key continue..." 
        ;;
esac
echo " "

done


 
