#!/bin/bash
################################################################
#
# Enable docker build support in container.
#
# Author: Marco Strigl
#
################################################################
#
# Copyright (c) 2017 SUSE Linux Products GmbH
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 or 3 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program (see the file COPYING); if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#
################################################################

LOCAL_REPOS_D="/etc/repos_obs_dockersupport.d/"
LOCAL_APTREPOS_D="/etc/aptrepos_obs_dockersupport.d/"
DATA_DIR=

zypper() {
    cmd=
    # try to find the command
    globalopts=()
    while test -n "$1"; do
        case $1 in
        -*)
	    globalopts[${#globalopts[@]}]="$1"
	    shift
	    ;;
	*)  
	    cmd=$1
	    shift
	    break
	    ;;
	esac
    done
    case $cmd in
    in|install|rm|remove|up|update|if|info)
	exec /usr/bin/zypper -D $LOCAL_REPOS_D "${globalopts[@]}" "$cmd" "$@"
	;;
    ar|addrepo)
	exec /usr/bin/zypper "${globalopts[@]}" "$cmd" -C "$@"
	;;
    ref|refresh)
	echo "skipping zypper refresh"
	exit 0
	;;
    *)
	exec /usr/bin/zypper "${globalopts[@]}" "$cmd" "$@"
	;;
    esac
}

obs_pkg_mgr() {
    case "$1" in
    install|remove)
	shift
	exec /usr/bin/zypper -D $LOCAL_REPOS_D --no-gpg-checks -n in "$@"
	;;
    add_repo)
	shift
	exec /usr/bin/zypper ar -C "$@"
	;;
    *)
	echo "Usage: obs_pkg_mgr (install|add_repo) args" >&2
	exit 1
	;;
    esac
}

apt_get() {
    cmd=
    # try to find the command
    globalopts=()
    while test -n "$1"; do
        case $1 in
        -*)
	    globalopts[${#globalopts[@]}]="$1"
	    shift
	    ;;
	*)  
	    cmd=$1
	    shift
	    break
	    ;;
	esac
    done
    case $cmd in
    update)
	exit 0
	;;
    install|upgrade)
	exec /usr/bin/apt-get -o Dir::Etc::SourceList=$LOCAL_APTREPOS_D/obssource -o Dir::Etc::SourceParts=$LOCAL_APTREPOS_D --allow-unauthenticated "${globalopts[@]}" "$cmd" "$@"
	;;
    *)
	exec /usr/bin/apt-get "${globalopts[@]}" "$cmd" "$@"
	;;
    esac
}

dnf() {
    # try to find command
    globalopts=()
    while test -n "$1"; do
        case $1 in
        -*)
	    globalopts[${#globalopts[@]}]="$1"
	    shift
	    ;;
	*)  
	    cmd=$1
	    shift
	    break
	    ;;
	esac
    done
    case $cmd in
    in|install|up|update)
	exec /usr/bin/dnf --setopt=reposdir=$LOCAL_REPOS_D "${globalopts[@]}" "$cmd" "$@"
	;;
    *)
	exec /usr/bin/dnf "${globalopts[@]}" "$cmd" "$@"
	;;
    esac
}

yum() {
    # try to find command
    globalopts=()
    while test -n "$1"; do
        case $1 in
        -*)
	    globalopts[${#globalopts[@]}]="$1"
	    shift
	    ;;
	*)  
	    cmd=$1
	    shift
	    break
	    ;;
	esac
    done
    case $cmd in
    in|install|up|update)
	exec /usr/bin/yum --setopt=reposdir=$LOCAL_REPOS_D "${globalopts[@]}" "$cmd" "$@"
	;;
    *)
	exec /usr/bin/yum "${globalopts[@]}" "$cmd" "$@"
	;;
    esac
}

obs_docker_support() {
    case "$1" in
    --install|-i)
	data_url=http://localhost:80
	test -n "$DATA_DIR" && data_url="file:$DATA_DIR"
	test -e /usr/bin/zypper && ln -s obs-docker-support /usr/local/sbin/zypper
	test -e /usr/bin/yum && ln -s obs-docker-support /usr/local/sbin/yum
	test -e /usr/bin/dnf && ln -s obs-docker-support /usr/local/sbin/dnf
	test -e /usr/bin/apt-get && ln -s obs-docker-support /usr/local/sbin/apt-get
	ln -s obs-docker-support /usr/local/sbin/obs_pkg_mgr
	if test -e /usr/bin/zypper -o -e /usr/bin/yum -o -e /usr/bin/dnf ; then
	    mkdir -p "$LOCAL_REPOS_D"
	    cat >$LOCAL_REPOS_D/obs_repository.repo <<EOF
[obs_repository]
name=obs_repository
enabled=1
autorefresh=0
baseurl=$data_url
type=rpm-md
gpgcheck=0
EOF
	    test -x /usr/bin/zypper && /usr/bin/zypper -D $LOCAL_REPOS_D ref
	fi
	if test -e /usr/bin/apt-get ; then
	    mkdir -p "$LOCAL_APTREPOS_D"
	    echo "deb $data_url ./" > $LOCAL_APTREPOS_D/obssource
	    test -e /var/lib/apt && mv /var/lib/apt /var/lib/apt.obssave
	    /usr/bin/apt-get -o Dir::Etc::SourceList=$LOCAL_APTREPOS_D/obssource -o Dir::Etc::SourceParts=$LOCAL_APTREPOS_D update
	fi
    ;;
    --uninstall|-u)
	rm -rf "$LOCAL_REPOS_D"
	rm -rf "$LOCAL_APTREPOS_D"
	rm -f /usr/local/sbin/zypper
	rm -f /usr/local/sbin/yum
	rm -f /usr/local/sbin/dnf
	rm -f /usr/local/sbin/apt-get
	rm -f /usr/local/sbin/obs_pkg_mgr
	if test -e /var/lib/apt.obssave ; then
	    rm -rf /var/lib/apt
	    mv /var/lib/apt.obssave /var/lib/apt
	fi
	rm -f /usr/local/sbin/obs-docker-support
    ;;
    esac
}

case ${0##*/} in
obs-docker-support)
    obs_docker_support "$@"
    ;;
obs_pkg_mgr)
    obs_pkg_mgr "$@"
    ;;
zypper)
    zypper "$@"
    ;;
apt-get)
    apt_get "$@"
    ;;
*)
    echo "obs-docker-support: unsupported mode ${0##*/}" >&2
    exit 1
esac

