#!/bin/bash

usage() {
    echo "Usage: $0 -i <ori_image_directory> -l <ori_label_directory> [-h]"
    echo "对image图片和label图片进行统一处理"
    echo "-i:原始图片的路径，-l:原始标签的路径，-h:帮助"
}

ori_image_directorys=""
ori_label_directorys=""
stack_result_directorys=""
stitch_result_directorys=""

while getopts "hl:i:" opt; do
    case $opt in
        h)
            usage
            exit 0
            ;;
        i)
            ori_image_directorys=$OPTARG
            ;;
        l)
            ori_label_directorys=$OPTARG
            ;;
        *)
            echo '!!! Error, Illegal input !!!'
            usage
            exit 1
            ;;
    esac
done

# 判断label、image是否都为空
echo $ori_label_directorys $ori_image_directorys
if [ -z "$ori_label_directorys" ] && [ -z "$ori_image_directorys" ]; then
    echo -e "\033[31mori_label_directory、ori_image_directory不能都为空\033[0m"
    usage
    exit 1
fi

# 进行绝对路径转化
ori_image_directory=$(readlink -f "$ori_image_directorys")
ori_label_directory=$(readlink -f "$ori_label_directorys")
if [ ! -d "$ori_label_directory" ] && [ ! -d "$ori_image_directory" ]; then
    echo "image、label都不存在，程序退出"
    echo -e "\033[31mori_image_directory\033[0m: $ori_image_directorys"
    echo "$ori_image_directory"
    echo -e "\033[31mori_label_directory\033[0m: $ori_label_directorys"
    echo "$ori_label_directory"
    exit 1
fi

# 激活conda环境
# source /home/"$USER"/miniconda/bin/activate Pat_infos

