Differences between revisions 1 and 2
Revision 1 as of 2009-02-26 08:17:24
Size: 11036
Comment:
Revision 2 as of 2009-03-16 03:33:28
Size: 11036
Editor: anonymous
Comment: converted to 1.6 markup
No differences found!

mirror-new-cd-set

# 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.

usage_message () {
    echo "Usage: $(basename $0) [options] \\
         cd-iso-directory cd-contents-directory version-string \\
         cd-mirror-dir cd-name-field"
    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-field old-name-field] (e.g. pos of xfce in iso for xfce CD)
    [-j|--jigdo-url URL-with-jigdo-files]
    [-f|--from-iso] (Assume iso's already created, don't use jigdo)
    [-t|--test-param]

"
}

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

popd () {
    cd "$OLDCURDIR"
    return $?
}

docmd () {
    [ "$VERBOSE" = "TRUE" ] && echo $@
    "$@" || exit 1
}

PARSEDPARAMS=$(getopt --options "vb:c:j:ptfo:n:" --longoptions "verbose,old-base-dir:,old-contents-dir:,jigdo-url:,test-param,from-iso,old-version:,old-name-field:" --name "$0" --shell sh -- "$@")

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

TESTPARAM=FALSE
JIGDOURL=
VERBOSE=FALSE
OLDCDBASEDIR=
OLDCONTENTSDIR=
OLDVERSION=
OLDNAMEFIELD=
VERBOSE_ARG=
OLDBASE_ARG=
OLDCONTENTS_ARG=
OLDVERSION_ARG=
OLDNAME_ARG=
EXTRACD_ARG=
OLDISO_ARG=
FROMISO_ARG=
UPDATE=FALSE
FROMISO=FALSE
EXTRACD=FALSE

OLDNAME=
OLDISO=

eval set -- "$PARSEDPARAMS"

while true; do
    case "$1" in
        -v|--verbose)
            VERBOSE=TRUE
            echo "Verbose messages enabled"
            VERBOSE_ARG="$1"
            shift
            ;;
        -b|--old-base-dir)
            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
            OLDBASE_ARG="$1"" ""$2"
            UPDATE=TRUE
            shift 2
            ;;
        -c|--old-contents-dir)
            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
            OLDCONTENTS_ARG="$1"" ""$2"
            UPDATE=TRUE
            shift 2
            ;;
        -j|--jigdo-url)     
            JIGDOURL=$2
            shift 2
            ;;
        -t|--test-param)
            TESTPARAM=TRUE
            shift
            ;;
        -f|--from-iso)
            FROMISO=TRUE
            FROMISO_ARG="$1"
            shift
            ;;
        -o|--old-version)
            OLDVERSION="$2"
            OLDVERSION_ARG="$1"" ""$2"
            UPDATE=TRUE
            shift 2
            ;;
        -n|--old-name-field)
            OLDNAMEFIELD="$2"
            UPDATE=TRUE
            ;;
        --) 
            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
fi

if [ -z "$4" ]; then
    usage_message
    echo "No mirror directory specified" >&2
    exit 1
fi

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

if [ -z "$MIRRORDIR" ]; then
    usage_message
    echo "Unknown mirror directory (doesn't exist?)" >&2
    exit 1
fi

NAMEFIELD=$5

if [ -z "$NAMEFIELD" ] || [ $NAMEFIELD -le 0 ]; then
    usage_message
    echo "Bad number for cd-name field" >&2
    exit 1
fi

if [ -z "$OLDCDBASEDIR" ]; then
    OLDCDBASEDIR="$CDBASEDIR"
fi
if [ -z "$OLDCONTENTSDIR" ]; then
    OLDCONTENTSDIR="$CONTENTDIRS"
fi
if [ -z "$OLDVERSION" ]; then
    OLDVERSION="$VERSION"
fi
if [ -z "$OLDNAMEFIELD" ]; then
    OLDNAMEFIELD="$NAMEFIELD"
fi

if [ "$FROMISO" = "TRUE" ] && [ -n "$JIGDOURL" ]; then
    usage_message
    echo "Can't supply a jigdo url when we're not using jigdos" >&2
    exit 1
fi

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

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

mkdir -p $CONTENTSDIR/$VERSION || exit 1

