#!/bin/sh
# ##############################################################################
# ############################### updarp ######################################
# ##############################################################################
#            Copyright (c) 2015-2017 Md. Jahidul Hamid
# 
# -----------------------------------------------------------------------------
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 
#     * Redistributions of source code must retain the above copyright notice,
#       this list of conditions and the following disclaimer.
# 
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
# 
#     * The names of its contributors may not be used to endorse or promote 
#       products derived from this software without specific prior written
#       permission.
#       
# Disclaimer:
# 
#     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
#     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
#     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
#     ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
#     LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
#     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
#     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
#     INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
#     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
#     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
#     POSSIBILITY OF SUCH DAMAGE.
# ##############################################################################

set -e
pname='updarp'
dist="stretch"
arch="amd64"
extracted="extracted"
label="Debialin"
origin="Debian"
components="main"
suit="$dist"
override="$dist"

help="
Options:
	-d , --dist 	:	dist [$dist]
	-a, --arch		:	architecture [$arch]
	-t, --target	:	targent directory [$extracted]
	-l, --label		:	label [$label]
	-o, --origin	:	origin [$origin]
	-c, --components:	components [$components]
	-s, --suit		:	suit [$suit]
	-ov, --override	:	override [$override]
	-p src dest		:	create pool from src and save to dest
	-h, --help		:	help menu
"

crpool(){
	dir="${1-apt.udeb/cache/archives}"
	odir="${2-.}"
	find "$dir" -maxdepth 1 -type f -exec sh -c '
	p=$0
	odir=$1
	n=$(basename $p)
	pn=$(echo "$n" |sed -e "s/-udeb.*$//" -e "s/[_-][0-9].*$//")
	if echo "$pn" | grep -q "^lib."; then
	pn1=$(echo "$pn" |sed -E "s/^(....).*/\1/")
	else
	pn1=$(echo "$pn" |sed -E "s/^(.).*$/\1/")
	fi 
	mkdir -p  "$odir/pool/main/$pn1/$pn"
	cp -L "$p" "$odir/pool/main/$pn1/$pn"
	' '{}' "$odir" \;
}

while [ $# -gt 0 ]
do
    case "$1" in
    	-d|--dist)
		shift
    		dist="${1-$dist}"
    		;;
        -a|--arch)
		shift
        	arch="${1-$arch}"
            ;;
        -t|--target)
		shift
        	extracted="${1-$extracted}"
            ;;
	-l|--label)
		shift
		label="${1-$label}"
            ;;
	-o|--origin)
		shift
		origin="${1-$origin}"
            ;;
	-c|--components)
		shift
		components="${1-$components}"
            ;;
	-s|--suit)
		shift
		suit="${1-$suit}"
		;;
	-ov|--override)
		shift
		override="$1" #can be empty
		;;
	-p|--pool)
		crpool ${2+"$2"} ${3+"$3"}
		exit 0
		;;
    -h|--help|*)
        echo "$help"
        exit 0
        ;;
    esac
    shift
done

if [ "$override" = "" ]; then
	override="$dist"
fi

override_url=http://ftp.de.debian.org/debian/indices/override.$override.main.gz

cachedir="$pname".cache

mkdir -p "$cachedir"

wget -c "$override_url" -O - | gunzip --stdout > "$cachedir"/override

config_common='
 Dir {
    ArchiveDir "'$extracted'";
    OverrideDir "'$cachedir'";
    CacheDir "'$cachedir'";
 };
            
 TreeDefault {
    Directory "pool/";
 };
'

config_deb_tmp="$cachedir/config-deb"
cat > "$config_deb_tmp" <<EOF
$config_common

 BinDirectory "pool/main" {
    Packages "dists/$dist/main/binary-$arch/Packages";
    BinOverride "override";
 };
                                   
 Default {
    Packages {
        Extensions ".deb";
    };
 };
EOF

config_udeb_tmp="$cachedir/config-udeb"
cat > "$config_udeb_tmp"<<EOF
$config_common
                    
 BinDirectory "pool/main" {
    Packages "dists/$dist/main/debian-installer/binary-$arch/Packages";
    BinOverride "override";
 };
                                   
 Default {
    Packages {
        Extensions ".udeb";
    };
 };
EOF

config_rel="$cachedir/config-release"

cat > "$config_rel" <<EOF
APT::FTPArchive::Release::Codename "$dist";
APT::FTPArchive::Release::Origin "$origin";
APT::FTPArchive::Release::Components "$components";
APT::FTPArchive::Release::Label "$label";
APT::FTPArchive::Release::Architectures "$arch";
APT::FTPArchive::Release::Suite "$suit";
EOF

apt-ftparchive generate "$config_deb_tmp"
apt-ftparchive generate "$config_udeb_tmp"
apt-ftparchive -c "$config_rel" release "$extracted/dists/$dist" > "extracted/dists/$dist/Release"


