#!/bin/sh

echo imapsync_docker_timestamp_dockerfile
imapsync_docker_timestamp_dockerfile() {
	DATE_CURRENT=`date +%Y_%m_%d_%H_%M_%S`
	echo $DATE_CURRENT
	sed -i -e "/just_a_comment_to_force_update/s/comment_to_force_update.*/comment_to_force_update $DATE_CURRENT/" Dockerfile
	ci -l -f -m"Changing timestamp to $DATE_CURRENT with imapsync_docker_timestamp_dockerfile" Dockerfile
}

echo imapsync_docker_build
imapsync_docker_build() {
    docker build -t gilleslamiral/imapsync . \
        && imapsync_docker_add_tag_version && echo Build SUCCEEDED || echo Build FAILED

    echo
    #docker run gilleslamiral/imapsync imapsync --testslive
    #docker run gilleslamiral/imapsync imapsync --testslive6
    # docker run gilleslamiral/imapsync imapsync --testslive6 --nossl2
}

echo imapsync_docker_add_tag_version
imapsync_docker_add_tag_version() {
    docker images gilleslamiral/imapsync
    echo 
    version=`docker run gilleslamiral/imapsync imapsync --version`
    echo $version
    docker tag gilleslamiral/imapsync:latest gilleslamiral/imapsync:$version
    docker images gilleslamiral/imapsync
}



echo imapsync_docker_testslive
imapsync_docker_testslive() {
    echo docker run gilleslamiral/imapsync imapsync --testslive
    docker run gilleslamiral/imapsync imapsync --testslive
}

echo imapsync_docker_testslive6
imapsync_docker_testslive6() {
    echo docker run gilleslamiral/imapsync imapsync --testslive6 --ssl1 --ssl2
    docker run gilleslamiral/imapsync imapsync --testslive6 --ssl1 --ssl2
}

echo imapsync_docker_tests
imapsync_docker_tests() {
    echo docker run gilleslamiral/imapsync imapsync --tests
    docker run gilleslamiral/imapsync imapsync --tests
}

echo docker_delete_all_images
docker_delete_all_images() {
    docker rm `docker ps -a -q`
    docker rmi `docker images -q`
}

echo docker_system_prune
docker_system_prune()
{
        docker system prune
}

echo docker_delete_dandling_images
docker_delete_dandling_images() {
	docker images
	dandling_images=`docker images -f dangling=true -q`
	exited_containers=`docker ps -a -f status=exited -q`
	test -n "$exited_containers" && docker rm $exited_containers
	test -n "$dandling_images"   && docker rmi $dandling_images
	docker images
}


echo imapsync_docker_rebuild_from_scratch
imapsync_docker_rebuild_from_scratch() {
    delete_all_images
    docker images
    imapsync_docker_build
}

echo imapsync_docker_upload
imapsync_docker_upload() {
    # upload only after successful tests
    set -x \
    && imapsync_docker_testslive \
    && imapsync_docker_tests \
    && docker run gilleslamiral/imapsync true \
    && docker login --username=gilleslamiral --password=`cat $HOME/var/pass/secret.docker` \
    && docker push gilleslamiral/imapsync:latest \
    && set +x && echo imapsync push SUCCEEDED || echo imapsync push FAILED
    set +x
}


