<commit_before>#!/bin/bash
# https://manual.seafile.com/deploy/using_sqlite.html
#
#
# Inspiration from following seafile dockers:
#   https://github.com/foxel/seafile-docker/blob/master/scripts/setup.sh

SEAFILE_PATH=''                       # will be defined later on
SEAFILE_URL="https://$SERVER_IP"      # note https (assuming you've enabled https)
VER_REGEX='^[0-9]+\.[0-9]+\.[0-9]+$'  # seafile version validation regex


# 1) downloads tarball (either the latest or specified ver, depending on $VER);
# 2) defines $SEAFILE_PATH pointing to active installation directory;
# 3) untars downloaded tarball into $SEAFILE_PATH.
download_seafile() {
    local downloaded_tarball url

    readonly downloaded_tarball='/tmp/seafile-server.tar.gz'

    if [[ "$VER" == latest ]]; then
        url="$(wget \
            --progress=dot:mega \
            --no-check-certificate \
            "https://www.seafile.com/en/download/" -O- \
            | grep -o 'https://bintray.com/artifact/download/seafile-org/seafile/seafile-server_.*x86-64.tar.gz' \
            | head -n1)" || exit 1

        readonly VER="$(grep -Po 'seafile-server_\K.*(?=_x8.*$)' <<< "$url")" || exit 1
        [[ "$VER" =~ $VER_REGEX ]] || fail "found latest ver was in unexpected format: [$VER]"
    else  # actual version number was specified:
        url="https://bintray.com/artifact/download/seafile-org/seafile/seafile-server_${VER}_x86-64.tar.gz"
    fi

    readonly SEAFILE_PATH="/seafile/seafile-server-$VER"  # define installation dir

    # sanity:
    if [[ -e "$SEAFILE_PATH" ]]; then
        fail "[$SEAFILE_PATH] already exists; assuming [$VER] is already installed. abort."
    fi

    wget \
        --progress=dot:mega \
        --no-check-certificate \
        -O "$downloaded_tarball" \
        "$url" || fail "wgetting tarball from [$url] failed"

    mkdir -p -- "$SEAFILE_PATH" || exit 1
    tar -xzf "$downloaded_tarball" --strip-components=1 -C "$SEAFILE_PATH" || exit 1
    rm -- "$downloaded_tarball" || exit 1
}


# Taken from https://github.com/haiwen/seafile-server-installer-cn/blob/master/seafile-server-ubuntu-14-04-amd64-http
setup_seafile() {
    local init_admin init_admin_bak

    readonly init_admin="${SEAFILE_PATH}/check_init_admin.py"
    readonly init_admin_bak="${init_admin}.bak"

    check_is_file "$init_admin"
    # Backup check_init_admin.py befor applying changes
    cp -- "$init_admin" "$init_admin_bak" || exit 1

    sed --follow-symlinks -i 's/= ask_admin_email()/= '"\"${SEAHUB_ADMIN_USER}\""'/' "$init_admin" || exit 1
    sed --follow-symlinks -i 's/= ask_admin_password()/= '"\"${SEAHUB_ADMIN_PW}\""'/' "$init_admin" || exit 1

    "${SEAFILE_PATH}/setup-seafile.sh" auto || fail "seafile setup failed."

    # Start and stop Seafile eco system. This generates the initial admin user.
    "${SEAFILE_PATH}/seafile.sh" start || exit 1
    "${SEAFILE_PATH}/seahub.sh" start || exit 1
    "${SEAFILE_PATH}/seahub.sh" stop || exit 1
    "${SEAFILE_PATH}/seafile.sh" stop || exit 1

    # Restore original check_init_admin.py
    mv -- "$init_admin_bak" "$init_admin" || exit 1
}


# https://manual.seafile.com/extension/webdav.html
setup_webdav() {
    local f

    readonly f='./conf/seafdav.conf'

    check_is_file "$f"
    crudini --merge "$f" <<'EOF'
[WEBDAV]
enabled = true
port = 8080
host = 127.0.0.1
fastcgi = true
share_name = /seafdav
EOF
}


