download.sh 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #!/bin/sh
  2. _use_backup="$2"
  3. function download_file() {
  4. downloader="wget"
  5. type wget >/dev/null 2>&1 || { downloader="curl"; }
  6. if [ "$downloader" = "wget" ]; then
  7. _down_prefix="O"
  8. else
  9. _down_prefix="o"
  10. fi
  11. _down_symbol=0
  12. if [ ! -f "$2" ]; then
  13. $downloader "$1" -$_down_prefix "$2" >/dev/null 2>&1 && \
  14. echo "$1 完成! $2" && _down_symbol=1
  15. else
  16. echo "$2 已存在!" && _down_symbol=1
  17. fi
  18. if [ $_down_symbol == 0 ]; then
  19. echo "下载 $1 失败!请检查网络连接!"
  20. rm -rf "$2"
  21. return 1
  22. fi
  23. return 0
  24. }
  25. function lib_download_link() {
  26. if [ "$_use_backup" = "yes" ]; then
  27. case $1 in
  28. "php") echo "https://www.php.net/distributions/php-$2.tar.gz" ;;
  29. "swoole") echo "https://pecl.php.net/get/swoole-$2.tgz" ;;
  30. "hash") echo "https://pecl.php.net/get/hash-$2.tgz" ;;
  31. "inotify") echo "https://pecl.php.net/get/inotify-$2.tgz" ;;
  32. "redis") echo "https://pecl.php.net/get/redis-$2.tgz" ;;
  33. "libxml2") echo "http://xmlsoft.org/sources/libxml2-$2.tar.gz" ;;
  34. "liblzma") echo "https://github.com/kobolabs/liblzma/archive/refs/heads/master.zip" ;;
  35. "curl") echo "https://curl.haxx.se/download/curl-$2.tar.gz" ;;
  36. *) echo "unknown" ;;
  37. esac
  38. else
  39. case $1 in
  40. "php") echo "http://mirrors.zhamao.xin/php/php-$2.tar.gz" ;;
  41. "swoole") echo "http://mirrors.zhamao.xin/pecl/swoole-$2.tgz" ;;
  42. "hash") echo "http://mirrors.zhamao.xin/pecl/hash-$2.tgz" ;;
  43. "inotify") echo "http://mirrors.zhamao.xin/pecl/inotify-$2.tgz" ;;
  44. "redis") echo "http://mirrors.zhamao.xin/pecl/redis-$2.tgz" ;;
  45. "libxml2") echo "https://dl.zhamao.me/libxml2/libxml2-$2.tar.gz" ;;
  46. "liblzma") echo "https://dl.zhamao.me/liblzma/liblzma.tar.gz" ;;
  47. "curl") echo "https://dl.zhamao.me/curl/curl-$2.tar.gz" ;;
  48. *) echo "unknown" ;;
  49. esac
  50. fi
  51. }
  52. function lib_x_cmd() {
  53. case $1 in
  54. "php"|"swoole"|"hash"|"inotify"|"redis"|"libxml2"|"curl")
  55. _x_cmd="tar"
  56. ;;
  57. "liblzma") if [ "$_use_backup" = "yes" ]; then _x_cmd="unzip"; else _x_cmd="tar"; fi ;;
  58. *) _x_cmd="unknown" ;;
  59. esac
  60. case $2 in
  61. "cmd")
  62. echo $_x_cmd
  63. ;;
  64. "file-prefix")
  65. case $_x_cmd in
  66. "tar") echo "-zxvf" ;;
  67. "unzip") echo "" ;;
  68. esac
  69. ;;
  70. "out-prefix")
  71. case $_x_cmd in
  72. "tar") echo "-C" ;;
  73. "unzip") echo "-d" ;;
  74. esac
  75. ;;
  76. esac
  77. }
  78. function lib_x() {
  79. $(lib_x_cmd $1 cmd) $(lib_x_cmd $1 file-prefix) "$_source_dir/$(lib_x_dirname $1 file)" $(lib_x_cmd $1 out-prefix) "$_source_dir/"
  80. }
  81. # 获取解压后的源码根目录
  82. function lib_x_dirname() {
  83. case $1 in
  84. "php"|"swoole"|"hash"|"inotify"|"redis"|"libxml2"|"curl")
  85. if [ "$2" = "file" ]; then _name_prefix=".tar.gz"; else _name_prefix=""; fi
  86. echo "$1-$(lib_ver $1)$_name_prefix"
  87. ;;
  88. "liblzma")
  89. if [ "$_use_backup" = "yes" ]; then
  90. if [ "$2" = "file" ]; then _name_prefix=".zip"; else _name_prefix=""; fi
  91. echo "$1-$(lib_ver $1)$_name_prefix"
  92. else
  93. if [ "$2" = "file" ]; then _name_prefix=".tar.gz"; else _name_prefix=""; fi
  94. echo "$1""$_name_prefix"
  95. fi
  96. ;;
  97. *)
  98. echo "unknown"
  99. ;;
  100. esac
  101. }
  102. download_file $(lib_download_link $1 $3) $(lib_x_firname $1 file)