You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
3.2 KiB

  1. #!/bin/bash
  2. if snapctl get gitea.snap.custom; then
  3. cdir=$(snapctl get gitea.snap.custom)
  4. else
  5. cdir=$SNAP_COMMON
  6. fi
  7. cfg="$cdir/conf/app.ini"
  8. bak="$cdir/conf/app.ini.bak-$(date -Ins)"
  9. basecfg="$SNAP/snap/helpers/app.ini"
  10. smp="$SNAP/gitea/custom/conf/app.ini.sample"
  11. function toSnap() {
  12. OIFS=$IFS
  13. IFS='
  14. '
  15. category="none"
  16. src="$cfg"
  17. [[ "$1" = "init" ]] && src="$smp"
  18. [[ "$1" = "snap" ]] && src="$basecfg"
  19. for l in $(sed 's_;\([A-Z]*\)_\1_g' $src | grep -v -e '^;' -e '^$'); do
  20. if echo $l | grep -q '^[[]'; then
  21. category=$(CatToSnap "$l")
  22. elif echo $l | grep -q '^[A-Z]'; then
  23. option=$(OptToSnap "$l")
  24. value=$(ValToSnap "$l")
  25. if [[ $category = "none" ]]; then
  26. snapctl set "$option=$value"
  27. else
  28. snapctl set "$category.$option=$value"
  29. fi
  30. fi
  31. done
  32. IFS=$OIFS
  33. }
  34. function toIni() {
  35. OIFS=$IFS
  36. IFS='
  37. '
  38. category="none"; option="none"; catUnset=true
  39. src=$smp
  40. [[ -f $cfg ]] && src="$cfg"
  41. tmpIni="$cfg.tmp"
  42. [[ -f $src ]] && cp "$src" "$tmpIni"
  43. cp $tmpIni $bak
  44. echo '' > $cfg
  45. for l in $(grep -v -e '^;' -e '^$' $tmpIni); do
  46. if echo $l | grep -q '^[[]'; then
  47. category=$(CatToSnap "$l")
  48. catUnset=true
  49. elif echo $l | grep -q '^[A-Z]'; then
  50. option=$(OptToSnap "$l")
  51. if [[ $category = "none" ]]; then
  52. value=$(snapctl get $option)
  53. echo $(OptToIni "$option") = $value >> $cfg
  54. else
  55. value=$(snapctl get $category.$option)
  56. if $catUnset; then
  57. echo "" >> $cfg
  58. echo "[$(CatToIni "$category")]" >> $cfg
  59. catUnset=false
  60. fi
  61. echo $(OptToIni "$option") = $value >> $cfg
  62. fi
  63. fi
  64. done;
  65. IFS=$OIFS
  66. }
  67. function CatToSnap {
  68. ret=$(echo "$1" \
  69. | grep -oP '[A-Za-z0-9._]+' \
  70. | sed 's|\.|-|g' \
  71. | sed 's|_|99|g')
  72. echo $ret
  73. }
  74. function OptToSnap {
  75. ret=$(echo "$1" \
  76. | grep -oP '^[A-Z_]+' \
  77. | tr '[:upper:]' '[:lower:]' \
  78. | sed 's|_|-|g')
  79. echo $ret
  80. }
  81. function ValToSnap {
  82. ret=$(echo "$1" \
  83. | grep -oP '=.*$' \
  84. | sed 's_^= __g' \
  85. | sed 's_^=__g' \
  86. | sed "s|SNAP_DIR_DATA|$SDATA|g" \
  87. | sed "s|SNAP_DIR_COMMON|$SCOMMON|g" \
  88. | sed 's|{}||g')
  89. echo $ret
  90. }
  91. function CatToIni {
  92. ret=$(echo "$1" \
  93. | sed 's|-|.|g' \
  94. | sed 's|\ |_|g' \
  95. | sed 's|99|_|g')
  96. echo $ret
  97. }
  98. function OptToIni {
  99. ret=$(echo "$1" \
  100. | tr '[:lower:]' '[:upper:]' \
  101. | sed 's|-|_|g')
  102. echo $ret
  103. }
  104. [[ "$1" = "configure" ]] \
  105. && toIni \
  106. && exit 0
  107. [[ "$1" = "install" ]] \
  108. && echo "Initial Configuration..." \
  109. && mkdir -p $SNAP_COMMON/conf \
  110. && toSnap init \
  111. && toSnap snap \
  112. && toIni sample \
  113. && exit 0
  114. [[ "$1" = "save" ]] \
  115. && echo "Saving current config..." \
  116. && toSnap \
  117. && exit 0