#
#  Copyright (c) 2006-2011 Commvault systems, Inc. <http://www.commvault.com>.
#  This file is part of OpenArchive.

#  This file is licensed to you under your choice of the GNU Lesser
#  General Public License, version 1 or any later version (LGPLv3 or
#  later), or the GNU General Public License, version 2 (GPLv2), in all
#  cases as published by the Free Software Foundation.
#

volume=""

excmd()
{
    command="$*"
    ${command}
    ret=$?
    if [ $ret != 0 ]; then
        echo "Execution of ${command} failed with return code:$ret"
        exit 1
    fi
}

usage()
{
    echo "$binary --volume <volume name>"
}

parse_args() 
{
    while [ "$1" != "" ]; do
        case $1 in
            --volume | -v)    shift
                              volume=$1
                              ;;

            --help | -h)      usage
                              exit
                              ;;
        esac
        shift
    done
}

validate_params()
{
    if [ -z "$volume" ]; then
        usage
        exit 1  
    fi
}

create_gfapi_vol_file()
{
    existing_vol_file="/var/lib/glusterd/vols/${volume}/trusted-${volume}.tcp-fuse.vol"
    gfapi_vol_file="/var/lib/glusterd/vols/${volume}/trusted-${volume}.gfapi.vol";

    #Backup the existing vol file
    if [ -f $existing_vol_file ]; then
        excmd "cp -f $existing_vol_file $gfapi_vol_file"
    else
        echo "Vol file $existing_vol_file missing"
        exit 1
    fi

}

enable_cloudsync()
{
    #Disable some of the performance xlators which could interrupt with cludsync functionality
    excmd "gluster volume set ${volume} performance.quick-read off"
    excmd "gluster volume set ${volume} performance.open-behind off"
    excmd "gluster volume set ${volume} performance.readdir-ahead off"
    excmd "gluster volume set ${volume} performance.read-ahead off"
    excmd "gluster volume set ${volume} performance.stat-prefetch off"
    excmd "gluster volume set ${volume} feature.cloudsync-remote-read on"
    excmd "gluster volume set ${volume} feature.cloudsync-storetype cvlt"
}

parse_args "$@"
validate_params
create_gfapi_vol_file
enable_cloudsync
