#!/bin/bash
#export WELCOME_TITLE="Welcome to My SerVice"
#grep "$WELCOME_TITLE" /root/.bashrc &> /dev/null || echo 'echo -e "\033[1;32m'${WELCOME_TITLE}'\033[0m"' >> /root/.bashrc
#!/bin/bash
Comfy_UI_Migrate() {
SOURCE_ROOT="/root/comfyui/ComfyUI"
DEST_ROOT="/home/suanba/ComfyUI"
TARGET_DIRS=("models" "input" "output" "user")

# 创建目标文件夹
mkdir -p "$DEST_ROOT"

[ -f  /etc/storage/file/base-storage ] && rm -rf /etc/storage/file/base-storage
# 检查/home/suanba/ComfyUI/是否存在
if [ ! -d "$DEST_ROOT" ]; then
  echo "目标目录 $DEST_ROOT 不存在，请检查路径或手动创建。"
  exit 1
fi

# 同步文件夹内容：只移动在源目录中有但在目标目录中没有的项目，并建立软连接
for dir in "${TARGET_DIRS[@]}"; do
  SOURCE_DIR="$SOURCE_ROOT/$dir"
  DEST_DIR="$DEST_ROOT/$dir"

  # 如果目标目录不存在，则直接移动整个源目录并创建软链接
  if [ ! -d "$DEST_DIR" ]; then
    mv -f "$SOURCE_DIR" "$DEST_ROOT/"
    ln -sfn "$DEST_DIR" "$SOURCE_ROOT/$dir"
    continue
  fi

  # 对于每个源目录中的文件或子目录，检查是否存在于目标目录中
  for item in "$SOURCE_DIR"/*; do
    base_name=$(basename "$item")
    dest_item="$DEST_DIR/$base_name"

    # 如果项目在目标目录中不存在，则移动它
    if [ ! -e "$dest_item" ]; then
      mv -f "$item" "$DEST_DIR/"
    fi
  done

  # 确保符号链接指向的是新的位置
  if [ -d "$DEST_DIR" ]; then
    rm -rf "$SOURCE_DIR"  # 删除旧的源目录（如果存在）
    ln -sfn "$DEST_DIR" "$SOURCE_ROOT/$dir"  # 创建新的符号链接
  fi
done

echo "操作完成。"

}

Echomimic_init() {

# 定义路径变量
DEST_DIR="/home/suanba/Echomimic-V2"
MODELS_DIR="$DEST_DIR/models"
OUTPUTS_SRC="/root/echomimic_v2/outputs"
OUTPUTS_DST="$DEST_DIR/outputs"
OUTPUTS_LINK="/root/echomimic_v2/outputs"

# 创建目标目录和models子目录
mkdir -p "$MODELS_DIR"

# 移动outputs文件夹到新的位置
mv "$OUTPUTS_SRC" "$DEST_DIR"

# 检查移动操作是否成功
if [ $? -eq 0 ]; then
    echo "outputs 文件夹已成功移动到 $DEST_DIR."

    # 创建软连接回到原来的位置
    ln -sfn "$OUTPUTS_DST" "$OUTPUTS_LINK"
    
    if [ $? -eq 0 ]; then
        echo "创建软连接成功: $OUTPUTS_LINK -> $OUTPUTS_DST."
    else
        echo "创建软连接失败!"
        exit 1
    fi
else
    echo "移动 outputs 文件夹失败!"
    exit 1
fi

echo "脚本执行完毕."
}

WebUI_init() {
# 定义路径变量
DEST_DIR="/home/waas/SDWebui/"
MODELS_SOURCE="/root/stable-diffusion-webui-1.9.4/models"
OUTPUTS_DST="$DEST_DIR/outputs"
#OUTPUTS_LINK="/root/echomimic_v2/outputs"
EXTENSION_SRC="/root/stable-diffusion-webui-1.9.4/extensions"

# 创建目标目录和models子目录
if [ ! -d ${DEST_DIR} ] ;then
        mkdir -p $DEST_DIR
        mkdir -p $OUTPUTS_DST
        mv $EXTENSION_SRC $DEST_DIR
        mv $MODELS_SOURCE $DEST_DIR
else
        if [ ! -d $OUTPUTS_DST ];then
                mkdir -p $OUTPUTS_DST
        fi
        if [ ! -d $DEST_DIR/models ];then
                mv $MODELS_SOURCE $DEST_DIR
        fi
        if [ ! -d $DEST_DIR/extensions ];then
                mv $EXTENSION_SRC $DEST_DIR
        fi

fi
# 移动outputs文件夹到新的位置
}
mkdir /etc/suanba-script
wget -q  https://file.aigate.cc/suanba/region1/v1/suanba-welcome.sh -O /etc/suanba-script/suanba-welcome.sh && echo "下载suanba-welcome.sh文件成功" || echo "下载suanba-welcome.sh文件失败"
wget -q  https://file.aigate.cc/suanba/region1/v1/cron-root-stoage.sh -O /etc/suanba-script/cron-root-stoage.sh && echo "cron-root-stoage.sh文件成功" || echo "下载cron-root-stoage.sh文件失败"
chmod +x /etc/suanba-script/cron-root-stoage.sh
grep "source /etc/suanba-script/suanba-welcome.sh" /root/.bashrc  &> /dev/null || echo "source /etc/suanba-script/suanba-welcome.sh" >> /root/.bashrc
grep "alias df='df /home/suanba'" /root/.bashrc &> /dev/null  || echo "alias df='df /home/suanba'" >> /root/.bashrc
if printenv |grep CUDA_VISIBLE_DEVICES &> /dev/null ;then
       sed -i '2i export CUDA_VISIBLE_DEVICES= ' /etc/suanba-script/suanba-welcome.sh
       grep "export CUDA_VISIBLE_DEVICES=" /root/.bashrc &> /dev/null ||  echo "export CUDA_VISIBLE_DEVICES=" >> /root/.bashrc
       grep "alias nvidia-smi='echo -n 不支持'" /root/.bashrc &> /dev/null ||  echo "alias nvidia-smi='echo -n "不支持"'" >> /root/.bashrc
       grep "readonly CUDA_VISIBLE_DEVICES" /root/.bashrc &> /dev/null || echo "readonly CUDA_VISIBLE_DEVICES" >>  /root/.bashrc
else
	grep "export CUDA_VISIBLE_DEVICES=" /etc/suanba-script/suanba-welcome.sh &> /dev/null && sed -i '/export CUDA_VISIBLE_DEVICES=/d' /etc/suanba-script/suanba-welcome.sh
	grep "export CUDA_VISIBLE_DEVICES=" /root/.bashrc &> /dev/null &&  sed -i '/export CUDA_VISIBLE_DEVICES=/d' /root/.bashrc
	grep "alias nvidia-smi='echo -n 不支持'" /root/.bashrc &> /dev/null &&  sed -i '/alias nvidia-smi='echo -n 不支持'/d' /root/.bashrc
	grep "readonly CUDA_VISIBLE_DEVICES" /root/.bashrc &> /dev/null && sed -i '/readonly CUDA_VISIBLE_DEVICES/d' /root/.bashrc
fi
	
printenv |grep WAAS_COMFYUI_IMAGE   &> /dev/null  && Comfy_UI_Migrate
printenv |grep ECHOMIMIC_IMAGE   &> /dev/null  &&  Echomimic_init
printenv |grep WAAS_SDWEBUI   &> /dev/null  &&  WebUI_init
if printenv |grep ECHO_BASH   &> /dev/null ;then
   grep ${ECHO_BASH} &> /dev/null || echo $ECHO_BASH >> /root/.bashrc
fi
cat << EOF > /root/miniconda3/share/jupyter/kernels/python3/kernel.json
{
 "argv": [
  "python",
  "-m",
  "ipykernel_launcher",
  "-f",
  "{connection_file}"
 ],
 "display_name": "Python 3 (ipykernel)",
 "language": "python",
 "metadata": {
  "debugger": true
 },
 "env": {
        "PATH": "/root/miniconda3/bin:/root/miniconda3/condabin:/usr/local/nvidia/bin:/usr/local/cuda/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
        "SHELL": "/bin/bash"
    }
}
EOF
#git config --global url."https://ghfast.top/".insteadOf https://
while true;do /etc/suanba-script/cron-root-stoage.sh ;sleep 30;done &
/usr/sbin/sshd -D $SSHD_OPTS &
/root/miniconda3/bin/jupyter lab  --ServerApp.token="$PASSWORD"  &
if printenv |grep OLLAMA_WAAS_IMAGES_ONLY &> /dev/null ;then
        /usr/bin/nohup  /usr/bin/ollama serve > /var/log/ollamaserv.log 2>&1 &
        cd /home/ollama-webui/nextjs-ollama-llm-ui && /usr/local/bin/npm run dev  > /var/log/ollama-webui.log 2>&1  &
fi

/usr/bin/code-server --auth password --bind-addr 0.0.0.0:8080  --locale="zh-cn"
 