# https://manual.seafile.com/deploy/deploy_with_nginx.html
setup_ccnet_for_nginx() {<commit_msg>change webdav host back to $SERVER_IP<commit_after>#!/bin/bash
# https://manual.seafile.com/deploy/using_sqlite.html
#
#
# Inspiration from following seafile dockers:
#   https://github.com/foxel/seafile-docker/blob/master/scripts/setup.sh

SEAFILE_PATH=''                       # will be defined later on
SEAFILE_URL="https://$SERVER_IP"      # note https (assuming you've enabled https)
VER_REGEX='^[0-9]+\.[0-9]+\.[0-9]+$'  # seafile version validation regex


# 1) downloads tarball (either the latest or specified ver, depending on $VER);
# 2) defines $SEAFILE_PATH pointing to active installation directory;
# 3) untars downloaded tarball into $SEAFILE_PATH.
download_seafile() {
    local downloaded_tarball url

    readonly downloaded_tarball='/tmp/seafile-server.tar.gz'

    if [[ "$VER" == latest ]]; then
        url="$(wget \
            --progress=dot:mega \
            --no-check-certificate \
            "https://www.seafile.com/en/download/" -O- \
            | grep -o 'https://bintray.com/artifact/download/seafile-org/seafile/seafile-server_.*x86-64.tar.gz' \
            | head -n1)" || exit 1

        readonly VER="$(grep -Po 'seafile-server_\K.*(?=_x8.*$)' <<< "$url")" || exit 1
        [[ "$VER" =~ $VER_REGEX ]] || fail "found latest ver was in unexpected format: [$VER]"
    else  # actual version number was specified:
        url="https://bintray.com/artifact/download/seafile-org/seafile/seafile-server_${VER}_x86-64.tar.gz"
    fi

    readonly SEAFILE_PATH="/seafile/seafile-server-$VER"  # define installation dir

    # sanity:
    if [[ -e "$SEAFILE_PATH" ]]; then
        fail "[$SEAFILE_PATH] already exists; assuming [$VER] is already installed. abort."
    fi

    wget \
        --progress=dot:mega \
        --no-check-certificate \
        -O "$downloaded_tarball" \
        "$url" || fail "wgetting tarball from [$url] failed"

    mkdir -p -- "$SEAFILE_PATH" || exit 1
    tar -xzf "$downloaded_tarball" --strip-components=1 -C "$SEAFILE_PATH" || exit 1
    rm -- "$downloaded_tarball" || exit 1
}


# Taken from https://github.com/haiwen/seafile-server-installer-cn/blob/master/seafile-server-ubuntu-14-04-amd64-http
setup_seafile() {
    local init_admin init_admin_bak

    readonly init_admin="${SEAFILE_PATH}/check_init_admin.py"
    readonly init_admin_bak="${init_admin}.bak"

    check_is_file "$init_admin"
    # Backup check_init_admin.py befor applying changes
    cp -- "$init_admin" "$init_admin_bak" || exit 1

    sed --follow-symlinks -i 's/= ask_admin_email()/= '"\"${SEAHUB_ADMIN_USER}\""'/' "$init_admin" || exit 1
    sed --follow-symlinks -i 's/= ask_admin_password()/= '"\"${SEAHUB_ADMIN_PW}\""'/' "$init_admin" || exit 1

    "${SEAFILE_PATH}/setup-seafile.sh" auto || fail "seafile setup failed."

    # Start and stop Seafile eco system. This generates the initial admin user.
    "${SEAFILE_PATH}/seafile.sh" start || exit 1
    "${SEAFILE_PATH}/seahub.sh" start || exit 1
    "${SEAFILE_PATH}/seahub.sh" stop || exit 1
    "${SEAFILE_PATH}/seafile.sh" stop || exit 1

    # Restore original check_init_admin.py
    mv -- "$init_admin_bak" "$init_admin" || exit 1
}


# https://manual.seafile.com/extension/webdav.html
setup_webdav() {
    local f

    readonly f='./conf/seafdav.conf'

    check_is_file "$f"
    crudini --merge "$f" <<'EOF'
[WEBDAV]
enabled = true
port = 8080
host = $SERVER_IP
fastcgi = true
share_name = /seafdav
EOF
}


# https://manual.seafile.com/deploy/deploy_with_nginx.html
setup_ccnet_for_nginx() {
{'fraction_non_alphanumeric': 0.156415770609319, 'fraction_numerical': 0.013189964157706093, 'mean_word_length': 3.101116990005879, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 3, 'https://': 18, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '868137', 'n_tokens_mistral': 2571, 'n_tokens_neox': 2424, 'n_words': 523}