# 记录脚本所在路径
script_path=$(dirname "$0")
echo -e "\033[32m******* 开始Run运行Seg_data_run.sh批量图片处理程序 *******\033[0m"
# 进行操作选择
while true; do
    PS3='Please enter your choice: '
    options=("移动图片与重命名" "图片统一大小与类型" "image、label配对检测" "对label进行重建" "TOOL_image、label堆叠" "TOOL_image、label拼接" "Quit")
    echo -e "\033[35m____ Seg_data_run选择 ____\033[0m"
    echo -e "\033[35mImage所在地址：\033[0m$ori_image_directory"
    echo -e "\033[35mLabel所在地址：\033[0m$ori_label_directory"
    select opt in "${options[@]}"
    do
      case $opt in
        ### 选项1 ###
        "移动图片与重命名")
            echo -e "\033[31mRun运行:\033[0mbash $script_path/1_rename_pics.sh -i $ori_image_directory -l $ori_label_directory"
            bash $script_path/1_rename_pics.sh -i "$ori_image_directory" -l "$ori_label_directory"
            echo ""
            break
        ;; 

        ### 选项2 ###
        "图片统一大小与类型")
            echo -n "请输入宽度（默认1920）:"
            read -r width
            echo -n "请输入高度（默认1080）:"
            read -r height
            if [ -z "$width" ]; then
                width=1920
            fi
            if [ -z "$height" ]; then
                height=1080
            fi
            # 如果输入图片路径为空
            if [ -z $ori_image_directory ];then
                echo -e "\033[31mRun运行:\033[0mbash $script_path/2_reformate_pics.sh -l $ori_label_directory -w $width -h $height "
                bash $script_path/2_reformate_pics.sh -l $ori_label_directory -w $width -h $height 
            elif [ -z $ori_label_directory ];then
                echo -e "\033[31mRun运行:\033[0mbash $script_path/2_reformate_pics.sh -l $ori_label_directory -w $width -h $height "
                bash $script_path/2_reformate_pics.sh -i $ori_image_directory -w $width -h $height 
            else
                echo -e "\033[31mRun运行:\033[0mbash $script_path/2_reformate_pics.sh -i $ori_image_directory -l $ori_label_directory -w $width -h $height"
                bash $script_path/2_reformate_pics.sh -i $ori_image_directory -l $ori_label_directory -w $width -h $height 
            fi
            echo ""
            break
        ;; 
        
        ### 选项3 ###
        "image、label配对检测")
            while [ ! -d "$ori_image_directory" ]; do
                echo -e "\033[31mImage地址为:$ori_image_directorys，其不存在\033[0m"
                echo -n "请输入image所在地址:"
                read -r ori_image_directorys
                ori_image_directory=$(readlink -f "$ori_image_directorys")
            done
            while [ ! -d "$ori_label_directory" ]; do
                echo -e "\033[31mLabel地址为:$ori_label_directorys，其不存在\033[0m"
                echo -n "请输入label所在地址:"
                read -r ori_label_directorys
                ori_label_directory=$(readlink -f "$ori_label_directorys")
            done
            echo -n "请输入图片前缀文本（默认为\"\"）:"
            read -r prefix # 禁止转译
            echo -n "请输入图片后缀文本（非.png类后缀名，默认为\"\"）:"
            read -r suffix
            if [ -z $preffix ];then
                preffix=""
            fi
            if [ -z $suffix ];then
                suffix=""
            fi
            echo -e "\033[31mRun运行:\033[0mbash $script_path/3_pair_ori_label.sh -i $ori_image_directory -l $ori_label_directory -p $prefix -s $suffix"
            bash $script_path/3_pair_ori_label.sh -i $ori_image_directory -l $ori_label_directory -p "$prefix" -s "$suffix"
            echo ""
            break
        ;; 

        ### 选项4 ###
        "对label进行重建")
            # 判断Label目录是否存在
            while [ -z "$ori_label_directory" ]; do
                echo -e "\033[31mLabel地址为:$ori_label_directorys，其存在异常\033[0m"
                echo -n "请输入堆叠结果存储目录地址:"
                read -r ori_label_directorys
                ori_label_directory=$(readlink -f "$ori_label_directorys")
            done
            echo -e "\033[31mRun运行:\033[0mbash $script_path/4_rebuild_labels.sh -l $ori_label_directory"
            bash $script_path/4_rebuild_labels.sh -l $ori_label_directory
            echo ""
            break
        ;; 

        ### 选项5 ###
        "TOOL_image、label堆叠")
            while [ ! -d "$ori_image_directory" ]; do
                echo -e "\033[31mImage地址为:$ori_image_directorys，其不存在\033[0m"
                echo -n "请输入image所在地址:"
                read -r ori_image_directorys
                ori_image_directory=$(readlink -f "$ori_image_directorys")
            done
            while [ ! -d "$ori_label_directory" ]; do
                echo -e "\033[31mLabel地址为:$ori_label_directorys，其不存在\033[0m"
                echo -n "请输入label所在地址:"
                read -r ori_label_directorys
                ori_label_directory=$(readlink -f "$ori_label_directorys")
            done
            
            echo -n "请输入堆叠图片透明程度（0~1,0为最透明，默认为0.3）"
            read -r alpha
            echo -n "请输入图片前缀文本（默认为\"\"）:"
            read -r prefix
            echo -n "请输入图片后缀文本（非.png类后缀名，默认为\"\"）:"
            read -r suffix
            if [ -z "$alpha" ]; then
                alpha="0.3"
            fi
            stack_result_directory=""$ori_label_directory"_堆叠_"$alpha"_透明度"
            echo -n "请输入堆叠结果存储目录(默认为$stack_result_directory)："
            read -r stack_result_directorys
            # 判断堆叠目录非为空则进行转换
            if [ ! -z "$stack_result_directorys" ]; then
                stack_result_directory=$(readlink -f "$stack_result_directorys")
            fi
            # 判断Label目录是否存在
            while [ -z "$ori_label_directory" ]; do
                echo -e "\033[31mLabel地址为:$ori_label_directorys，其存在异常\033[0m"
                echo -n "请输入堆叠结果存储目录地址:"
                read -r ori_label_directorys
                ori_label_directory=$(readlink -f "$ori_label_directorys")
            done
            echo -e "\033[31mRun运行:\033[0mbash $script_path/5_TOOL_stack_pics.sh -i $ori_image_directory -l $ori_label_directory -p $prefix -s $suffix -a $alpha -r $stack_result_directory"
            bash $script_path/5_TOOL_stack_pics.sh -i "$ori_image_directory" -l "$ori_label_directory" -p "$prefix" -s "$suffix" -a "$alpha" -r "$stack_result_directory"
            echo ""
            break
        ;; 

        ### 选项6 图片拼接 ### 
        "TOOL_image、label拼接")
            while [ ! -d "$ori_image_directory" ]; do
                echo -e "\033[31mImage地址为:$ori_image_directorys，其不存在\033[0m"
                echo -n "请输入image所在地址:"
                read -r ori_image_directorys
                ori_image_directory=$(readlink -f "$ori_image_directorys")
            done
            echo "1.label目前为\"$ori_label_directory\""
            echo -n "是否调整label所在文件夹(默认为不调整，有输入视为调整):"
            read -r temp
            while [ ! -z "$temp" ]; do
                read -r ori_label_directorys
                ori_label_directorys=$(readlink -f "$ori_label_directorys")
            done
            while [ ! -d "$ori_label_directory" ]; do
                echo -e "\033[31mLabel地址为:$ori_label_directorys，其不存在\033[0m"
                echo -n "请输入label所在地址:"
                read -r ori_label_directorys
                ori_label_directory=$(readlink -f "$ori_label_directorys")
            done
            stitch_result_directory=""$ori_label_directory"_拼接"
            echo -n "2.请输入堆叠结果存储目录(默认为$stitch_result_directory)："
            read -r stitch_result_directorys
            # 判断堆叠目录非为空则进行转换
            if [ ! -z "$stitch_result_directorys" ]; then
                stitch_result_directory=$(readlink -f "$stitch_result_directorys")
            fi

            echo -n "3.请输入图片前缀文本（默认为\"\"）:"
            read -r prefix
            echo -n "4.请输入图片后缀文本（非.png类后缀名，默认为\"\"）:"
            read -r suffix

            echo -e "\033[31mRun运行:\033[0mbash $script_path/6_TOOL_stitch_pics.sh -i $ori_image_directory -l $ori_label_directory -p "$prefix" -s "$suffix" -r "$stitch_result_directory""
            bash $script_path/6_TOOL_stitch_pics.sh -i "$ori_image_directory" -l "$ori_label_directory" -p "$prefix" -s "$suffix" -r "$stitch_result_directory"
            exit 0
            ;;

        ### 选项7 退出 ### 
        "Quit")
            echo -e "\033[35mSeg_data_run.sh Exiting...\033[0m"
            exit 0
            ;;
          
        *)
            echo "Invalid option: $REPLY"
            echo -e ""
            break
            ;;
      esac
    done
done