mirror-new-cd

# This file is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This file 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 file, if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

# Assumes previous cd in the series has already been processed

usage_message () {
    echo "Usage: $(basename $0) [options] \\
         cd-iso-directory cd-contents-directory version-string \\
         cd-name cd-mirror-dir iso-name"
    echo ""

    echo "Options:

    [-v|--verbose] 
    [-b|--old-base-dir old-cd-iso-dir]
    [-c|--old-contents-dir old-contents-dir] 
    [-o|--old-version old-version]
    [-n|--old-name old-cd-name]
    [-i|--old-iso old-iso-name]
    [-f|--from-iso] (Assume iso's already created, don't use jigdo)
    [-e|--extracd] (Don't add cd files to mirror, e.g. xfce is duplicates)
    [-t|--test-param]

"

}

pushd () {
    OLDCURDIR="$(pwd)"
    cd "$1"
}

popd () {
    cd "$OLDCURDIR"
}


PARSEDPARAMS=$(getopt --options "vb:c:to:n:i:fe" --longoptions "verbose,old-base-dir:,old-contents-dir:,test-param,old-version:,old-name:,old-iso:,from-iso,extracd" --name "$0" --shell sh -- "$@")

if [ $? != 0 ]; then
    echo "Aborting..." >&2
    exit 1
fi

TESTPARAM=FALSE
VERBOSE=FALSE
FROMISO=FALSE
EXTRACD=FALSE
OLDCDBASEDIR=
OLDCONTENTSDIR=
OLDVERSION=
OLDNAME=
OLDISO=
VERBOSE_ARG=
OLDBASE_ARG=
OLDCONTENTS_ARG=
OLDCDNAME_ARG=
OLDISO_ARG=
OLDVERSION_ARG=

UPDATE=FALSE


eval set -- "$PARSEDPARAMS"

while true; do
    case "$1" in
        --verbose|-v)
            VERBOSE=TRUE
            echo "Verbose messages enabled"
            VERBOSE_ARG="$1"
            shift
            ;;
        --old-base-dir|-b)
            OLDCDBASEDIR=$(readlink --no-newline --canonicalize-existing "$2")
            if [ -z "$OLDCDBASEDIR" ]; then
                usage_message
                echo "Old ISO directory '$2' missing (dangling symlink?)" >&2
                exit 1
            fi
            UPDATE=TRUE
            OLDBASE_ARG="$1"" ""$2"
            shift 2
            ;;
        --old-contents-dir|-c)
            OLDCONTENTSDIR=$(readlink --no-newline --canonicalize-existing "$2")
            if [ -z "$OLDCDBASEDIR" ]; then
                usage_message
                echo "Old contents directory '$2' missing (dangling symlink?)" >&2
                exit 1
            fi
            UPDATE=TRUE
            OLDCONTENTS_ARG="$1"" ""$2"
            shift 2
            ;;
        -f|--from-iso)
            FROMISO=TRUE
            shift
            ;;
        -e|--extracd)
            EXTRACD=TRUE
            shift
            ;;
        --test-param|-t)
            TESTPARAM=TRUE
            shift
            ;;
        -o|--old-version)
            OLDVERSION="$2"
            OLDVERSION_ARG="$1"" ""$2"
            UPDATE=TRUE
            shift 2
            ;;
        -n|--old-name)
            OLDCDNAME="$2"
            UPDATE=TRUE
            shift 2
            ;;
        -i|--old-iso)
            OLDISONAME="$2"
            UPDATE=TRUE
            shift 2
            ;;
        --) 
            shift
            break
            ;;
        *) 
            echo "$0: Internal error parsing parameters!"  >&2
            exit 1 
            ;;
    esac
done

CDBASEDIR=$(readlink --no-newline --canonicalize-existing "$1")

if [ -z "$CDBASEDIR" ]; then
    usage_message
    echo "Unknown cd base directory" >&2
    exit 1
fi

CONTENTSDIR=$(readlink --no-newline --canonicalize-existing "$2")

if [ -z "$CONTENTSDIR" ]; then
    usage_message
    echo "Unknown directory for CD contents" >&2
    exit 1
fi

VERSION="$3"

if [ -z "$VERSION" ]; then
    usage_message
    echo "Unknown version" >&2
    exit 1
fi

NAME="$4"

if [ -z "$NAME" ]; then
    usage_message
    echo "Unknown cd name" >&2
    exit 1
fi

CDTYPE=$(echo $NAME | cut -f1 -d-)
CDNUM=""

if [ "$CDTYPE" = "CD" ]; then
    CDNUM="$(echo $NAME | cut -f2 -d-)"
fi

if [ "$CDTYPE" = "CD" ] && [ -z "$CDNUM" ]; then
    echo "I don't know how to process a CD with this type of name" >&2
    exit 1
fi

MIRRORDIR=$(readlink --no-newline --canonicalize-existing "$5")

if [ -z "$MIRRORDIR" ]; then
    usage_message
    echo "Unknown mirror directory" >&2
    exit 1
fi

ISONAME="$6"

if [ -z "$ISONAME" ]; then
    usage_message
    echo "Unknown ISO image name" >&2
    exit 1
fi

if [ "$UPDATE" = "TRUE" ]; then
    if [ -z "$OLDCDBASEDIR" ]; then
        OLDCDBASEDIR="$CDBASEDIR"
    fi
    if [ -z "$OLDCONTENTSDIR" ]; then
        OLDCONTENTSDIR="$CONTENTDIRS"
    fi
    if [ -z "$OLDVERSION" ]; then
        OLDVERSION="$VERSION"
    fi
    if [ -z "$OLDCDNAME" ]; then
        usage_message
        echo "Old CD name is required when doing updates" >&2
        exit 1
    fi
    if [ -z "$OLDISONAME" ]; then
        usage_message
        echo "Old ISO name is required when doing updates" >&2
        exit 1
    fi
