#!/bin/bash

nas_storage_directory_name="/share_data"
storage_directory_name="/dev/sdb"

function update_sources() {
	        printf "==========\n正在更新源\n==========\n"
		        echo "
			        deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
				        deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
					        deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
						        deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse
							        deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
								        deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
									        deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
										        deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
											        deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse
												        deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse" > /etc/apt/sources.list
													        apt update -y
														                }
																function create_apt_proxy() {
																	        printf "==========\n创建apt代理\n==========\n"
																		        apt install apt-cacher-ng -y

																			        echo "Port: 3142" >>  /etc/apt-cacher-ng/acng.conf

																				        echo 'CacheDir: /var/cache/apt-cacher-ng
																					        LogDir: /var/log/apt-cacher-ng
																						        Port: 3142' > /etc/apt-cacher-ng/acng.conf

																							        systemctl restart apt-cacher-ng

																								#       systemctl status apt-cacher-ng
																								        if systemctl is-actice apt-cacher-ng;then
																										                echo"apt-cacher-ng搭建完成"
																												        fi
																													                                    }

																																	    function create_nfs() {
																																		            printf "==========\n创建nfs磁盘\n==========\n"

																																			            apt install nfs-kernel-server -y
																																				            if ! systemctl is-active nfs-kernel-server; then
																																						                    echo "nfs-kernel-server下载完成"
																																								            fi

																																									            mkdir -p "$nas_storage_directory_name"

																																										            mkfs.ext4 "$storage_directory_name"

																																											            echo "$storage_directory_name  $nas_storage_directory_name     ext4    defaults        0       2" >>  /etc/fstab

																																												            mount -a

																																													            echo "$nas_storage_directory_name *(rw,sync,no_subtree_check)" >> /etc/exports

																																														            systemctl restart nfs-kernel-server

																																															            systemctl enable nfs-kernel-server

																																																            if ! systemctl is-active nfs-kernel-server; then
																																																		                    systemctl restart nfs-kernel-server
																																																				                    if systemctl is-active nfs-kernel-server; then
																																																							                            echo "NFS搭建完成"
																																																										                    else
																																																													                            echo "NFS服务启动失败"
																																																																                    fi
																																																																		            else
																																																																				                    echo "NFS服务已运行"
																																																																						            fi
																																																																							                                                                            }
																																																																																                                                                            
																																																																																    function create_pip_proxy() {
																																																																																	            printf "==========\n创建pip代理\n==========\n"

																																																																																		            apt install python3-pip -y

																																																																																			            mkdir -p ~/.pip

																																																																																				            echo '[global]
																																																																																					            index-url = https://pypi.tuna.tsinghua.edu.cn/simple' | tee ~/.pip/pip.conf

																																																																																						            pip install devpi-server devpi-client

																																																																																							            mkdir -p /var/www/html/devpi

																																																																																								            devpi-init --serverdir /var/www/html/devpi

																																																																																									            echo "
																																																																																										            [Unit]
																																																																																											            Description=Devpi Server
																																																																																												            After=network.target
																																																																																													            [Service]
																																																																																														            Type=simple
																																																																																															            ExecStart=/usr/local/bin/devpi-server --serverdir /var/www/html/devpi --host 0.0.0.0 --port 48080
																																																																																																            Restart=on-failure
																																																																																																	            User=root
																																																																																																		            Group=root
																																																																																																			            [Install]
																																																																																																				            WantedBy=multi-user.target" > /etc/systemd/system/devpi-server.service

																																																																																																					            systemctl daemon-reload

																																																																																																						            systemctl start devpi-server

																																																																																																							            systemctl enable devpi-server

																																																																																																								    #       systemctl status devpi-server

																																																																																																								            devpi use http://localhost:48080
																																																																																																									            
																																																																																																									            devpi login root --password=''

																																																																																																										            devpi index -c root/dev

																																																																																																											            devpi use root/dev

																																																																																																												            devpi list
																																																																																																													                                                                                                                                                            }

																																																																																																																																    echo "请选择执行的部分 (all, nfs, apt, devpi):"

																																																																																																																																    read -r section

																																																																																																																																    case "$section" in
																																																																																																																																	            all)
																																																																																																																																			                    update_sources
																																																																																																																																					                    create_apt_proxy
																																																																																																																																							                    create_nfs
																																																																																																																																									                    create_pip_proxy
																																																																																																																																											                    ;;
																																																																																																																																													            nfs)
																																																																																																																																															                    create_nfs
																																																																																																																																																	                    ;;
																																																																																																																																																			            apt)
																																																																																																																																																					                    update_sources
																																																																																																																																																							                    create_apt_proxy
																																																																																																																																																									                    ;;
																																																																																																																																																											            devpi)
																																																																																																																																																													                    create_pip_proxy
																																																																																																																																																															                    ;;
																																																																																																																																																																	            *)
																																																																																																																																																																			                    echo "无效的选项。请使用: all, nfs, apt, devpi"
																																																																																																																																																																					                    ;;
																																																																																																																																																																					    esac

