EXTRACT_DIR=images
PLAYOUT_IMAGE_TAR_GZ=images/cineplayout-srv.tar.gz
PLAYOUT_IMAGE_TAR=images/cineplayout-srv.tar

BARCO_IMAGE_TAR_GZ=images/cineplayer-barco-srv.tar.gz
BARCO_IMAGE_TAR=images/cineplayer-barco-srv.tar

HULA_IMAGE_TAR_GZ=images/cineplayer-hula-srv.tar.gz
HULA_IMAGE_TAR=images/cineplayer-hula-srv.tar

HULA2_IMAGE_TAR_GZ=images/cineplayer-hula2-srv.tar.gz
HULA2_IMAGE_TAR=images/cineplayer-hula2-srv.tar

die () {
    echo >&2 "$@"
    exit 1
}

UpdatePlayoutImage(){
    cd ${EXTRACT_DIR}
    echo "Downloading Playout container image"

    curl -O http://software.bitonlive.net/cinema/Setup/InstallDatastore/dockers/CinePlayout/cineplayout-srv.tar.gz
    tar -zxvf cineplayout-srv.tar.gz cineplayout-srv.tar
    
    cd ..

    docker -H :3000 load -i ${PLAYOUT_IMAGE_TAR}
}

UpdateBarcoImage(){
    cd ${EXTRACT_DIR}
    echo "Downloading CinePlayerBarco container image"

    curl -O http://software.bitonlive.net/cinema/Setup/InstallDatastore/dockers/Barco/Player/cineplayer-barco-srv.tar.gz
    tar -zxvf cineplayer-barco-srv.tar.gz cineplayer-barco-srv.tar

    cd ..
    docker -H :3000 load -i ${BARCO_IMAGE_TAR}
}

UpdateHulaImage(){
    cd ${EXTRACT_DIR}
    echo "Downloading CinePlayerHula container image"

    curl -O http://software.bitonlive.net/cinema/Setup/InstallDatastore/dockers/Hula/Player/cineplayer-hula-srv.tar.gz
    tar -zxvf cineplayer-hula-srv.tar.gz cineplayer-hula-srv.tar

    cd ..

    docker -H :3000 load -i ${HULA_IMAGE_TAR}
}

UpdateHulaImage(){
    cd ${EXTRACT_DIR}
    echo "Downloading CinePlayerHula container image"

    curl -O http://software.bitonlive.net/cinema/Setup/InstallDatastore/dockers/Hula/Player/cineplayer-hula-srv.tar.gz
    tar -zxvf cineplayer-hula-srv.tar.gz cineplayer-hula-srv.tar

    cd ..

    docker -H :3000 load -i ${HULA_IMAGE_TAR}
}

UpdateHula2Image(){
    cd ${EXTRACT_DIR}
    echo "Downloading CinePlayerHula2 container image"

    curl -O http://software.bitonlive.net/cinema/Setup/InstallDatastore/dockers/Hula/Player/cineplayer-hula2-srv.tar.gz
    tar -zxvf cineplayer-hula2-srv.tar.gz cineplayer-hula2-srv.tar

    cd ..

    docker -H :3000 load -i ${HULA2_IMAGE_TAR}
}

CreateCinePlayerContainer(){
    echo "Creating ${CINEPLAYER_CONTAINER} container with address ${CINEPLAYER_CONTAINER_ADDRESS} ..."

    docker -H :3000 run --restart="always" --network=net-cineplayer --ip=${CINEPLAYER_CONTAINER_ADDRESS} -d -P -it --name ${CINEPLAYER_CONTAINER} -e DOCKERNAME=${CINEPLAYER_CONTAINER} -e IMB=${IMB_NAME} -e THEATER=${THEATER_INDEX} -v /datastore:/datastore -v /etc/localtime:/etc/localtime:ro cineplayout-srv
}

