#!/bin/bash

set -x

if [ -f isobuilder.conf ];then
    source isobuilder.conf
else
    source /etc/isobuilder.conf
fi

download_udeb_pkg()
{
    local PKG=$1
    UDEB_PATH=$(curl -s ${MIRROR}/dists/${CODENAME}/main/debian-installer/binary-${ARCH}/Packages.gz | zcat | grep Filename | grep $PKG | awk '{print $2}')
    wget ${MIRROR}/${UDEB_PATH}
}

download_deb_pkg()
{
    local FILE=$1
    cd ${WORK_DIR}/pkg/ && /usr/lib/isobuilder/tools/download_deb.sh "$(cat $FILE | tr '\n' ' ' )"
}

create_local_repo()
{
    mkdir -pv ${WORK_DIR}/mirror/conf
    REPO_CONF=${WORK_DIR}/mirror/conf/distributions

    case $ARCH in
       amd64|i386)
            ARCH_OPTS="i386 amd64 source"
            ;;
       mips64el|mipsel)
            ARCH_OPTS="mipsel mips64el source"
            ;;
       *)
       echo "unknown arch"
            ;;
       esac

cat > ${REPO_CONF} << EOF
Codename: $CODENAME
Description: deepin official main repository
Architectures: ${ARCH_OPTS}
Components: main contrib non-free
UDebComponents: main
Contents: .gz
Suite: stable
#SignWith: yes
EOF
    cd $WORK_DIR/mirror                                  &&    \
    reprepro includedeb $CODENAME $WORK_DIR/pkg/*.deb    &&    \
    reprepro includeudeb $CODENAME $WORK_DIR/pkg/*.udeb
}

get_all_pkg()
{
    local FILE=$1
    INCLUDE_DEB="$(cat $FILE | tr '\n' ' ' )"
    RET=$(debootstrap --print-debs --no-check-gpg --include=$INCLUDE_DEB --components=main,non-free,contrib --arch=$ARCH $CODENAME $TDIR $MIRROR)
    echo $RET
}

do_it()
{

    [ ! -d "$WORK_DIR" ] && mkdir -pv ${WORK_DIR}/

    case $TASK in
        download_pkg)
            mkdir -pv $WORK_DIR/pkg/
            download_deb_pkg "/usr/lib/isobuilder/data/${TYPE}-all-deb.list"

            for udeb_pkg in `cat /usr/lib/isobuilder/data/all-udeb.list`
            do
                download_udeb_pkg $udeb_pkg
            done

         ;;
        create_repo)
            create_local_repo
            mkdir -pv ${WORK_DIR}/mirror/dists/${CODENAME}/main/installer-${ARCH}/current/images/
            cp -av ${DI_DIR}/* ${WORK_DIR}/mirror/dists/${CODENAME}/main/installer-${ARCH}/current/images/
            if [ "$?" == "0" ]; then
                echo "create repo is complete"
            else
                echo "create repo is failed"
            fi
            ;;
        create_iso)
            mkdir -pv ${WORK_DIR}/preseed/ && cp -av /usr/lib/isobuilder/preseed/${TYPE}/* ${WORK_DIR}/preseed/
            cp -av /usr/lib/isobuilder/deepin-cd ${WORK_DIR}/

            cd ${WORK_DIR}/deepin-cd && bash build.sh
            if [ "$?" == "0" ];then
                echo "iso is build ok"
            else
                echo "iso is build failed"
            fi
            ;;
        clean_all)
            rm -rvf ${WORK_DIR}/tmp*
            rm -rvf ${WORK_DIR}/pkg
            rm -rvf ${WORK_DIR}/mirror
            rm -rvf ${WORK_DIR}/deepin-cd
            ;;
        *)
            echo "Unkown option";;
    esac
}


get_help()
{
    cat << EOF
Deepin-ISO-builder - create custom debian-installer CD/DVD

Usges:
    isobuiler action

[action]

--download_pkg   download pkgs from apt-mirror
--create_repo    use download pkgs generate repo
--create_iso     use local repo generate iso
--clean_all      clean all builddir

example:

    isobuilder  --download_pkg
    isobuilder  --create_repo
    isobuilder  --create_iso
EOF
}

cmd_opts="$@"
if [ -n "$cmd_opts" ];then
    while [ -n "$1" ];
do
    opt=${1}
    case "${opt}" in
        --download_pkg)
                TASK="download_pkg"
                shift ;;
        --create_repo)
                TASK="create_repo"
                shift ;;
        --create_iso)
                TASK="create_iso"
                shift ;;
        --clean_all)
                TASK="clean_all"
                shift ;;
        --help)
            get_help
            exit 0;;
        *)
            echo "Unknown options  please see help docs!"
            get_help
            exit 0;;
    esac
done

    do_it $TASK
else
    get_help
fi
