#!/bin/bash
rm -rf Dockerfile >>/dev/null
ID=`docker run --rm $1 cat /etc/os-release  | grep -E "^ID="|awk -F"=" '{print $2}' |xargs`
VERSION_ID=`docker run --rm $1 cat /etc/os-release  | grep -E "^VERSION_ID="|awk -F"=" '{print $2}' |xargs`
image=$1
framework_name_version=$3
newimage=$2
#echo $ID
#echo $VERSION_ID
dockerfile_from(){
	echo "FROM $image" >>Dockerfile

}
dockerfile_python3(){
	docker run --rm $image  python -V >/dev/null 2>&1
	if [ $? -ne 0 ];then
        	if [ $ID==ubuntu ];then
    		    	echo "RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \\
                	python3.6-dev \\
                	python3-pip \\
            	  	python3-setuptools \\
                	&& apt-get clean \\
                	&& rm -rf /var/lib/apt/lists/*" >>Dockerfile
			PYTHONPATH=/usr/local/lib/python3.6/dist-
			echo "RUN ln -s /usr/bin/python3.6 /usr/bin/python \\
			   	  && ln -s /usr/bin/pip3 /usr/bin/pip \\
			     	  && pip install -i https://pypi.doubanio.com/simple/ -U pip" >>Dockerfile
                #        echo "$PYTHONPATH"
		fi
	else
	docker run --rm  $image  find / -type d -name python  >>a.tmp 2>&1
	PYTHONPATH=`grep package a.tmp| grep  lib/python  | awk -F"packages" '{print $1}' | uniq  | head -n 1`
#	echo "$PYTHONPATH"
	rm -rf a.tmp
	fi
	dockerfile_jupyter

}
dockerfile_ssh(){
#	docker run --rm $2  ssh -V
	if [ $ID==ubuntu ];then
	echo "COPY ./sources.list.${VERSION_ID} /etc/apt/ources.list">>Dockerfile
	echo -e "RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \\
       		 vim \\
        	curl \\
        	git \\
        	unzip \\
        	zip \\
       		wget \\
        	openssh-server iputils-ping locales tzdata \\
        	&& sed -i \"s/.*PermitRootLogin.*/PermitRootLogin yes/g\" /etc/ssh/sshd_config \\
        	&& apt-get clean \\
        	&& rm -rf /var/lib/apt/lists/*" >>Dockerfile
	fi
}
dockerfile_jupyter(){
	echo "RUN mkdir /etc/jupyter" >>Dockerfile
	echo "COPY jupyter_notebook_config.py /etc/jupyter/" >>Dockerfile
	echo "COPY run.sh /usr/local/bin/" >>Dockerfile
	echo "RUN chmod +x /usr/local/bin/run.sh"
	echo -e  "RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \\
        && pip config set global.index-url https://pypi.doubanio.com/simple/ \\
        && pip install --no-cache-dir notebook==6.1.5 \\
        && sed -i \"s/DELETE/DELETE', 'OPTIONS/g\" ${PYTHONPATH}packages/notebook/terminal/api_handlers.py \\
        && sed -i \"/parent=nb_app/a\        term_settings={'cwd': nb_app.notebook_dir},\" ${PYTHONPATH}packages/notebook/terminal/__init__.py" >>Dockerfile


}
dockerfile_expose_cmd(){
	echo -e "EXPOSE 22\n# IPython\nEXPOSE 8888\nENV LANG=C.UTF-8\nCMD [\"run.sh\"]
	" >>Dockerfile


}
dockerfile_install_framwork(){
	
	echo "RUN pip install ${framework_name_version}" >>Dockerfile
}
dockerfile_upgrade_framwork(){
	
	echo "RUN pip install --upgrade ${framework__version}" >>Dockerfile

}
help(){ 
	echo -e "please run this script as below:\n "
	echo -e "if you want to delivery new image to AI platfrom use  ./operate_image.sh  imagename newimage\n \n EX ./operate_image.sh nvidia/cuda:11.4.1-cudnn8-devel-ubuntu18.04  192.168.20.177:38080/frame-system/cuda:11.4.1-cudnn8-devel-ubuntu18.04 \n"
	echo -e "if you want to pip install framwork or sofrware use  ./operate_image.sh imagename newimage framwork_name==framwork_version \n\n EX ./operate_image.sh  nvidia/cuda:11.4.1-cudnn8-devel-ubuntu18.04  192.168.20.177:38080/frame-system/cuda:11.4.1-cudnn8-devel-ubuntu18.04 tensorflow-gpu==2.3.0 "
}
docker_build(){
   docker build -t ${newimage} .
 
}
dockerfile_join(){
 #	echo $#
	case $# in
	3)             
		dockerfile_from 
		dockerfile_ssh
		dockerfile_python3
		dockerfile_install_framwork
		;;
	2)
		dockerfile_from 
		dockerfile_ssh
		dockerfile_python3
                ;;
	*)
		help
		exit 127
		;;
	esac
        dockerfile_expose_cmd
	docker_build
}
dockerfile_join $@
