setup_gogs()
{
    # Generate salt
    salt=$(openssl rand -base64 10 | fold -w 10 | head -n 1)

    # Encode password
    enc_password=$(/opt/gogs/scripts/encode-password.py $salt $panelpassword)

    # Configure Gogs
    mkdir -p /opt/gogs/custom/conf/
    cat > /opt/gogs/custom/conf/app.ini <<EOT
APP_NAME = Gogs: Go Git Service
RUN_USER = gogs
RUN_MODE = prod

[repository]
ROOT = /var/gogs/repositories
SCRIPT_TYPE = bash
FORCE_PRIVATE = false
MAX_CREATION_LIMIT = -1

[server]
PROTOCOL = http
DOMAIN = $(ifconfig  | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}')
HTTP_ADDR =
HTTP_PORT = 3000

[database]
DB_TYPE = mysql
NAME = gogs
USER = gogs
PASSWD = $panelpassword

[security]
INSTALL_LOCK = True
EOT

    for i in $(seq 1 20); do
            mysql -u root -e "exit"
            if [[ $? != 0 ]]; then
                    sleep 5
            else
                    break
            fi
    done
    # Create database in MySQL
    mysql -u root -e "GRANT ALL PRIVILEGES ON gogs.* TO 'gogs'@'localhost' IDENTIFIED BY '$panelpassword';"
    mysql -u root -e "FLUSH PRIVILEGES;"
    mysql -u root -e "UPDATE mysql.user SET authentication_string=PASSWORD('$panelpassword') WHERE User='root';FLUSH PRIVILEGES;"

    # Let Gogs create necessary tables
    systemctl stop gogs
    systemctl start gogs

    # Wait for creation of table "gogs.user"
    for i in $(seq 1 10); do
        mysql -e "describe gogs.user" &> /dev/null
        if [[ $? != 0 ]]; then
            sleep 5
        else
            break
        fi
    done

    # Add admin user to database when "gogs.user" table is created by Gogs
    unix_time=$(date +%s)

    mysql -e "INSERT INTO gogs.user ( lower_name, name, full_name, email, passwd, login_type,
     login_source, login_name, type, location, website, rands, salt, created_unix,
     updated_unix, last_repo_visibility, max_repo_creation, is_active, is_admin,
     allow_git_hook, allow_import_local, avatar, avatar_email, use_custom_avatar,
     num_followers, num_following, num_stars, num_repos, description, num_teams,
     num_members)
     VALUES ('root', 'root', '', 'root@example.com', '$enc_password', 0, 0,
     '', '0', '', '', '0', '$salt', '$unix_time', '$unix_time', '0',
     '-1', '1', '1', '1', '1', '0', 'root@example.com',
     '0', '0', '0', '0', '0', '', '0', '0');"

     systemctl stop gogs

}

# Change password for debian-sys-maint
change_dsm_pwd() {
    dsm_usr="$(awk '/user/{print $NF; exit;}' /etc/mysql/debian.cnf)"
    old_dsm_usr_pwd="$(awk '/password/{print $NF; exit;}' /etc/mysql/debian.cnf)"
    rnd_dsm_pwd="$(< /dev/urandom tr -dc A-Za-z0-9 | head -c 32;echo)"
    mysql --user=${dsm_usr} --password=${old_dsm_usr_pwd} --execute="GRANT ALL PRIVILEGES ON *.* TO '${dsm_usr}'@'localhost' IDENTIFIED BY '${rnd_dsm_pwd}';"

    cat > /etc/mysql/debian.cnf <<EOF
# Automatically generated for Debian scripts. DO NOT TOUCH!
[client]
host     = localhost
user     = ${dsm_usr}
password = ${rnd_dsm_pwd}
socket   = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host     = localhost
user     = ${dsm_usr}
password = ${rnd_dsm_pwd}
socket   = /var/run/mysqld/mysqld.sock
EOF

    systemctl restart mysql.service

    systemctl start gogs
}

app_setup()
{
  setup_gogs
    if [ $? != 0 ]; then
        error "ERROR: error during setup GOGS..."
        exit 3
    fi
  
  change_dsm_pwd
    if [ $? != 0 ]; then
        error "ERROR: change debian-system-maint failed..."
        exit 3
    fi
}