CreateCinePlayoutContainer(){
    CINEPLAYOUT_CONTAINER=cineplayout-${IMB_NAME}-${THEATER_INDEX}
    CINEPLAYOUT_CONTAINER_ADDRESS=172.18.0.$((${THEATER_INDEX} * 10 + 1))

    CINEPLAYOUT_CONTAINER_PSTART=$((${THEATER_INDEX} * 10 + 48000))
    CINEPLAYOUT_CONTAINER_PEND=$((${THEATER_INDEX} * 10 + 48009))

    echo "Creating ${CINEPLAYOUT_CONTAINER} container with address ${CINEPLAYOUT_CONTAINER_ADDRESS} ..."

    docker -H :3000 run --restart="always" --network=net-cineplayer --ip=${CINEPLAYOUT_CONTAINER_ADDRESS} -d -P -p ${CINEPLAYOUT_CONTAINER_PSTART}-${CINEPLAYOUT_CONTAINER_PEND}:${CINEPLAYOUT_CONTAINER_PSTART}-${CINEPLAYOUT_CONTAINER_PEND}/tcp -p ${CINEPLAYOUT_CONTAINER_PSTART}-${CINEPLAYOUT_CONTAINER_PEND}:${CINEPLAYOUT_CONTAINER_PSTART}-${CINEPLAYOUT_CONTAINER_PEND}/udp -it --name ${CINEPLAYOUT_CONTAINER} -e DOCKERNAME=${CINEPLAYOUT_CONTAINER} -e IMB=${IMB_NAME} -e THEATER=${THEATER_INDEX} -v /datastore:/datastore -v /etc/localtime:/etc/localtime:ro cineplayout-srv
}

CreateCinePlayerBarcoContainer(){
    echo "Creating ${CINEPLAYER_CONTAINER} container with address ${CINEPLAYER_CONTAINER_ADDRESS} ..."

    docker -H :3000 run --restart="always" --network=net-cineplayer --ip=${CINEPLAYER_CONTAINER_ADDRESS} -d -P -it --name ${CINEPLAYER_CONTAINER} -e DOCKERNAME=${CINEPLAYER_CONTAINER} -e IMB=${IMB_NAME} -e THEATER=${THEATER_INDEX} -v /datastore:/datastore -v /etc/localtime:/etc/localtime:ro cineplayer-barco-srv
}

CreateCinePlayerHulaContainer(){
    echo "Creating ${CINEPLAYER_CONTAINER} container with address ${CINEPLAYER_CONTAINER_ADDRESS} ..."

    docker -H :3000 run --restart="always" --network=net-cineplayer --ip=${CINEPLAYER_CONTAINER_ADDRESS} -d -P -it --name ${CINEPLAYER_CONTAINER} -e DOCKERNAME=${CINEPLAYER_CONTAINER} -e IMB=${IMB_NAME} -e THEATER=${THEATER_INDEX} -e AssetStoreHost=172.18.0.2 -v /datastore:/datastore -v /etc/localtime:/etc/localtime:ro cineplayer-hula-srv
}

CreateCinePlayerHula2Container(){
    echo "Creating ${CINEPLAYER_CONTAINER} hula2 container with address ${CINEPLAYER_CONTAINER_ADDRESS} ..."

    docker -H :3000 run --restart="always" --network=net-cineplayer --ip=${CINEPLAYER_CONTAINER_ADDRESS} -d -P -it --name ${CINEPLAYER_CONTAINER} -e DOCKERNAME=${CINEPLAYER_CONTAINER} -e IMB=${IMB_NAME} -e THEATER=${THEATER_INDEX} -e AssetStoreHost=172.18.0.2 -v /datastore:/datastore -v /etc/localtime:/etc/localtime:ro cineplayer-hula2-srv
}

