#!/bin/bash

if [ `id -u` -ne 0 ];then
	echo "请使用root用户执行此脚本！"
	exit 0
fi

var="d5000"
existflag=1	#default exist directory /home/d5000/conf...

echo "检测用户及用户组...."
user=`grep "^$var:" /etc/passwd`
if [ "$user" = "" ];then
	echo "程序安装失败，退出！"
	echo "失败原因：d5000用户不存在！"
	exit 0
fi

group=`grep "^$var:" /etc/group`
if [ "$group" = "" ];then
	echo "程序安装失败，退出！"
	echo "失败原因：d5000用户组不存在！"
	exit 0
fi

echo "检测用户家目录...."
home=`grep "^$var" /etc/passwd | awk -F: '{print $6}'`
if [ "$home" = "" ];then
	echo "找不到d5000用户的家目录，安装程序退出！"
	exit 0
fi

conf=${home}"/conf"
bin=${home}"/bin"
lib=${home}"/lib"
nic=${home}"/conf/nic"

echo "检测配置文件目录...."
if [ ! -d $conf ];then
	mkdir "$conf"
	existflag=0
fi

if [ ! -d $bin ];then
	mkdir "$bin"
	existflag=0
fi

if [ ! -d $lib ];then
	mkdir "$lib"
	existflag=0
fi

if [ ! -d $nic ];then
	mkdir "$nic"
	existflag=0
fi

if [ "$existflag" = "0" ];then
	echo "配置文件目录不存在，创建所需目录...."
	chown -R d5000:d5000 "$home"
fi

echo "检测旧版本程序...."
upgrade=`pkginfo -i | grep sys_nicmonitor`
if [ -n "$upgrade" ];then
	echo "存在旧版本程序，开始升级...."
	`pkgadd -u sys_nicmonitor#1.7-x86_64-linx-Rocky4.2.pkg.tar.gz`
	`pkgadd -u dotconf#1.0.13-1.pkg.tar.gz`
	chmod u+s $bin"/sys_nicmonitor"
	echo "程序升级成功！"
else
	echo "未找到旧版本程序，开始安装...."
	`pkgadd sys_nicmonitor#1.7-x86_64-linx-Rocky4.2.pkg.tar.gz`
	`pkgadd dotconf#1.0.13-1.pkg.tar.gz`
	chmod u+s $bin"/sys_nicmonitor"
	echo "程序安装成功！"
fi

exit 0
