#!/bin/bash

[ ! -z "$OPENSHIFT_CONTROLLER_LIB_GIT" ] && return 0
OPENSHIFT_CONTROLLER_LIB_GIT=true

source "/etc/openshift/node.conf"

function clone_git_repo {
    application="$1"
    user_id=$2
    group_id=$3
    uuid=$4
    desc="$5"
    APP_HOME="${GEAR_BASE_DIR}/${uuid}"

    if [ ! -d $APP_HOME ]; then echo "ERROR: Application ${uuid} not found!  Please create." 1>&2; exit 2; fi

    GIT_DIR=$APP_HOME/git/$application.git
    mkdir -p "$APP_HOME/git"
    git clone --bare --no-hardlinks $CART_INFO_DIR/data/git_template.git $GIT_DIR

    # Repacking is needed to handle a bug with running multiple
    # git clone operations under concurrency.  The cloned repos
    # can end up in a corrupt state which repack will correct
    pushd $GIT_DIR > /dev/null
    git repack
    popd > /dev/null

    setup_git_repo $application $user_id $group_id $uuid "$desc"
}

function clone_external_git_repo {
    application="$1"
    user_id=$2
    group_id=$3
    uuid=$4
    git_url="$5"
    desc="$6"
    APP_HOME="${GEAR_BASE_DIR}/${uuid}"

    if [ ! -d $APP_HOME ]; then echo "ERROR: Application ${uuid} not found!  Please create." 1>&2; exit 2; fi

    allowed=""
    methods=('git://' 'http://' 'https://' 'file://' 'ftp://' 'ftps://' 'rsync://')
    for method in "${methods[@]}"
    do
        if [[ $git_url =~ ^"$method" ]]
        then
            allowed=1
        fi
    done

    if [ -z "$allowed" ]
    then
        client_error "Source Code repository URL type must be one of: ${methods[*]}"
        exit 130
    fi

    echo "Cloning git repository from remote '${git_url}'."
    GIT_DIR=$APP_HOME/git/$application.git
    mkdir -p "$APP_HOME/git"

    (
        # Run in a subshell so that we can emit an error message even though hooks run with "set -e"
        git clone --bare --no-hardlinks $git_url $GIT_DIR

        if [ $? -ne 0 ]
        then
            client_error "Source Code repository could not be cloned: '${git_url}'.  Please verify the repository is correct and contact support."
            exit 131
        fi
    ) || exit 131

    # Repacking is needed to handle a bug with running multiple
    # git clone operations under concurrency.  The cloned repos
    # can end up in a corrupt state which repack will correct
    pushd $GIT_DIR > /dev/null
    git repack
    popd > /dev/null

    setup_git_repo $application $user_id $group_id $uuid "$desc"
}

function destroy_git_repo {
    application="$1"
    uuid="$2"
    APP_HOME="${GEAR_BASE_DIR}/${uuid}"
    
    if [ ! -d $APP_HOME ]; then echo "ERROR: Application ${uuid} not found!  Please create." 1>&2; exit 2; fi
    
    GIT_DIR=$APP_HOME/git/$application.git
    rm -rf $GIT_DIR || warning "Could not remove ${application}" 108
}

function setup_git_repo {
    application="$1"
    user_id=$2
    group_id=$3
    uuid=$4
    desc="$5"
    
    APP_HOME="${GEAR_BASE_DIR}/${uuid}"
    GIT_DIR=$APP_HOME/git/$application.git

    echo "$desc application '$application'" > $GIT_DIR/description

    cat <<EOF > $GIT_DIR/hooks/pre-receive
#!/bin/bash

# Import Environment Variables
for f in ~/.env/*
do
    . \$f
done

pre_receive_app.sh
EOF

    cat <<EOF > $GIT_DIR/hooks/post-receive
#!/bin/bash

# Import Environment Variables
for f in ~/.env/*
do
    . \$f
done

post_receive_app.sh ${BROKER_HOST}
EOF

    chmod 0755 $GIT_DIR/hooks/post-receive $GIT_DIR/hooks/pre-receive
    chown $user_id.$group_id -R $GIT_DIR
    # Secure the hooks dir
    chown root.root -R $GIT_DIR/hooks
    #chown root.root $GIT_DIR
    chmod 0755 $GIT_DIR
    cat <<EOF > $APP_HOME/.gitconfig
[user]
  name = OpenShift System User
[gc]
  auto = 100
EOF
}

function populate_repo_dir {
    CART_BIN_DIR="${CARTRIDGE_BASE_PATH}/abstract/info/bin"
    $CART_BIN_DIR/deploy_git_dir.sh ${GIT_DIR} "$APP_REPO_DIR"
}
