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.
 
 
 
 
 
 

108 lines
2.4 KiB

  1. #!/bin/bash
  2. set -e
  3. # Default configurations
  4. username=lemmy
  5. dbname=lemmy
  6. port=5432
  7. yes_no_prompt_invalid() {
  8. echo "Invalid input. Please enter either \"y\" or \"n\"." 1>&2
  9. }
  10. print_config() {
  11. echo " database name: $dbname"
  12. echo " username: $username"
  13. echo " port: $port"
  14. }
  15. ask_for_db_config() {
  16. echo "The default database configuration is:"
  17. print_config
  18. echo
  19. default_config_final=0
  20. default_config_valid=0
  21. while [ "$default_config_valid" == 0 ]
  22. do
  23. read -p "Use this configuration (y/n)? " default_config
  24. case "$default_config" in
  25. [yY]* ) default_config_valid=1; default_config_final=1;;
  26. [nN]* ) default_config_valid=1; default_config_final=0;;
  27. * ) yes_no_prompt_invalid;;
  28. esac
  29. echo
  30. done
  31. if [ "$default_config_final" == 0 ]
  32. then
  33. config_ok_final=0
  34. while [ "$config_ok_final" == 0 ]
  35. do
  36. read -p "Database name: " dbname
  37. read -p "Username: " username
  38. read -p "Port: " port
  39. #echo
  40. #echo "The database configuration is:"
  41. #print_config
  42. #echo
  43. config_ok_valid=0
  44. while [ "$config_ok_valid" == 0 ]
  45. do
  46. read -p "Use this configuration (y/n)? " config_ok
  47. case "$config_ok" in
  48. [yY]* ) config_ok_valid=1; config_ok_final=1;;
  49. [nN]* ) config_ok_valid=1; config_ok_final=0;;
  50. * ) yes_no_prompt_invalid;;
  51. esac
  52. echo
  53. done
  54. done
  55. fi
  56. }
  57. ask_for_password() {
  58. password=""
  59. password_confirm=""
  60. password_valid=0
  61. while [ "$password_valid" == 0 ]
  62. do
  63. read -p "Enter database password: " -s password
  64. echo
  65. read -p "Verify database password: " -s password_confirm
  66. echo
  67. echo
  68. # Start the loop from the top if either check fails
  69. if [ -z "$password" ]
  70. then
  71. echo "Error: Password cannot be empty." 1>&2
  72. echo
  73. continue
  74. fi
  75. if [ "$password" != "$password_confirm" ]
  76. then
  77. echo "Error: Passwords don't match." 1>&2
  78. echo
  79. continue
  80. fi
  81. # Set the password_valid variable to break out of the loop
  82. password_valid=1
  83. done
  84. }
  85. ask_for_db_config
  86. ask_for_password
  87. psql -c "CREATE USER $username WITH PASSWORD '$password' SUPERUSER;" -U postgres
  88. psql -c "CREATE DATABASE $dbname WITH OWNER $username;" -U postgres
  89. export LEMMY_DATABASE_URL=postgres://$username:$password@localhost:$port/$dbname
  90. echo "The database URL is $LEMMY_DATABASE_URL"