if [ "$FROMISO" = "TRUE" ]; then
    pushd "$CDBASEDIR/cd-$VERSION" || exit 1
    BASEEXT=".iso"
else
    if [ -n "$JIGDOURL" ]; then
        if [ ! -x "$CDBASEDIR/cd-jigdo-$VERSION" ]; then
            mkdir -p "$CDBASEDIR/cd-jigdo-$VERSION"
        fi
    fi
    pushd "$CDBASEDIR/cd-jigdo-$VERSION" || exit 1
    BASEEXT=".jigdo"

    if [ -n "$JIGDOURL" ]; then
        echo "Downloading jigdo files"
        if [ "$VERBOSE" = "TRUE" ]; then
            wget -O- "$JIGDOURL" | grep 'a href="' | grep -v 'index.html' | sed -e 's/.*href="\(.*\)".*/\1/' | wget --base="$JIGDOURL" -i- || exit 1
        else
            wget --quiet -O- "$JIGDOURL" | grep 'a href="' | grep -v 'index.html' | sed -e 's/.*href="\(.*\)".*/\1/' | wget --quiet --base="$JIGDOURL" -i- || exit 1
        fi
    fi
fi

determine_old_isoname() {
    if [ "$UPDATE" = "TRUE" ]; then
        SKIPNHALF=TRUE
        if [ "$CDTYPE" != "CD" ]; then
            if [ "$CDTYPE" = "xfce+lxde" ]; then
                if [ -n "echo $OLDVERSION | grep 4.0" ]; then
                    OLDNAME="xfce-CD-1"
                else
                    OLDNAME="$NAME"
                fi            
                FINDNAME="*$OLDNAME"".iso"
            elif [ "$CDTYPE" = "update" ]; then
                OLDNAME="$NAME"
                FINDNAME="*update-*-$UPDATENAME"".iso"
            elif [ "$CDTYPE" = "etchnhalf" ]; then
                OLDNAME="$NAME"
                FINDNAME="*etchnhalf-*-$NHALFNAME"".iso"
                SKIPNHALF=FALSE
            else
                OLDNAME="$NAME"
                FINDNAME="*$OLDNAME"".iso"
            fi

            if [ "$SKIPNALF" = "TRUE" ]; then
                GREPV="grep -v 'nhalf'"
            else
                GREPV="cat -"
            fi
            FINDRESULT=$(find "$OLDCDBASEDIR"/cd-"$OLDVERSION" -name "$FINDNAME" | $GREPV)
            if [ -n "$FINDRES" ]; then
                OLDISO=$(basename $FINDRESULT)          
                if [ ! -r "$CDBASEDIR/cd-$VERSION/$iso" ] && [ "$FROMISO" != "TRUE" ]; then
                    NOFINDNAME="WARNING: Couldn't find old ISO matching $FINDNAME"
                elif [ "$FROMISO" = "TRUE" ] && [ -r "$CDBASEDIR/cd-$VERSION/$iso" ]; then
                    NOFINDNAME="WARNING: Couldn't find old ISO matching $FINDNAME"                     
                else
                    NOFINDNAME=""
                fi
                [ -n "$NOFINDNAME" ] && echo "$NOFINDNAME" >&2
                OLDISO=""
            fi

            if [ -z "$OLDISO" ]; then
                OLDNAME="CD-0"
                OLDISO="ignore.iso"
            fi
        else
            if [ "$CDNUM" -eq 1 ]; then
                OLDNAME="CD-0"
                OLDISO="ignore.iso"
            elif [ "$1" != "LAST" ]; then
                OLDNAME="CD-""$(expr $CDNUM - 1)"
                OLDISO=$(basename $(find "$OLDCDBASEDIR"/cd-"$OLDVERSION" -name "*$OLDNAME"".iso"))
            else
                OLDNAME="$NAME"
                OLDISO=$(basename $(find "$OLDCDBASEDIR"/cd-"$OLDVERSION" -name "*$OLDNAME"".iso"))
            fi
        fi

        OLDNAME_ARG="--old-name $OLDNAME"
        OLDISO_ARG="--old-iso $OLDISO"
    fi

}

