Skip to content

Commit

Permalink
enh: refined install script
Browse files Browse the repository at this point in the history
  • Loading branch information
tphakala committed Dec 6, 2024
1 parent 7e91898 commit 6ad11ad
Showing 1 changed file with 146 additions and 127 deletions.
273 changes: 146 additions & 127 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ NC='\033[0m' # No Color

# Function to print colored messages
print_message() {
echo -e "${2}${1}${NC}"
if [ "$3" = "nonewline" ]; then
echo -en "${2}${1}${NC}"
else
echo -e "${2}${1}${NC}"
fi
}

# Function to check if a command exists
Expand Down Expand Up @@ -103,98 +107,13 @@ test_rtsp_url() {
return 1
}

# Function to configure locale
configure_locale() {
print_message "\nLocale Configuration" "$GREEN"
print_message "Available languages:" "$YELLOW"

# Create arrays for locales
declare -a locale_codes=("af" "ca" "cs" "zh" "hr" "da" "nl" "en" "et" "fi" "fr" "de" "el" "hu" "is" "id" "it" "ja" "lv" "lt" "no" "pl" "pt" "ru" "sk" "sl" "es" "sv" "th" "uk")
declare -a locale_names=("Afrikaans" "Catalan" "Czech" "Chinese" "Croatian" "Danish" "Dutch" "English" "Estonian" "Finnish" "French" "German" "Greek" "Hungarian" "Icelandic" "Indonesia" "Italian" "Japanese" "Latvian" "Lithuania" "Norwegian" "Polish" "Portuguese" "Russian" "Slovak" "Slovenian" "Spanish" "Swedish" "Thai" "Ukrainian")

# Display available locales
for i in "${!locale_codes[@]}"; do
printf "%2d) %-12s" "$((i+1))" "${locale_names[i]}"
if [ $((i % 3)) -eq 2 ]; then
echo
fi
done
echo

while true; do
print_message "\nSelect your language (1-${#locale_codes[@]}):" "$YELLOW"
read -r selection

if [[ "$selection" =~ ^[0-9]+$ ]] && [ "$selection" -ge 1 ] && [ "$selection" -le "${#locale_codes[@]}" ]; then
LOCALE_CODE="${locale_codes[$((selection-1))]}"
print_message "Selected language: ${locale_names[$((selection-1))]}" "$GREEN"
# Update config file
sed -i "s/locale: en/locale: ${LOCALE_CODE}/" "$CONFIG_FILE"
break
else
print_message "Invalid selection. Please try again." "$RED"
fi
done
}

# Function to configure location
configure_location() {
print_message "\nLocation Configuration" "$GREEN"
print_message "1) Enter coordinates manually" "$YELLOW"
print_message "2) Enter city name" "$YELLOW"
read -p "Select location input method (1/2): " location_choice

case $location_choice in
1)
while true; do
read -p "Enter latitude (-90 to 90): " lat
read -p "Enter longitude (-180 to 180): " lon

if [[ "$lat" =~ ^-?[0-9]*\.?[0-9]+$ ]] && \
[[ "$lon" =~ ^-?[0-9]*\.?[0-9]+$ ]] && \
(( $(echo "$lat >= -90 && $lat <= 90" | bc -l) )) && \
(( $(echo "$lon >= -180 && $lon <= 180" | bc -l) )); then
break
else
print_message "Invalid coordinates. Please try again." "$RED"
fi
done
;;
2)
while true; do
read -p "Enter city name: " city
read -p "Enter country code (e.g., US, FI): " country

# Use OpenStreetMap Nominatim API to get coordinates
coordinates=$(curl -s "https://nominatim.openstreetmap.org/search?city=${city}&country=${country}&format=json" | jq -r '.[0] | "\(.lat) \(.lon)"')

if [ -n "$coordinates" ] && [ "$coordinates" != "null null" ]; then
lat=$(echo "$coordinates" | cut -d' ' -f1)
lon=$(echo "$coordinates" | cut -d' ' -f2)
print_message "Found coordinates: $lat, $lon" "$GREEN"
break
else
print_message "Could not find coordinates for the specified city. Please try again." "$RED"
fi
done
;;
*)
print_message "Invalid choice. Exiting." "$RED"
exit 1
;;
esac

# Update config file
sed -i "s/latitude: 00.000/latitude: $lat/" "$CONFIG_FILE"
sed -i "s/longitude: 00.000/longitude: $lon/" "$CONFIG_FILE"
}