InstallCinePlayoutContainer(){
    mkdir ${EXTRACT_DIR}

    # User may have forced the image pull
    if [ "$#" -eq 4 -a "$4" == "--update-images" ]
    then
        UpdatePlayoutImage
        UpdateBarcoImage
        UpdateHulaImage
        UpdateHula2Image
    fi
    # If image file not exists, pull it
    if [ ! -f "${PLAYOUT_IMAGE_TAR}" ]
    then
        UpdatePlayoutImage
    fi
    if [ ! -f "${BARCO_IMAGE_TAR}" -a "$IMB_NAME" == "barco" ]
    then
        UpdateBarcoImage
    fi
    if [ ! -f "${HULA_IMAGE_TAR}" -a "$IMB_NAME" == "hula" ]
    then
        UpdateHulaImage
    fi
    if [ ! -f "${HULA2_IMAGE_TAR}" -a "$IMB_NAME" == "hula" ]
    then
        UpdateHula2Image
    fi

    ## ********** CinePlayer preparation section *************
    CINEPLAYER_CONTAINER=cineplayer-${IMB_NAME}-${THEATER_INDEX}
    CINEPLAYER_CONTAINER_ADDRESS=172.18.0.$((${THEATER_INDEX} * 10))
    
    if [ "$IMB_NAME" == "barco" ]
    then
        CreateCinePlayerBarcoContainer
    elif [ "$IMB_NAME" == "hula" ]
    then
        if [ $USE_HULA_V2 -eq 1 ]
        then
            CreateCinePlayerHula2Container
        else
            CreateCinePlayerHulaContainer
        fi
        
    elif [ "$IMB_NAME" == "gdc" -o "$IMB_NAME" == "dolby" ]
    then
        CreateCinePlayerContainer 
    else
        die "IMB ${IMB_NAME} not supported"
    fi

    ## ********** CinePlayout preparation section *************

    CreateCinePlayoutContainer

    # If we are installing the dolby, we must configure the mounts manager
    if [ "$IMB_NAME" == "dolby" ]
    then
        echo "Configuring Dolby mounts service on /datastore/cinemounts/theater${THEATER_INDEX}"

        sudo systemctl stop mountsmanager@"/datastore/cinemounts/theater${THEATER_INDEX}"
        sudo systemctl disable mountsmanager@-datastore-cinemounts-theater${THEATER_INDEX}

        sudo systemctl start mountsmanager@"/datastore/cinemounts/theater${THEATER_INDEX}"
        sudo systemctl enable mountsmanager@-datastore-cinemounts-theater${THEATER_INDEX}
    fi
}

UpdateCinePlayoutSw(){
    curl -O http://software.bitonlive.net/cinema/Setup/InstallDatastore/dockers/CinePlayout/update-cineplayout.sh

    CINEPLAYOUT_CONTAINER=cineplayout-${IMB_NAME}-${THEATER_INDEX}
    echo "Updating ${CINEPLAYOUT_CONTAINER}"


    docker -H :3000 exec -t ${CINEPLAYOUT_CONTAINER} rm /var/opt/cineplayer/bin/update-cineplayout.sh
    docker -H :3000 cp update-cineplayout.sh ${CINEPLAYOUT_CONTAINER}:/var/opt/cineplayer/bin
    
    docker -H :3000 exec -t ${CINEPLAYOUT_CONTAINER} chmod +x /var/opt/cineplayer/bin/update-cineplayout.sh
    docker -H :3000 exec -t ${CINEPLAYOUT_CONTAINER} /var/opt/cineplayer/bin/update-cineplayout.sh

    docker -H :3000 restart ${CINEPLAYOUT_CONTAINER}
}