for basefile in $(ls *$BASEEXT | sort -n -t- -k $(expr $NAMEFIELD + 1)); do
    BASENAME="$(basename $basefile $BASEEXT)"
    NAME="$(echo $(basename $basefile $BASEEXT) | cut -f$NAMEFIELD- -d-)"

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

    if [ "$CDTYPE" != "CD" ]; then
        EXTRACD=TRUE
        EXTRACD_ARG="--extracd"
        UPDATECD="$(echo $BASENAME | cut -f2 -d- )"
        NHALFCD="$(echo $BASENAME | cut -f3 -d- )"
        if [ "$UPDATECD" = "update" ]; then
            CDNUM="$(echo $NAME | cut -f3 -d-)"
            UPDATENAME="$NAME"
            NAME="update-CD-$CDNUM"
            CDTYPE="update"
        elif [ "$NHALFCD" = "etchnhalf" ]; then
            CDTYPE="etchnhalf"      
            CDNUM="$(echo $NAME | cut -f3 -d-)"
            if [ -z "$CDNUM" ]; then
                CDNUM=1
            fi
            if [ $CDNUM -le 0 ]; then
                CDNUM=1
            fi
            NHALFNAME="$NAME"
            NAME="etchnhalf-$(echo $(basename $basefile $BASEEXT) | cut -f'$(expr $NAMEFIELD + 1)'- -d-)"
        else
            CDNUM=1
        fi
    else
        CDNUM="$(echo $NAME | cut -f2 -d-)"
        if [ -z "$CDNUM" ] || [ $CDNUM -le 0 ]; then
            echo "$BASENAME: I don't know how to process a CD with this type of name" >&2
            exit 1
        fi
    fi

    determine_old_isoname

    iso="$BASENAME"".iso"

    if [ "$VERBOSE" = "TRUE" ]; then
        echo "BASENAME=$BASENAME"
        echo "NAME=$NAME"
        echo "CDTYPE=$CDTYPE"
        echo "CDNUM=$CDNUM"
        echo "OLDNAME=$OLDNAME"
        echo "iso=$iso"
        echo "OLDISO=$OLDISO"       
        echo ""
    fi

    ACTION="Adding"
    if [ "$UPDATE" = "TRUE" ]; then
        ACTION="Updating"
    fi
    
    if [ ! -r "$CDBASEDIR/cd-$VERSION/$iso" ] && [ "$FROMISO" != "TRUE" ]; then
        echo "$ACTION $NAME"
        docmd mirror-new-cd  $VERBOSE_ARG $OLDBASE_ARG $OLDCONTENTS_ARG $OLDNAME_ARG $OLDISO_ARG $OLDVERSION_ARG $FROMISO_ARG $EXTRACD_ARG $CDBASEDIR $CONTENTSDIR $VERSION $NAME $MIRRORDIR "$iso"
    elif [ "$FROMISO" = "TRUE" ] && [ -r "$CDBASEDIR/cd-$VERSION/$iso" ]; then
        echo "$ACTION $NAME"
        docmd mirror-new-cd  $VERBOSE_ARG $OLDBASE_ARG $OLDCONTENTS_ARG $OLDNAME_ARG $OLDISO_ARG $OLDVERSION_ARG $EXTRACD_ARG $FROMISO_ARG $CDBASEDIR $CONTENTSDIR $VERSION $NAME $MIRRORDIR "$iso" $OLDVERSION
    elif  [ "$FROMISO" = "TRUE" ] && [ ! -r "$CDBASEDIR/cd-$VERSION/$iso" ]; then       echo "Missing $iso" >&2
        exit 1
    else
        echo "Skipping $iso, it already exists" >&2
    fi

    OLDNAME_ARG=
    OLDISO_ARG=
    EXTRACD_ARG=
    EXTRACD=FALSE
done

OLDISO=""

determine_old_isoname LAST

if [ -n "$OLDISO" ] && [ "$OLDISO" != "ignore.iso" ] && [ "$UPDATE" = "TRUE" ]; then
    docmd pool-delcd "$OLDCDBASEDIR" "$OLDCONTENTSDIR" "$OLDVERSION" "$OLDCDNAME" "$OLDISONAME" "$VERBOSE_ARG" || exit 1

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

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

    docmd rm -f "$OLDCDBASEDIR"/cd-"$OLDVERSION"/"$OLDISO" || exit 1    

fi

popd

docmd sudo aufs-remount

echo "Done"

exit 0