# Function to configure audio input
configure_audio_input() {
print_message "\nAudio Input Configuration" "$GREEN"
print_message "1) Use sound card" "$YELLOW"
print_message "2) Use RTSP stream" "$YELLOW"
read -p "Select audio input method (1/2): " audio_choice
print_message "1) Use sound card"
print_message "2) Use RTSP stream"
print_message "Select audio input method (1/2): " "$YELLOW" "nonewline"
read -r audio_choice

case $audio_choice in
1)
Expand Down Expand Up @@ -243,7 +162,7 @@ configure_sound_card() {
fi

while true; do
print_message "\nPlease select a device number from the list above (1-${#devices[@]}):" "$YELLOW"
print_message "\nPlease select a device number from the list above (1-${#devices[@]}): " "$YELLOW" "nonewline"
read -r selection

if [[ "$selection" =~ ^[0-9]+$ ]] && [ "$selection" -ge 1 ] && [ "$selection" -le "${#devices[@]}" ]; then
Expand All @@ -266,7 +185,7 @@ configure_sound_card() {
# Function to configure RTSP stream
configure_rtsp_stream() {
while true; do
print_message "\nEnter RTSP URL (format: rtsp://user:password@address/path):" "$YELLOW"
print_message "\nEnter RTSP URL (format: rtsp://user:password@address/path): " "$YELLOW" "nonewline"
read -r RTSP_URL

if [[ ! $RTSP_URL =~ ^rtsp:// ]]; then
Expand Down Expand Up @@ -297,15 +216,16 @@ configure_rtsp_stream() {
# Function to configure audio export format
configure_audio_format() {
print_message "\nAudio Export Configuration" "$GREEN"
print_message "Select audio format for captured sounds:" "$YELLOW"
print_message "1) WAV (Uncompressed, largest files)" "$YELLOW"
print_message "2) FLAC (Lossless compression)" "$YELLOW"
print_message "3) AAC (High quality, smaller files)" "$YELLOW"
print_message "4) MP3 (Most compatible)" "$YELLOW"
print_message "5) Opus (Best compression)" "$YELLOW"
print_message "Select audio format for captured sounds:"
print_message "1) WAV (Uncompressed, largest files)"
print_message "2) FLAC (Lossless compression)"
print_message "3) AAC (High quality, smaller files)"
print_message "4) MP3 (Most compatible)"
print_message "5) Opus (Best compression)"

while true; do
read -p "Select format (1-5): " format_choice
print_message "Select format (1-5): " "$YELLOW" "nonewline"
read -r format_choice
case $format_choice in
1) format="wav"; break;;
2) format="flac"; break;;
Expand All @@ -320,6 +240,93 @@ configure_audio_format() {
sed -i "s/type: wav/type: $format/" "$CONFIG_FILE"
}

# Function to configure locale
configure_locale() {
print_message "\nLocale Configuration for bird species names" "$GREEN"
print_message "Available languages:" "$YELLOW"

# Create arrays for locales
declare -a locale_codes=("af" "ca" "cs" "zh" "hr" "da" "nl" "en" "et" "fi" "fr" "de" "el" "hu" "is" "id" "it" "ja" "lv" "lt" "no" "pl" "pt" "ru" "sk" "sl" "es" "sv" "th" "uk")
declare -a locale_names=("Afrikaans" "Catalan" "Czech" "Chinese" "Croatian" "Danish" "Dutch" "English" "Estonian" "Finnish" "French" "German" "Greek" "Hungarian" "Icelandic" "Indonesia" "Italian" "Japanese" "Latvian" "Lithuania" "Norwegian" "Polish" "Portuguese" "Russian" "Slovak" "Slovenian" "Spanish" "Swedish" "Thai" "Ukrainian")

# Display available locales
for i in "${!locale_codes[@]}"; do
printf "%2d) %-12s" "$((i+1))" "${locale_names[i]}"
if [ $((i % 3)) -eq 2 ]; then
echo
fi
done
echo

while true; do
print_message "Select your language (1-${#locale_codes[@]}): " "$YELLOW" "nonewline"
read -r selection

if [[ "$selection" =~ ^[0-9]+$ ]] && [ "$selection" -ge 1 ] && [ "$selection" -le "${#locale_codes[@]}" ]; then
LOCALE_CODE="${locale_codes[$((selection-1))]}"
print_message "Selected language: ${locale_names[$((selection-1))]}" "$GREEN"
# Update config file
sed -i "s/locale: en/locale: ${LOCALE_CODE}/" "$CONFIG_FILE"
break
else
print_message "Invalid selection. Please try again." "$RED"
fi
done
}

# Function to configure location
configure_location() {
print_message "\nLocation Configuration" "$GREEN"
print_message "1) Enter coordinates manually" "$YELLOW"
print_message "2) Enter city name" "$YELLOW"
read -p "Select location input method (1/2): " location_choice

case $location_choice in
1)
while true; do
read -p "Enter latitude (-90 to 90): " lat
read -p "Enter longitude (-180 to 180): " lon

if [[ "$lat" =~ ^-?[0-9]*\.?[0-9]+$ ]] && \
[[ "$lon" =~ ^-?[0-9]*\.?[0-9]+$ ]] && \
(( $(echo "$lat >= -90 && $lat <= 90" | bc -l) )) && \
(( $(echo "$lon >= -180 && $lon <= 180" | bc -l) )); then
break
else
print_message "Invalid coordinates. Please try again." "$RED"
fi
done
;;
2)
while true; do
read -p "Enter city name: " city
read -p "Enter country code (e.g., US, FI): " country

# Use OpenStreetMap Nominatim API to get coordinates
coordinates=$(curl -s "https://nominatim.openstreetmap.org/search?city=${city}&country=${country}&format=json" | jq -r '.[0] | "\(.lat) \(.lon)"')

if [ -n "$coordinates" ] && [ "$coordinates" != "null null" ]; then
lat=$(echo "$coordinates" | cut -d' ' -f1)
lon=$(echo "$coordinates" | cut -d' ' -f2)
print_message "Found coordinates: $lat, $lon" "$GREEN"
break
else
print_message "Could not find coordinates for the specified city. Please try again." "$RED"
fi
done
;;
*)
print_message "Invalid choice. Exiting." "$RED"
exit 1
;;
esac

# Update config file
sed -i "s/latitude: 00.000/latitude: $lat/" "$CONFIG_FILE"
sed -i "s/longitude: 00.000/longitude: $lon/" "$CONFIG_FILE"
}


# Function to configure basic authentication
configure_auth() {
print_message "\nSecurity Configuration" "$GREEN"
Expand Down Expand Up @@ -351,6 +358,35 @@ configure_auth() {
fi
}

# Function to add systemd service configuration
add_systemd_config() {
# Create systemd service
print_message "\nCreating systemd service..." "$GREEN"
sudo tee /etc/systemd/system/birdnet-go.service << EOF
[Unit]
Description=BirdNET-Go
After=docker.service
Requires=docker.service
[Service]
Restart=always
ExecStart=/usr/bin/docker run --rm \\
-p 8080:8080 \\
--env TZ="${TZ}" \\
${AUDIO_ENV} \\
-v ${CONFIG_DIR}:/config \\
-v ${DATA_DIR}:/data \\
${BIRDNET_GO_IMAGE}
[Install]
WantedBy=multi-user.target
EOF

# Reload systemd and enable service
sudo systemctl daemon-reload
sudo systemctl enable birdnet-go.service
}

# ASCII Art Banner
cat << "EOF"
____ _ _ _ _ _____ _____ ____
Expand Down Expand Up @@ -398,11 +434,17 @@ check_install_package "apache2-utils"
# Check and install Docker
if ! command_exists docker; then
print_message "Docker not found. Installing Docker..." "$YELLOW"
# Install Docker using convenience script
curl -fsSL https://get.docker.com | sh
# Install Docker from apt repository
sudo apt-get install -y docker.io
# Add current user to docker group
sudo usermod -aG docker "$USER"
print_message "Docker installed successfully. You may need to log out and back in for group changes to take effect." "$GREEN"
# Start and enable Docker service
sudo systemctl start docker
sudo systemctl enable docker
print_message "Docker installed successfully. To make group member changes take effect, please log out and log back in and run install.sh again." "$GREEN"
print_message "Exiting install script..." "$YELLOW"
# exit install script
exit 0
fi

# Check if directories can be created
Expand Down Expand Up @@ -443,31 +485,8 @@ fi
# Pause for 5 seconds
sleep 5

# Create systemd service
print_message "\nCreating systemd service..." "$YELLOW"
sudo tee /etc/systemd/system/birdnet-go.service << EOF
[Unit]
Description=BirdNET-Go
After=docker.service
Requires=docker.service
[Service]
Restart=always
ExecStart=/usr/bin/docker run --rm \\
-p 8080:8080 \\
--env TZ="${TZ}" \\
${AUDIO_ENV} \\
-v ${CONFIG_DIR}:/config \\
-v ${DATA_DIR}:/data \\
${BIRDNET_GO_IMAGE}
[Install]
WantedBy=multi-user.target
EOF

# Reload systemd and enable service
sudo systemctl daemon-reload
sudo systemctl enable birdnet-go.service
# Add systemd service configuration
add_systemd_config

print_message "\nInstallation completed!" "$GREEN"
print_message "Configuration directory: $CONFIG_DIR" "$GREEN"
Expand Down

0 comments on commit 6ad11ad

Please sign in to comment.