fi


if [ "$VERBOSE" = "TRUE" ] || [ "$TESTPARAM" = "TRUE" ]; then
    echo "CDBASEDIR=$CDBASEDIR"
    echo "CONTENTSDIR=$CONTENTSDIR"
    echo "OLDCDBASEDIR=$OLDCDBASEDIR"
    echo "OLDCONTENTSDIR=$OLDCONTENTSDIR"
    echo "VERSION=$VERSION"
    echo "NAME=$NAME"
    echo "MIRRORDIR=$MIRRORDIR"
    echo "ISONAME=$ISONAME"
    echo "OLDVERSION=$OLDVERSION"
    echo "OLDISONAME=$OLDISONAME"
    echo "TESTPARAM=$TESTPARAM"
    echo "VERBOSE=$VERBOSE"
    echo "UPDATE=$UPDATE"
    echo "FROMISO=$FROMISO"
    echo ""
    echo "Done parsing parameters"
fi

if [ "$TESTPARAM" = "TRUE" ]; then
    exit 0
fi

if [ "$VERBOSE" = "TRUE" ]; then
    echo "mkdir -p $CONTENTSDIR/$VERSION/$NAME"
fi

VERBOSE_ARG=
[ "$VERBOSE" = "TRUE" ] && VERBOSE_ARG="--verbose"

if [ "$UPDATE" = "TRUE" ]; then
    echo "    Getting and updating to $ISONAME"
else
    echo "    Getting and adding $ISONAME"
fi

pushd "$CDBASEDIR"/cd-"$VERSION" || exit 1

if [ "$FROMISO" != "TRUE" ]; then
    JIGDOBASE=$(basename "$ISONAME" .iso)
    ln -s "$CDBASEDIR"/cd-jigdo-"$VERSION"/"$JIGDOBASE"".jigdo" .  || exit 1
    ln -s "$CDBASEDIR"/cd-jigdo-"$VERSION"/"$JIGDOBASE"".template" . || exit 1

    [ "$VERBOSE" = "TRUE" ] && echo "Creating iso using jigdo"

    RETFILE=$(mktemp -t 'jigdo-ret.XXXXXXXX')
    echo "" | jigdo-lite --noask "$JIGDOBASE"".jigdo" 2>&1 | tee -a $RETFILE
    if [ -z "$(tail $RETFILE | grep 'OK: Checksums match, image is good!')" ]; then
        echo "Jigdo failed!"
        exit 1
    fi

    [ "$VERBOSE" = "TRUE" ] && echo "Removing jigdo symlinks from iso directory"
    rm -f "$JIGDOBASE"".jigdo" || exit 1
    rm -f "$JIGDOBASE"".template" || exit 1
fi

if [ "$EXTRACD" != "TRUE" ]; then
    pool-addcd "$CDBASEDIR" "$CONTENTSDIR" "$VERSION" "$NAME" "$MIRRORDIR" "$ISONAME" "$VERBOSE_ARG" || exit 1

    if [ "$CDTYPE" = "CD" ] && [ $CDNUM -eq 1 ] || [ "$OLDNAME" = "CD-0" ]; then
        if [ "$UPDATE" = "TRUE" ]; then
            echo "    First CD and we've successfully added, therefore we're done."
        fi
        exit 0
    fi

    if [ "$UPDATE" = "TRUE" ]; then
        pool-delcd "$OLDCDBASEDIR" "$OLDCONTENTSDIR" "$OLDVERSION" "$OLDCDNAME" "$OLDISONAME" "$VERBOSE_ARG" || exit 1

        [ "$VERBOSE" = "TRUE" ] && echo "Removing dangling symlinks"

        if [ "$VERBOSE" = "TRUE" ]; then
            symlinks -dr "$MIRRORDIR" || exit 1
        else
            symlinks -dr "$MIRRORDIR" >/dev/null || exit 1
        fi

# if [ "$VERBOSE" = "TRUE" ]; then
#     while [ -n "$(ps auxw | grep '$CDBASEDIR'/cd-'$OLDVERSION'/'$OLDISONAME' | grep -v grep)" ]; do
#       echo "Waiting for fuseiso to release iso"
#       sleep 60
#     done
# else 
#     while [ -n "$(ps auxw | grep '$CDBASEDIR'/cd-'$OLDVERSION'/'$OLDISONAME' | grep -v grep)" ]; do
#       sleep 60
#     done
# fi

        [ "$VERBOSE" = "TRUE" ] && echo "Removing old version of the CD one less in sequence than this one."    
        rm -f "$OLDCDBASEDIR"/cd-"$OLDVERSION"/"$OLDISONAME" || exit 1  
        [ "$VERBOSE" = "TRUE" ] && echo "Done upgrading $NAME"
    else
        [ "$VERBOSE" = "TRUE" ] && echo "Done adding $NAME"
    fi
else # if [ "EXTRACD" = "TRUE" ]; then
    if [ "$UPDATE" = "TRUE" ]; then
        [ "$VERBOSE" = "TRUE" ] && echo "Removing old version of the CD."    
        rm -f "$OLDCDBASEDIR"/cd-"$OLDVERSION"/"$OLDISONAME" || exit 1
    fi  
fi

popd || exit 1

exit 0