<commit_before>#!/bin/bash

# Start this: bash <(curl https://raw2.github.com/bukodi/devops/master/installDevServer.sh) [AdminPassw0rd]

SCRIPT_BASE_URL=https://raw2.github.com/bukodi/devops/master
EXTERNAL_HOST_NAME=$(hostname -f)
START_TIME=$(date)


if [ `whoami` != root ]; then
    echo 'Please run this script as root'
    exit
fi

if [ $# -eq 0 ]; then
    echo 'Enter a new password for the admin user!'
    read -s -p "Password:" psw1
    echo ''
    read -s -p "Verify:" psw2
    echo ''
    if [ $psw1 != $psw2 ]; then
        echo 'Password and verification are not equal!'
        exit
    fi
    ADMIN_PASSWORD=$<PASSWORD>
elif [ $# -eq 1 ]; then
    echo 'WARNING: the bash history contains the password provided as argument!'
    ADMIN_PASSWORD=$1
else
    echo 'Wrong number of arguments!'
    exit
fi

#TODO test ping EXTERNAL_HOST_NAME (if not then add to /etc/host)

#Execute apt-get-all script:
bash <(curl $SCRIPT_BASE_URL/scripts/apt-get-all.sh)

###############################################################################################

function setupApache {
    echo $'\n\n*** Configure Apache  ****'
    cd /etc/apache2
    #Load modules
    a2enmod ssl
    a2enmod proxy
    a2enmod proxy_http
    a2enmod rewrite
    
    #Enable SSL
    ln -s /etc/apache2/sites-available/default-ssl.conf /etc/apache2/sites-enabled/default-ssl.conf
    sed -i 's/DocumentRoot \/var\/www/DocumentRoot \/var\/www\n\t\tInclude conf-available\/reverse-proxies.conf/' sites-enabled/default-ssl.conf
    
    #Redirect http to https
    sed -i 's/DocumentRoot \/var\/www/DocumentRoot \/var\/www\n\n\tRewriteEngine On\n\tRewriteRule \^(\.\*)\$ https:\/\/%{HTTP_HOST}\$1 \[R=301,L\]\n/' sites-enabled/000-default.conf
    
    #Setup proxies
    echo "# Proxies" >> conf-available/reverse-proxies.conf
    cd - > /dev/null
    
    #Create index.html
    echo '<html>' >> index.html
    echo '<head><title>Development server</title></head>' >> index.html
    echo '<body>' >> index.html
    echo '</body>' >> index.html
    echo '</html>' >> index.html
    mv index.html /var/www/index.html
    
    #Restart apache
    service apache2 restart
}

function addApacheProxy {
    path=$1
    url=$2
    label=$3

    # Add a proxy to public https website
    echo "ProxyPass $path $url" >> /etc/apache2/conf-available/reverse-proxies.conf
    echo "ProxyPassReverse $path $url" >> /etc/apache2/conf-available/reverse-proxies.conf

    sed -i "s/<\\/body>/ <p><a href\=\"${path/\//\\/}\">${label/\//\\/}<\\/a><\\/p>\\n<\\/body>/" /var/www/index.html
}

function createAdminUser {
    echo $'\n\n*** Create admin user****'
    if [ -z $(getent group admin) ]; then <commit_msg>Update installDevServer.sh<commit_after>#!/bin/bash

# Start this: bash <(curl https://raw2.github.com/bukodi/devops/master/installDevServer.sh) [AdminPassw0rd]

SCRIPT_BASE_URL=https://raw2.github.com/bukodi/devops/master
EXTERNAL_HOST_NAME=$(hostname -f)
START_TIME=$(date)


if [ `whoami` != root ]; then
    echo 'Please run this script as root'
    exit
fi

if [ $# -eq 0 ]; then
    echo 'Enter a new password for the admin user!'
    read -s -p "Password:" psw1
    echo ''
    read -s -p "Verify:" psw2
    echo ''
    if [ $psw1 != $psw2 ]; then
        echo 'Password and verification are not equal!'
        exit
    fi
    ADMIN_PASSWORD=$<PASSWORD>
elif [ $# -eq 1 ]; then
    echo 'WARNING: the bash history contains the password provided as argument!'
    ADMIN_PASSWORD=$1
else
    echo 'Wrong number of arguments!'
    exit
fi

#TODO test ping EXTERNAL_HOST_NAME (if not then add to /etc/host)

#Execute apt-get-all script:
bash <(curl $SCRIPT_BASE_URL/scripts/apt-get-all.sh)

###############################################################################################

function setupApache {
    echo $'\n\n*** Configure Apache  ****'
    cd /etc/apache2
    #Load modules
    a2enmod ssl
    a2enmod proxy
    a2enmod proxy_http
    a2enmod rewrite
    
    #Enable SSL
    ln -s /etc/apache2/sites-available/default-ssl.conf /etc/apache2/sites-enabled/default-ssl.conf
    sed -i 's/DocumentRoot \/var\/www/DocumentRoot \/var\/www\n\t\tInclude conf-available\/reverse-proxies.conf/' sites-enabled/default-ssl.conf
    
    #Redirect http to https
    sed -i 's/DocumentRoot \/var\/www/DocumentRoot \/var\/www\n\n\tRewriteEngine On\n\tRewriteRule \^(\.\*)\$ https:\/\/%{HTTP_HOST}\$1 \[R=301,L\]\n/' sites-enabled/000-default.conf
    
    #Setup proxies
    echo "# Proxies" >> conf-available/reverse-proxies.conf
    cd - > /dev/null
    
    #Create index.html
    echo '<html>' >> index.html
    echo '<head><title>Development server</title></head>' >> index.html
    echo '<body>' >> index.html
    echo '</body>' >> index.html
    echo '</html>' >> index.html
    mv index.html /var/www/index.html
    
    #Restart apache
    service apache2 restart
}

function addApacheProxy {
    path=$1
    url=$2
    label=$3

    # Add a proxy to public https website
    echo "ProxyPass $path $url" >> /etc/apache2/conf-available/reverse-proxies.conf
    echo "ProxyPassReverse $path $url" >> /etc/apache2/conf-available/reverse-proxies.conf

    sed -i "s/<\\/body>/ <p><a href\=\"${path//\//\\/}\">${label//\//\\/}<\\/a><\\/p>\\n<\\/body>/" /var/www/index.html
}

function createAdminUser {
    echo $'\n\n*** Create admin user****'
    if [ -z $(getent group admin) ]; then 
{'fraction_non_alphanumeric': 0.19004272710384543, 'fraction_numerical': 0.01114620100315809, 'mean_word_length': 2.8873646209386283, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 67, 'https://': 4, 'lorem ipsum': 0, 'www.': 12, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2598399', 'n_tokens_mistral': 1919, 'n_tokens_neox': 1838, 'n_words': 443}