#!/bin/bash

set -e

# This is install-issue. Used for clean install of Linux Gazette
# Issues on Debian GNU/* Systems. Maybe this is bad bash-"code", but
# it works (at least for me :) ).
# If you want a better one send me a patch.
# But this should never be called by a human, only from my maintainer-
# scripts, so i dont care much about that.
#
# Today it has not much todo. But i dont do that in the maintainer-scripts.
# If i want to change anything in the future it is a little bit simpler to
# only change one script instead of >80 :)
#
# What it does: 
# Install-Time:
# Untars the tarball for the issue into the right dir.
# Then it calls fixlinks for it on the new files *and* the index of lg-base
# to correct the links.
#
# Remove-Time:
# Removes the extracted files and calls fixlinks again.
#
# This is Copyright (C) 2002 Joerg Jaspert <joerg@debian.org>.
# This is free software; see the GNU General Public Licence version 2
# or later for copying conditions.  There is NO warranty.
# $Id: install-issue,v 1.12 2004/10/31 00:34:41 joerg Exp $

version="0.0.0.0.0.12"

if [ $# -lt 2 ]; then
    cat <<EOF
This is Copyright (C) 2002-2004 Joerg Jaspert <joerg@debian.org>.
This is free software; see the GNU General Public Licence version 2
or later for copying conditions.  There is NO warranty.

install-issue $version - Only for use in the maintainer-scripts of lg-issue* packages
usage: install-issue [install|remove] ISSUENR

EOF

exit 1;

fi;

work=$1;
issue=$2;

lgbase=/usr/share/doc/lg;
scriptpath=/usr/share/lg/scripts
tarissue=/usr/share/doc/lg-issue${issue}/lg-issue${issue}.tar.gz

if [ ${issue} != "01to08" ] && [ ${issue} -ge 100 ]; then
	issuepath=${lgbase}/${issue};
else
	issuepath=${lgbase}/issue${issue};
fi


if [ $work = "install" ]; then
    if [ -z ${tarissue} ]
	then
	echo "The issue is missing. Why?";
	exit 1;
    fi;
    if [ -d $issuepath ]; then
	cd ${issuepath};
    else
	mkdir -p ${issuepath}
	cd ${issuepath};
    fi
    tar xzf ${tarissue};
    if [ "$issue" = "01to08" ]; then
	${scriptpath}/fixlinks ${lgbase}/index.html ${lgbase}/lg_index.html ${issuepath}/gazette_toc.html ${issuepath}/index.html
    else
	${scriptpath}/fixlinks ${lgbase}/index.html ${lgbase}/lg_index.html ${issuepath}/index.html
    fi
    exit 0;
    
elif [ $work = "remove" ]; then
    if [ -d ${issuepath} ]; then
	cd ${issuepath};
	if [ "$issue" = "01to08" ]; then
	    /usr/bin/find . ! -name gazette_toc.html -exec rm -rf \{\} \; >/dev/null 2>&1 || true
		${scriptpath}/fixlinks ${lgbase}/index.html ${lgbase}/lg_index.html ${issuepath}/gazette_toc.html
	else
	    /usr/bin/find . ! -name index.html -exec rm -rf \{\} \; >/dev/null 2>&1 || true
		${scriptpath}/fixlinks ${lgbase}/index.html ${lgbase}/lg_index.html ${issuepath}/index.html
	fi
    else
	echo "There is an error removing the issue ${issue} in ${issuepath}"
	exit 3;
    fi
    exit 0;
    
else
    echo "Unknown Work-Mode $work...";
    exit 2;
fi

