#!/bin/bash

usage() {
    echo "Usage: $0 l <ori_label_directory> [ -h ]"
    echo "对label图片进行处理及转化（-l不能为空） "
    echo "-l:原始label的路径，-h:帮助"
    echo "e.g. 4_rebuild_labels.sh -l ./C组标注图片"
    echo "接下来一路回车就ok"
}

ori_label_directorys=""

while getopts "hl:" opt; do
    case $opt in
        h)
            usage
            exit 0
            ;;
        l)
            ori_label_directorys=$OPTARG
            ;;
        *)
            echo  -e '\033[31m!!! Error, Illegal input !!!\033[0m'
            usage
            exit 1
            ;;
    esac
done

# 判断输入地址是否为空
if [ -z "$ori_label_directorys" ]; then
    echo -e "\033[31m输入地址 -i -l 存在空地址\033[0m"
    usage
    exit 1
fi

# 地址转化
ori_label_directory=$(readlink -f "$ori_label_directorys")
if [ -z "$ori_label_directory" ]; then
    echo -e "\033[31m无法解析地址，程序退出\033[0m"
    echo -e "\033[31mori_label_directory\033[0m: $ori_label_directorys"
    exit 1
fi
if [ ! -d "$ori_label_directory" ]; then
    echo -e "\033[31mlabel目录不存在，程序退出\033[0m"
    echo -e "\033[31mori_label_directory\033[0m: $ori_label_directorys"
    exit 1
fi

echo -e "\033[32m_____ 4_rebuild_labels.sh _____\033[0m"


echo -n "请选择label图片搜索深度(默认为1):"
read -r fold_search_depth
if [ -z $fold_search_depth ]; then
    fold_search_depth='1'
fi

save_pro_folds=""${ori_label_directory%/}"_pro_label_fold" # 去掉末尾的足/
save_pro_fold=$(readlink -f "$save_pro_folds")
echo -n "请选择初步处理label后label_pro图片存储位置(默认为$save_pro_fold):"
read -r save_pro_folds
if [ -z $save_pro_folds ]; then
    save_pro_folds=""${ori_label_directory%/}"_pro_label_fold"
fi
save_pro_fold=$(readlink -f "$save_pro_folds")

echo -n "请选择label_pro图片后缀(默认为\"_label\"):"
read -r pro_suffix_name
if [ -z $pro_suffix_name ]; then
    pro_suffix_name='_label'
fi

save_GT_folds=""${ori_label_directory%/}"_GT_label_fold"
save_GT_fold=$(readlink -f "$save_GT_folds")
echo -n "请选择处理label_pro后label_GT图片存储位置(默认为$save_GT_fold):"
read -r save_pro_folds
if [ -z $save_pro_folds ]; then
    save_GT_folds=""${ori_label_directory%/}"_GT_label_fold"
fi
save_GT_fold=$(readlink -f "$save_GT_folds")

echo -n "请选择label_GT图片后缀(默认为\"_gtFine_labelTrainIds\"):"
read -r GT_suffix_name
if [ -z $GT_suffix_name ]; then
    GT_suffix_name='_gtFine_labelTrainIds'
fi

echo -n "请选择label_GT图片通道数(1或3，默认为1):"
read -r GT_channel
if [ -z $GT_channel ]; then
    GT_channel='1'
fi
if [[ $GT_channel != '1' && $GT_channel != '3' ]]; then
    echo -e "\033[35mGT_channel只能为1或3，输入有误，将其默认变为1\033[0m"
    GT_channel='1'
fi

echo -n "请选择GT图片背景颜色(0或255，默认为0（黑色）)):"
read -r back_gnd_color
if [ -z $back_gnd_color]; then
    back_gnd_color='0'
fi

echo -n "请选择GT图片中第一类的颜色(黑色背景(0)下默认为1，白色背景(255)下默认为0):"
read -r first_class_color
if [ -z $first_class_color]; then
    if [ $back_gnd_color == '255' ]; then
        first_class_color='0'
    else
        first_class_color='1'
    fi
fi

echo -n "请选择图片类型(png或jpg，默认为png（没有\".\"）)):"
read -r pic_type
if [ -z $pic_type ]; then
    pic_type='png'
fi

echo -n "请选择重建起始目录(label或pro，默认为label):"
read -r Rebuild_from
if [ -z $Rebuild_from ]; then
    Rebuild_from='label'
fi
if [[ $Rebuild_from != 'label' && $Rebuild_from != 'pro' ]]; then
    echo -e "\033[35mRebuild_from只能为label或pro，输入有误，将其默认变为label\033[0m"
    Rebuild_from='label'
fi

echo -n "请选择重建最终目标(pro或GT，默认为GT):"
read -r Rebuild_to
if [ -z $Rebuild_to ]; then
    Rebuild_to='GT'
fi
if [[ $Rebuild_to != 'GT' && $Rebuild_to != 'pro' ]]; then
    echo -e "\033[35mRebuild_to只能为GT或pro，输入有误，将其默认变为GT\033[0m"
    Rebuild_to='GT'
fi

echo -n "请选择是否保存pro图片生成中间状态（e.g.灰度图等）(false或true，默认为false):"
read -r save_process_pics
if [ -z $save_process_pics ]; then
    save_process_pics='false'
fi
if [[ $save_process_pics != 'true' && $save_process_pics != 'false' ]]; then
    echo -e "\033[35msave_process_pics只能为true或false，输入有误，将其默认变为false\033[0m"
    save_process_pics='false'
fi

# 获取当前脚本的路径和名称
script_path=$(dirname "$0")
# 将当前目录更改为脚本所在的路径
cd "$script_path"

# 激活conda环境
source /home/"$USER"/miniconda/bin/activate Deal_pics
echo -e "\033[35m运行：\033[0mpython 4_deal_labels.py -src_fold $ori_label_directory -save_pro_fold $save_pro_fold -save_GT_fold $save_GT_fold -fold_search_depth $fold_search_depth -pro_suffix_name $pro_suffix_name -GT_suffix_name $GT_suffix_name -GT_channel $GT_channel -back_gnd_color $back_gnd_color -first_class_color $first_class_color -pic_type $pic_type -Rebuild_from $Rebuild_from -Rebuild_to $Rebuild_to -save_process_pics $save_process_pics "
echo ""
python 4_deal_labels.py -src_fold $ori_label_directory -save_pro_fold $save_pro_fold -save_GT_fold $save_GT_fold -fold_search_depth $fold_search_depth -pro_suffix_name $pro_suffix_name -GT_suffix_name $GT_suffix_name -GT_channel $GT_channel -back_gnd_color $back_gnd_color -first_class_color $first_class_color -pic_type $pic_type -Rebuild_from $Rebuild_from -Rebuild_to $Rebuild_to -save_process_pics $save_process_pics

echo "4_rebuild_label_pics.sh重构完毕"