UpdateCinePlayerSw(){
    CINEPLAYER_CONTAINER=cineplayer-${IMB_NAME}-${THEATER_INDEX}

    if [ "$IMB_NAME" == "barco" ]
    then
        curl http://software.bitonlive.net/cinema/Setup/InstallDatastore/dockers/Barco/Player/CinePlayerBarco.out -o CinePlayerBarco

        docker -H :3000 cp CinePlayerBarco ${CINEPLAYER_CONTAINER}:/var/opt/cineplayer/bin
        docker -H :3000 exec -t ${CINEPLAYER_CONTAINER} chmod +x /var/opt/cineplayer/bin/CinePlayerBarco
        docker -H :3000 exec -t ${CINEPLAYER_CONTAINER} pkill -6 -f CinePlayer++
        docker -H :3000 exec -t ${CINEPLAYER_CONTAINER} mv /var/opt/cineplayer/bin/CinePlayerBarco /var/opt/cineplayer/bin/CinePlayer++

        rm CinePlayerBarco
    elif [ "$IMB_NAME" == "hula" ]
    then
        if [ $USE_HULA_V2 -eq 1 ]
        then
            HULA_EXE=CinePlayer2
        else
            HULA_EXE=CinePlayer
        fi

        curl http://software.bitonlive.net/cinema/Setup/InstallDatastore/dockers/Hula/Player/${HULA_EXE} -o CinePlayerHula

        docker -H :3000 cp CinePlayerHula ${CINEPLAYER_CONTAINER}:/var/opt/cineplayer/bin
        docker -H :3000 exec -t ${CINEPLAYER_CONTAINER} chmod +x /var/opt/cineplayer/bin/CinePlayerHula
        docker -H :3000 exec -t ${CINEPLAYER_CONTAINER} pkill -6 -f ${HULA_EXE}
        docker -H :3000 exec -t ${CINEPLAYER_CONTAINER} mv /var/opt/cineplayer/bin/CinePlayerHula /var/opt/cineplayer/bin/${HULA_EXE}

        rm CinePlayerHula
    else
        echo "Updating ${CINEPLAYER_CONTAINER}"

        docker -H :3000 exec -t ${CINEPLAYER_CONTAINER} curl http://software.bitonlive.net/cinema/Setup/InstallDatastore/dockers/CinePlayout/update-cineplayer.sh -o /var/opt/cineplayer/bin/update-cineplayer.sh
        
        docker -H :3000 exec -t ${CINEPLAYER_CONTAINER} chmod +x /var/opt/cineplayer/bin/update-cineplayer.sh
        docker -H :3000 exec -t ${CINEPLAYER_CONTAINER} /var/opt/cineplayer/bin/update-cineplayer.sh

        docker -H :3000 restart ${CINEPLAYER_CONTAINER}
    fi
}

# Help parameter parsing
if [ "$#" -eq 1 -a "$1" == "--help" ]
then
    echo "Install CinePlayout CLI version 0.0.0.1"
    echo ""
    echo "Command format:"
    echo "install-cineplayout.sh (container|playout|player) imb-type theater [--update-image]"

    echo ""
    echo "The container operation will download (if necessary) and install the new docker container"
    echo "The player operation will update the player relative to the specified imb and theater"
    echo "The playout operation will update the playout relative to the specified imb and theater"
    echo "Example:"
    echo "install-cineplayout.sh docker dolby 3"
    echo "install-cineplayout.sh playout dolby 3"
    exit 0
fi

# Theater parameter validation
[ "$#" -eq 3 -o "$#" -eq 4 ] || die "3 argument required, $# provided. See --help for command line help" 
echo "$3" | grep -E -q '^[1]?[0-9]$' || die "Theater numeric argument required, $3 provided"

# We prepare the IMB name lowercase and the thear
IMB_NAME=$(echo "${2,,}")
THEATER_INDEX="$3"
USE_HULA_V2=0

if [ "$IMB_NAME" == "hula2" ]
then
    IMB_NAME="hula"
    USE_HULA_V2=1
fi

if [ "$1" == "container" ]    
then
    InstallCinePlayoutContainer
    UpdateCinePlayerSw
    UpdateCinePlayoutSw
elif [ "$1" == "playout" ]
then
    UpdateCinePlayoutSw
elif [ "$1" == "player" ]
then
    UpdateCinePlayerSw
else
    die "Invalid operation $1"
fi    

